From 03d51384750102cba9ed8c515ac5588d2e14d6d8 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 29 Sep 2026 16:30:56 +0200 Subject: [PATCH] docs: add runnable library examples --- README.md | 1 + docs/README.md | 2 ++ docs/usage.md | 7 +++++++ examples/README.md | 21 +++++++++++++++++++++ examples/analyze_path.rs | 31 +++++++++++++++++++++++++++++++ examples/analyze_text.rs | 30 ++++++++++++++++++++++++++++++ examples/plan_replacements.rs | 16 ++++++++++++++++ 7 files changed, 108 insertions(+) create mode 100644 examples/README.md create mode 100644 examples/analyze_path.rs create mode 100644 examples/analyze_text.rs create mode 100644 examples/plan_replacements.rs diff --git a/README.md b/README.md index cd60ab8..6eaf608 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ signals, not the probability that AI wrote the content. ## Documentation +- [Runnable Rust examples](examples/README.md) - [Documentation index](docs/README.md) - [Command-line usage and current capabilities](docs/usage.md) - [Human, concise, and JSON output contracts](docs/output.md) diff --git a/docs/README.md b/docs/README.md index 84e65c1..b1241c5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3,6 +3,8 @@ The root [README](../README.md) is the short project overview. These guides hold the details needed to use, embed, and develop John Slop: +- [Runnable Rust examples](../examples/README.md) demonstrate in-memory + analysis, streaming path input, and shared replacement planning. - [Usage](usage.md) documents accepted inputs, detector output, exit statuses, and the current formatter/LSP status. - [Output contracts](output.md) specifies human and concise presentation plus diff --git a/docs/usage.md b/docs/usage.md index 74a0e91..7d855f8 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -62,6 +62,13 @@ Use `john-slop config show` to print the resolved TOML and `john-slop config paths` to inspect source precedence. See [Configuration](configuration.md) for the schema and merge rules. +## Rust examples + +The [`examples` directory](../examples/README.md) contains runnable Cargo +examples for direct text analysis, streaming path input, and replacement edit +planning. Run all example targets as part of the normal test matrix with +`cargo test --examples --all-features`. + ## Supported input policy Input reads use strict UTF-8 and reject NUL-containing text. They do not follow diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..2123c24 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,21 @@ +# Runnable Rust examples + +These Cargo examples exercise the same library boundaries used by the three +John Slop binaries: + +```sh +cargo run --example analyze_text +cargo run --example analyze_path -- examples/fixtures +cargo run --example plan_replacements +``` + +- `analyze_text` analyzes an in-memory prose snapshot and prints its explainable + findings. +- `analyze_path` streams a file, directory, or supported ZIP container. Its + optional path defaults to `examples/fixtures`. +- `plan_replacements` prints the exact UTF-8 byte edits that the formatter and + LSP adapters will share. + +These examples use embedded defaults so their behavior does not depend on a +developer's user configuration. Applications that want normal configuration +layering should call `john_slop::load_config()` instead. diff --git a/examples/analyze_path.rs b/examples/analyze_path.rs new file mode 100644 index 0000000..01486d6 --- /dev/null +++ b/examples/analyze_path.rs @@ -0,0 +1,31 @@ +//! Stream and analyze every supported document under a path. + +use std::{env, error::Error, path::PathBuf}; + +use john_slop::{AnalysisContext, Config, Detector, read_inputs}; + +fn main() -> Result<(), Box> { + let path = env::args_os() + .nth(1) + .map_or_else(|| PathBuf::from("examples/fixtures"), PathBuf::from); + let config = Config::embedded(); + let detector = Detector::new(&config)?; + + for document in read_inputs(&config, &path)? { + match document { + Ok(document) => { + let context = AnalysisContext::for_source(&document.source); + let analysis = detector.analyze_with(&document.content, &context); + println!( + "{}: confidence={}, findings={}", + document.source, + analysis.confidence.score(), + analysis.findings.len() + ); + } + Err(error) => eprintln!("input error: {error}"), + } + } + + Ok(()) +} diff --git a/examples/analyze_text.rs b/examples/analyze_text.rs new file mode 100644 index 0000000..a20ccb1 --- /dev/null +++ b/examples/analyze_text.rs @@ -0,0 +1,30 @@ +//! Analyze an in-memory document with the embedded heuristic catalog. + +use std::error::Error; + +use john_slop::{AnalysisContext, Config, Detector}; + +fn main() -> Result<(), Box> { + let source = "As an AI language model, I can help—but this draft needs review."; + let detector = Detector::new(&Config::embedded())?; + let analysis = detector.analyze_with(source, &AnalysisContext::for_path("article.md")); + let summary = analysis.summary(); + + println!( + "confidence={} band={} findings={}", + summary.confidence_score, + summary + .confidence_band + .map_or("none", john_slop::ConfidenceBand::as_str), + summary.findings.total() + ); + + for finding in analysis.findings { + println!( + "{} {}..{} {:?}", + finding.rule_id, finding.byte_span.start, finding.byte_span.end, finding.matched_text + ); + } + + Ok(()) +} diff --git a/examples/plan_replacements.rs b/examples/plan_replacements.rs new file mode 100644 index 0000000..96621b8 --- /dev/null +++ b/examples/plan_replacements.rs @@ -0,0 +1,16 @@ +//! Inspect the edits shared by detector, formatter, and future LSP adapters. + +use john_slop::{Config, plan_replacements}; + +fn main() { + let source = "Review—then subtract 5−2."; + let edits = plan_replacements(&Config::embedded(), source); + + println!("source: {source}"); + for edit in edits { + println!( + "replace bytes {}..{} with {:?}", + edit.byte_span.start, edit.byte_span.end, edit.replacement + ); + } +}