Library
Use linecop as a Rust library.
Manual contents
linecop provides a library and a binary. The library runs the same pipeline as the command: discover and load a config, count files, resolve each file’s caps, and report the gauges that breach.
[dependencies]
linecop = "0.9.1"The crate needs Rust 1.90 or newer. The examples use anyhow for errors, the same error type the crate returns.
The entry points
| Item | What it does |
|---|---|
run | Scans a path with RunOptions and returns true when a file breaches the reporting line. |
check_content | Checks an in-memory buffer with an explicit config, language, and baseline. It never reads the path. |
explain | Returns the rendered resolution for one file: count mode, matching rules, and effective caps. |
config | Loads and models .linecop.yaml: Config, Cap, CapSet, and Resolver. |
checker | The report types: CheckReport, FileReport, Gauge, and ScopeGauge. |
report | The output layer: Format and print. |
The stable surface is run, check_content, explain, config, CheckReport, FileReport, Gauge, ScopeGauge, Format, and report::print. init, schema, and discovery::ConfigSearch are public for the binary. The comment and counter modules are internal.
Config, Cap, CheckReport, Gauge, ScopeGauge, and RunOptions are non-exhaustive. Adding a field is not a break, and RunOptions::new builds the defaults.
Run a scan
use std::path::Path;
use anyhow::Result;
use linecop::{RunOptions, run};
fn main() -> Result<()> {
let options = RunOptions::new();
let over = run(Some(Path::new("src")), &options)?;
std::process::exit(if over { 1 } else { 0 });
}run uses the same config discovery as the binary. Set options.config_path for one file, options.no_config for the built-in default, or options.baseline for a headroom check.
Check a buffer
check_content takes the source bytes, the config, an optional language, and a baseline. It returns a CheckReport with the same gauges a scan would measure, without reading the path, so an editor can check unsaved text:
use std::path::Path;
use anyhow::Result;
use linecop::{check_content, config};
fn check(source: &str) -> Result<()> {
let config = config::load(Path::new(".linecop.yaml"))?;
let report = check_content(
Path::new("src/lib.rs"),
source.as_bytes(),
&config,
None,
100,
)?;
for file in &report.files {
if file.has_breaches() {
println!("{} needs attention", file.path.display());
}
}
Ok(())
}language overrides the extension or shebang when the path names neither. A baseline of 100 reports only files over a cap. A lower value reports files at or above that percentage.
Explain a file
use std::path::Path;
use anyhow::Result;
use linecop::RunOptions;
fn main() -> Result<()> {
let options = RunOptions::new();
let text = linecop::explain(Path::new("src/lib.rs"), &options)?;
println!("{text}");
Ok(())
}The text is the same output as linecop explain, including each matching rule and the effective caps.
Print a report
report::print writes a CheckReport in text, JSON, or paths form:
use std::path::Path;
use anyhow::Result;
use linecop::report::{Format, print};
use linecop::{check_content, config};
fn main() -> Result<()> {
let config = config::load(Path::new(".linecop.yaml"))?;
let source = std::fs::read(Path::new("src/lib.rs"))?;
let report = check_content(
Path::new("src/lib.rs"),
&source,
&config,
None,
100,
)?;
let mut stdout = std::io::stdout();
print(&mut stdout, &report, Format::Text, None)?;
Ok(())
}Format::Text prints the human report, Format::Json prints the array, and Format::Paths prints one path per line. The last argument is the optional top count.
The API documentation lists every public item. The usage page shows the command-line behavior these calls mirror.