Skip to main content

chio_openapi/
lib.rs

1//! OpenAPI 3.x spec parser and Chio `ToolManifest` generator.
2//!
3//! This crate parses OpenAPI 3.0 and 3.1 specifications (both YAML and JSON)
4//! and generates Chio `ToolManifest` values where each route becomes a
5//! `ToolDefinition` with input schema derived from path, query, and body
6//! parameters.
7
8#![forbid(unsafe_code)]
9
10mod extensions;
11mod generator;
12mod parser;
13mod policy;
14
15pub use extensions::{ChioExtensions, Sensitivity};
16pub use generator::{GeneratorConfig, ManifestGenerator};
17pub use parser::{OpenApiSpec, Operation, Parameter, ParameterLocation, PathItem};
18pub use policy::{DefaultPolicy, PolicyDecision};
19
20use thiserror::Error;
21
22/// Errors produced by the OpenAPI parser and manifest generator.
23#[derive(Debug, Error)]
24pub enum OpenApiError {
25    /// The input is not valid JSON.
26    #[error("invalid JSON: {0}")]
27    InvalidJson(#[from] serde_json::Error),
28
29    /// The input is not valid YAML.
30    #[error("invalid YAML: {0}")]
31    InvalidYaml(#[from] serde_yaml::Error),
32
33    /// The OpenAPI spec is missing a required field.
34    #[error("missing required field: {0}")]
35    MissingField(String),
36
37    /// The OpenAPI version is not supported.
38    #[error("unsupported OpenAPI version: {0}")]
39    UnsupportedVersion(String),
40
41    /// A `$ref` could not be resolved.
42    #[error("unresolved reference: {0}")]
43    UnresolvedRef(String),
44
45    /// The OpenAPI document contains an invalid value.
46    #[error("invalid OpenAPI spec: {0}")]
47    InvalidSpec(String),
48}
49
50/// Result alias for this crate.
51pub type Result<T> = std::result::Result<T, OpenApiError>;
52
53/// Convenience function: parse an OpenAPI spec from a string (auto-detecting
54/// JSON vs YAML) and generate a list of `ToolDefinition` values.
55///
56/// For more control, use `OpenApiSpec::parse` and `ManifestGenerator`
57/// directly.
58pub fn tools_from_spec(input: &str) -> Result<Vec<chio_core_types::ToolDefinition>> {
59    let spec = OpenApiSpec::parse(input)?;
60    let generator = ManifestGenerator::new(GeneratorConfig::default());
61    Ok(generator.generate_tools(&spec))
62}