pub mod baseline;
pub mod check;
pub mod compare;
pub mod config;
pub mod contract;
pub mod demand;
pub mod init;
pub mod mcp;
pub mod render;
pub mod report;
pub mod rules;
pub mod surface;
pub use crate::check::{Options, Scope};
pub use crate::compare::Change;
pub use crate::config::{Compatibility as Level, Config};
pub use crate::contract::{
Contract, DocumentResolver, FileSystemResolver, InMemoryResolver, SingleDocumentResolver,
};
pub use crate::report::Report;
pub use crate::rules::Finding;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub type Format = crate::config::ContractFormat;
pub fn parse(format: Format, source: &str, bytes: &[u8]) -> Result<Contract, String> {
parse_with_resolver(format, source, bytes, &SingleDocumentResolver)
}
pub fn parse_with_resolver(
format: Format,
source: &str,
bytes: &[u8],
resolver: &dyn DocumentResolver,
) -> Result<Contract, String> {
match format {
Format::Openapi => contract::openapi::ingest_with_resolver(source, bytes, resolver)
.map_err(|e| e.to_string()),
Format::Proto => contract::proto::ingest_with_resolver(source, bytes, resolver)
.map_err(|e| e.to_string()),
Format::Graphql => contract::graphql::ingest_with_resolver(source, bytes, resolver)
.map_err(|e| e.to_string()),
Format::Asyncapi => contract::asyncapi::ingest_with_resolver(source, bytes, resolver)
.map_err(|e| e.to_string()),
Format::Mcp => {
contract::mcp::ingest_with_resolver(source, bytes, resolver).map_err(|e| e.to_string())
}
}
}
#[must_use]
pub fn compare(base: &Contract, head: &Contract) -> Vec<Change> {
let mut changes = compare::compare_contracts(base, head);
changes.extend(compare::partial_changes(head));
changes.sort();
changes.dedup();
changes
}
#[must_use]
pub fn evaluate(changes: &[Change], contract: &str, level: Level) -> Vec<Finding> {
rules::evaluate(changes, contract, level)
}
#[must_use]
pub fn lint(base: &Contract, head: &Contract, contract: &str, level: Level) -> Vec<Finding> {
rules::lint::introduced(base, head, contract, level)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
Info,
Warning,
Error,
}
impl Severity {
pub fn parse(input: &str) -> Result<Self, String> {
match input.trim().to_ascii_lowercase().as_str() {
"info" => Ok(Self::Info),
"warning" | "warn" => Ok(Self::Warning),
"error" | "err" => Ok(Self::Error),
other => Err(format!(
"unknown severity `{other}`; expected one of: info, warning, error"
)),
}
}
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Info => "info",
Self::Warning => "warning",
Self::Error => "error",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Verdict {
Clean,
Findings,
ToolFailure,
}
impl Verdict {
#[must_use]
pub fn exit_code(self) -> i32 {
match self {
Verdict::Clean => 0,
Verdict::Findings => 1,
Verdict::ToolFailure => 2,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exit_codes_match_the_documented_contract() {
assert_eq!(Verdict::Clean.exit_code(), 0);
assert_eq!(Verdict::Findings.exit_code(), 1);
assert_eq!(Verdict::ToolFailure.exit_code(), 2);
}
#[test]
fn severity_orders_for_threshold_comparison() {
assert!(Severity::Error > Severity::Warning);
assert!(Severity::Warning > Severity::Info);
}
#[test]
fn version_is_calver_not_a_placeholder() {
assert!(
VERSION.starts_with("2026."),
"unexpected version: {VERSION}"
);
assert_ne!(VERSION, "0.0.0");
}
#[test]
fn the_public_api_round_trips_bytes_to_findings() {
let base = parse(
Format::Openapi,
"api/openapi.yaml",
b"openapi: 3.1.0\npaths:\n /p:\n get:\n operationId: getP\n responses:\n \"200\": { description: ok }\n",
)
.expect("base");
let head = parse(
Format::Openapi,
"api/openapi.yaml",
b"openapi: 3.1.0\npaths: {}\n",
)
.expect("head");
let findings = evaluate(&compare(&base, &head), "payments", Level::WireJson);
assert!(findings.iter().any(|f| f.rule_id == "endpoint-removed"));
assert!(findings.iter().all(|f| f.contract == "payments"));
}
#[test]
fn parse_reports_a_refused_ref_rather_than_returning_a_partial_contract() {
let error = parse(
Format::Openapi,
"api/openapi.yaml",
b"openapi: 3.1.0\npaths:\n /p:\n get:\n responses:\n \"200\":\n content:\n application/json:\n schema:\n $ref: 'http://example.com/x.yaml'\n",
)
.expect_err("a remote ref must not parse");
assert!(error.contains("network"), "{error}");
}
}