openakb_validate/
result.rs1use std::collections::BTreeSet;
4
5use serde::Serialize;
6
7use crate::Code;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Segment<'a> {
12 Key(&'a str),
14 Index(usize),
16}
17
18#[must_use]
20pub fn json_pointer<'a>(segments: impl IntoIterator<Item = Segment<'a>>) -> String {
21 let mut pointer = String::new();
22
23 for segment in segments {
24 pointer.push('/');
25 match segment {
26 Segment::Key(key) => push_escaped_key(&mut pointer, key),
27 Segment::Index(index) => pointer.push_str(&index.to_string()),
28 }
29 }
30
31 pointer
32}
33
34fn push_escaped_key(pointer: &mut String, key: &str) {
35 for character in key.chars() {
36 match character {
37 '~' => pointer.push_str("~0"),
38 '/' => pointer.push_str("~1"),
39 _ => pointer.push(character),
40 }
41 }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
49pub struct Finding {
50 pub code: Code,
52 pub path: String,
54 pub message: String,
56}
57
58impl Finding {
59 #[must_use]
61 pub const fn name(&self) -> &'static str {
62 self.code.name()
63 }
64}
65
66#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
70pub struct Advisory {
71 pub path: String,
73 pub message: String,
75}
76
77#[derive(Debug, Clone, Default, Serialize)]
79pub struct ValidationResult {
80 pub findings: Vec<Finding>,
82 pub warnings: Vec<Advisory>,
84}
85
86impl ValidationResult {
87 #[must_use]
89 pub fn ok(&self) -> bool {
90 self.findings.is_empty()
91 }
92
93 #[must_use]
95 pub fn codes(&self) -> BTreeSet<Code> {
96 self.findings.iter().map(|finding| finding.code).collect()
97 }
98}