use std::fmt;
use url::Url;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub struct SchemaId {
pub uri: Url,
pub pointer: String,
}
impl SchemaId {
pub fn new(uri: Url, pointer: impl Into<String>) -> Self {
SchemaId {
uri,
pointer: pointer.into(),
}
}
}
impl fmt::Display for SchemaId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}#{}", self.uri, self.pointer)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Severity {
Warning,
Error,
}
impl fmt::Display for Severity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Severity::Warning => "warning",
Severity::Error => "error",
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DiagnosticKind {
NormalizationLoss { keyword: String },
ExternalReference { reference: String },
AnchorReference { reference: String },
IdRebasing { id: String },
UnsupportedReferenceTarget { reference: String },
UnsupportedKeyword { keyword: String },
Ungeneratable { keyword: String },
UnsupportedDialect { dialect: String },
LossyNumber,
OpenComposition { keyword: String },
TypelessObject,
NumericEnum,
IgnoredExtension { keyword: String },
ReferenceCycle,
Unformatted,
Unvalidated { keyword: String },
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Diagnostic {
pub severity: Severity,
pub kind: DiagnosticKind,
pub schema_id: SchemaId,
pub pointer: String,
pub message: String,
}
impl Diagnostic {
pub fn new(
severity: Severity,
kind: DiagnosticKind,
schema_id: SchemaId,
pointer: impl Into<String>,
message: impl Into<String>,
) -> Self {
Diagnostic {
severity,
kind,
schema_id,
pointer: pointer.into(),
message: message.into(),
}
}
}
impl fmt::Display for Diagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}: {}#{}: {}",
self.severity, self.schema_id.uri, self.pointer, self.message
)
}
}
pub(crate) fn synthetic(pointer: &str, label: &str) -> String {
format!("{pointer}/~{label}")
}
pub(crate) fn source_of(pointer: &str) -> String {
pointer
.split('/')
.filter(|segment| {
!(segment.starts_with('~') && !segment.starts_with("~0") && !segment.starts_with("~1"))
})
.collect::<Vec<_>>()
.join("/")
}
pub(crate) fn push_token(pointer: &mut String, token: &str) {
pointer.push('/');
for c in token.chars() {
match c {
'~' => pointer.push_str("~0"),
'/' => pointer.push_str("~1"),
c => pointer.push(c),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn schema_id_displays_as_uri_and_pointer() {
let id = SchemaId::new(
Url::parse("file:///a.json").unwrap(),
"/components/schemas/Pet",
);
assert_eq!(id.to_string(), "file:///a.json#/components/schemas/Pet");
}
#[test]
fn pointer_tokens_are_escaped() {
let mut p = String::new();
push_token(&mut p, "a/b");
push_token(&mut p, "c~d");
assert_eq!(p, "/a~1b/c~0d");
}
#[test]
fn diagnostic_displays_severity_location_and_message() {
let d = Diagnostic {
severity: Severity::Warning,
kind: DiagnosticKind::NormalizationLoss {
keyword: "collectionFormat".into(),
},
schema_id: SchemaId::new(Url::parse("file:///a.json").unwrap(), "/parameters/0"),
pointer: "/parameters/0/collectionFormat".into(),
message: "dropped".into(),
};
assert_eq!(
d.to_string(),
"warning: file:///a.json#/parameters/0/collectionFormat: dropped"
);
assert!(Severity::Warning < Severity::Error);
}
}