docs: add runnable library examples

This commit is contained in:
Dandaedre 2026-09-29 16:30:56 +02:00
parent f2360c3d60
commit 03d5138475
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
7 changed files with 108 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

21
examples/README.md Normal file
View File

@ -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.

31
examples/analyze_path.rs Normal file
View File

@ -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<dyn Error>> {
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(())
}

30
examples/analyze_text.rs Normal file
View File

@ -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<dyn Error>> {
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(())
}

View File

@ -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
);
}
}