Skip to content

Embed Sonjoo in a Rust application

View as Markdown

Use the Sonjoo library when your Rust process should run the audit directly. Pass AuditOptions, a shared cancellation flag, and a progress callback to engine::audit. It returns the report as a JSON value.

Build against a specific Sonjoo checkout or release and test your integration when upgrading. Place your example project beside a sonjoo-seo-audit checkout, or adjust the path below.

The example needs the same Rust and platform build tools as building the CLI from source.

Cargo.toml
[package]
name = "sonjoo-embed-example"
version = "0.1.0"
edition = "2021"
[dependencies]
sonjoo = { path = "../sonjoo-seo-audit" }

Save this file as src/main.rs.

src/main.rs
use std::{
env,
error::Error,
sync::{atomic::AtomicBool, Arc},
};
use sonjoo::{
engine::{self, AuditOptions},
reports,
};
fn main() -> Result<(), Box<dyn Error>> {
let url = env::args().nth(1).ok_or("Pass the URL to audit.")?;
let options = AuditOptions {
url,
categories: vec!["core".to_owned()],
save: false,
..AuditOptions::default()
};
let cancel = Arc::new(AtomicBool::new(false));
let report = engine::audit(&options, Arc::clone(&cancel), |_| {})?;
let finding = reports::findings_json(&report)
.into_iter()
.find(|row| row["ruleId"] == "core-title-present")
.ok_or("The report did not include the failing title check.")?;
println!("{finding}");
Ok(())
}

In another terminal, start the fixture from the Sonjoo checkout.

Terminal window
python3 tests/fixtures/sites.py --port 18765

From the example project, run the audit against the page with an intentionally missing title.

Terminal window
cargo run -- 'http://127.0.0.1:18765/healthy?remove-title=1'

The finding contains ruleId: "core-title-present", status: "fail", and the affected URL and missing-title evidence in details. The example searches the fix-oriented finding list. To inspect passing and unmeasured rule results, read categoryResults on the report.

Entry point When to use it
engine::AuditOptions Select the URL, audit mode, categories, and limits.
engine::audit Run with the default engine configuration.
engine::audit_configured Pass the engine a configuration value explicitly. Start with config::default_config() when composing overrides.
reports::findings_json Extract the fix-oriented finding list from a report.
reports::render Render a report into a format such as json, html, markdown, or llm.

The audit call blocks until it returns a report or an error. In an async application, run it on a worker that can block. Pass a callback to receive progress values. To request cancellation, keep a clone of the Arc<AtomicBool> and set it to true from the controlling thread. The library call returns the report to your application. It does not add an entry to audit history or apply the CLI exit-code gates. Configured external-link checks can still use the local link cache.

For a separate local Sonjoo process, use the HTTP API.