use crate::Section;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ResolveError {
NotALocalReference {
reference: String,
},
ExternalDocument {
document: String,
},
MalformedPointer {
reference: String,
},
UnsupportedRootSection {
section: String,
},
UnknownSection {
section: String,
},
ComponentsMissing {
section: Section,
},
NotFound {
section: Section,
name: String,
},
SectionMismatch {
expected: Section,
found: Section,
},
PointerTooDeep {
reference: String,
},
ReferenceChainTooLong {
reference: String,
last: String,
max_hops: usize,
},
CyclicReference {
reference: String,
},
DiscriminatorMappingMismatch {
property_name: String,
value: String,
schema: String,
},
}
impl fmt::Display for ResolveError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotALocalReference { reference } => {
write!(
f,
"`{reference}` is not a local JSON pointer (expected `#/...`)"
)
}
Self::ExternalDocument { document } => {
write!(
f,
"reference points into `{document}`, which is a separate document"
)
}
Self::MalformedPointer { reference } => {
write!(f, "`{reference}` does not name a component")
}
Self::UnsupportedRootSection { section } => {
write!(f, "cannot resolve references into `{section}`")
}
Self::UnknownSection { section } => {
write!(f, "`{section}` is not a known section of `#/components`")
}
Self::ComponentsMissing { section } => {
write!(
f,
"document has no `components` object to look up `{section}` in"
)
}
Self::NotFound { section, name } => {
write!(f, "`{section}` does not contain `{name}`")
}
Self::SectionMismatch { expected, found } => {
write!(
f,
"expected a reference into `{expected}` but got one into `{found}`"
)
}
Self::PointerTooDeep { reference } => {
write!(
f,
"`{reference}` points inside a component; only whole components resolve"
)
}
Self::ReferenceChainTooLong {
reference,
last,
max_hops,
} => {
write!(
f,
"`{reference}` did not reach an item within {max_hops} hops, \
still at `{last}` (cyclic?)"
)
}
Self::CyclicReference { reference } => {
write!(
f,
"`{reference}` refers back to a non-schema component that contains it, \
which cannot be fully resolved"
)
}
Self::DiscriminatorMappingMismatch {
property_name,
value,
schema,
} => {
write!(
f,
"discriminator `{property_name}` maps `{value}` to `{schema}`, \
which is not one of the schema's alternatives"
)
}
}
}
}
impl std::error::Error for ResolveError {}