use std::collections::BTreeSet;
use serde::Serialize;
use crate::Code;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Segment<'a> {
Key(&'a str),
Index(usize),
}
#[must_use]
pub fn json_pointer<'a>(segments: impl IntoIterator<Item = Segment<'a>>) -> String {
let mut pointer = String::new();
for segment in segments {
pointer.push('/');
match segment {
Segment::Key(key) => push_escaped_key(&mut pointer, key),
Segment::Index(index) => pointer.push_str(&index.to_string()),
}
}
pointer
}
fn push_escaped_key(pointer: &mut String, key: &str) {
for character in key.chars() {
match character {
'~' => pointer.push_str("~0"),
'/' => pointer.push_str("~1"),
_ => pointer.push(character),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub struct Finding {
pub code: Code,
pub path: String,
pub message: String,
}
impl Finding {
#[must_use]
pub const fn name(&self) -> &'static str {
self.code.name()
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub struct Advisory {
pub path: String,
pub message: String,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ValidationResult {
pub findings: Vec<Finding>,
pub warnings: Vec<Advisory>,
}
impl ValidationResult {
#[must_use]
pub fn ok(&self) -> bool {
self.findings.is_empty()
}
#[must_use]
pub fn codes(&self) -> BTreeSet<Code> {
self.findings.iter().map(|finding| finding.code).collect()
}
}