use std::collections::{BTreeMap, BTreeSet};
pub mod asyncapi;
pub mod graphql;
pub mod mcp;
pub mod openapi;
pub mod proto;
pub mod resolver;
pub use resolver::{
DocumentResolver, FileSystemResolver, InMemoryResolver, SingleDocumentResolver,
};
pub const MEDIA_ANY: &str = "*/*";
pub const MEDIA_GRPC: &str = "application/grpc";
pub const MEDIA_GRPC_STREAM: &str = "application/grpc+stream";
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct EndpointKey {
pub method: String,
pub path: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Contract {
pub endpoints: BTreeMap<EndpointKey, Endpoint>,
pub security_schemes: BTreeMap<String, SecurityScheme>,
pub unmodelled: Vec<Unmodelled>,
pub lint_candidates: Vec<LintCandidate>,
}
impl Contract {
#[must_use]
pub fn empty() -> Self {
Self {
endpoints: BTreeMap::new(),
security_schemes: BTreeMap::new(),
unmodelled: Vec::new(),
lint_candidates: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LintCandidate {
pub kind: LintCandidateKind,
pub pointer: String,
pub span: Span,
pub detail: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LintCandidateKind {
EnumTypeMismatch,
EnumDuplicateValue,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Endpoint {
pub operation_id: Option<String>,
pub deprecated: bool,
pub sunset: Option<String>,
pub parameters: Vec<Parameter>,
pub request: Option<Payload>,
pub responses: BTreeMap<String, Payload>,
pub security: Vec<SecurityRequirement>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Parameter {
pub name: String,
pub location: String,
pub required: bool,
pub deprecated: bool,
pub ty: TypeRef,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Payload {
pub media_types: BTreeMap<String, TypeRef>,
pub span: Span,
}
impl Payload {
#[must_use]
pub fn single(ty: TypeRef, span: Span) -> Self {
Self {
media_types: BTreeMap::from([(MEDIA_ANY.to_owned(), ty)]),
span,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct SecurityRequirement {
pub name: String,
pub scopes: BTreeSet<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SecurityScheme {
pub ty: String,
pub scheme: Option<String>,
pub flows: BTreeSet<String>,
pub location: Option<String>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Unmodelled {
pub kind: UnmodelledKind,
pub pointer: String,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum UnmodelledKind {
Unsupported(String),
ExternalRef(String),
UnresolvableRef(String),
SchemaDeferred,
InvalidShape,
}
impl UnmodelledKind {
#[must_use]
pub fn describe(&self) -> String {
match self {
Self::Unsupported(detail) => format!("unsupported construct `{detail}`"),
Self::ExternalRef(reference) => {
format!("`$ref` into another file (`{reference}`), which ingest does not read")
}
Self::UnresolvableRef(reference) => {
format!("`$ref` `{reference}` does not resolve in this document")
}
Self::SchemaDeferred => "no schema declared".to_owned(),
Self::InvalidShape => "malformed schema".to_owned(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Span {
pub file: String,
pub line: usize,
pub column: usize,
pub pointer: String,
}
impl Span {
#[must_use]
pub fn new(file: &str, line: usize, column: usize, pointer: impl Into<String>) -> Self {
Self {
file: file.to_owned(),
line,
column,
pointer: pointer.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Discriminator {
pub property_name: String,
pub mapping: BTreeMap<String, String>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum TypeRef {
Scalar {
ty: String,
format: Option<String>,
nullable: bool,
constraints: Constraints,
},
Enum {
values: BTreeSet<String>,
numbers: BTreeMap<String, i32>,
},
Array {
items: Box<TypeRef>,
nullable: bool,
},
Tuple {
prefix_items: Vec<TypeRef>,
additional_items: Option<Box<TypeRef>>,
nullable: bool,
},
Object {
fields: BTreeMap<String, Field>,
additional: bool,
nullable: bool,
},
OneOf {
variants: Vec<TypeRef>,
discriminator: Option<Discriminator>,
},
Cycle(String),
Unknown(UnmodelledKind),
}
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct Constraints {
pub minimum: Option<String>,
pub maximum: Option<String>,
pub min_length: Option<u64>,
pub max_length: Option<u64>,
pub pattern: Option<String>,
}
impl Constraints {
#[must_use]
pub fn is_empty(&self) -> bool {
self == &Self::default()
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Field {
pub ty: TypeRef,
pub required: bool,
pub deprecated: bool,
pub number: Option<i32>,
pub span: Option<Span>,
}
impl Field {
#[must_use]
pub fn new(ty: TypeRef, required: bool) -> Self {
Self {
ty,
required,
deprecated: false,
number: None,
span: None,
}
}
#[must_use]
pub fn at(ty: TypeRef, required: bool, span: Span) -> Self {
Self {
span: Some(span),
..Self::new(ty, required)
}
}
}