Skip to main content

shacl_ir/compiled/
severity.rs

1use super::compiled_shacl_error::CompiledShaclError;
2use iri_s::IriS;
3use rudof_rdf::rdf_core::vocabs::ShaclVocab;
4use shacl_ast::severity::Severity;
5use std::fmt::Display;
6
7#[derive(Hash, Clone, PartialEq, Eq, Debug)]
8pub enum CompiledSeverity {
9    Trace,
10    Debug,
11    Info,
12    Warning,
13    Violation,
14    Generic(IriS),
15}
16
17impl CompiledSeverity {
18    pub fn iri(&self) -> IriS {
19        match self {
20            CompiledSeverity::Trace => ShaclVocab::sh_trace().clone(),
21            CompiledSeverity::Debug => ShaclVocab::sh_debug().clone(),
22            CompiledSeverity::Violation => ShaclVocab::sh_violation().clone(),
23            CompiledSeverity::Warning => ShaclVocab::sh_warning().clone(),
24            CompiledSeverity::Info => ShaclVocab::sh_info().clone(),
25            CompiledSeverity::Generic(iri) => iri.clone(),
26        }
27    }
28
29    pub fn compile(severity: Option<Severity>) -> Result<Option<Self>, Box<CompiledShaclError>> {
30        let ans = match severity {
31            Some(severity) => {
32                let severity = match severity {
33                    Severity::Trace => CompiledSeverity::Trace,
34                    Severity::Debug => CompiledSeverity::Debug,
35                    Severity::Violation => CompiledSeverity::Violation,
36                    Severity::Warning => CompiledSeverity::Warning,
37                    Severity::Info => CompiledSeverity::Info,
38                    Severity::Generic(iri_ref) => {
39                        let iri = iri_ref.get_iri().map_err(|e| CompiledShaclError::IriRefConversion {
40                            iri_ref: iri_ref.to_string(),
41                            err: e.to_string(),
42                        })?;
43                        CompiledSeverity::Generic(iri.clone())
44                    },
45                };
46                Some(severity)
47            },
48            None => None,
49        };
50
51        Ok(ans)
52    }
53
54    pub fn from_iri(iri: &IriS) -> Option<Self> {
55        if iri == ShaclVocab::sh_trace() {
56            Some(CompiledSeverity::Trace)
57        } else if iri == ShaclVocab::sh_debug() {
58            Some(CompiledSeverity::Debug)
59        } else if iri == ShaclVocab::sh_violation() {
60            Some(CompiledSeverity::Violation)
61        } else if iri == ShaclVocab::sh_warning() {
62            Some(CompiledSeverity::Warning)
63        } else if iri == ShaclVocab::sh_info() {
64            Some(CompiledSeverity::Info)
65        } else {
66            Some(CompiledSeverity::Generic(iri.clone()))
67        }
68    }
69
70    pub fn to_iri(&self) -> IriS {
71        match self {
72            CompiledSeverity::Trace => ShaclVocab::sh_trace().clone(),
73            CompiledSeverity::Debug => ShaclVocab::sh_debug().clone(),
74            CompiledSeverity::Violation => ShaclVocab::sh_violation().clone(),
75            CompiledSeverity::Warning => ShaclVocab::sh_warning().clone(),
76            CompiledSeverity::Info => ShaclVocab::sh_info().clone(),
77            CompiledSeverity::Generic(iri) => iri.clone(),
78        }
79    }
80}
81
82impl From<&CompiledSeverity> for IriS {
83    fn from(value: &CompiledSeverity) -> Self {
84        CompiledSeverity::to_iri(value)
85    }
86}
87
88impl Display for CompiledSeverity {
89    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90        match self {
91            CompiledSeverity::Trace => write!(f, "Trace"),
92            CompiledSeverity::Debug => write!(f, "Debug"),
93            CompiledSeverity::Violation => write!(f, "Violation"),
94            CompiledSeverity::Warning => write!(f, "Warning"),
95            CompiledSeverity::Info => write!(f, "Info"),
96            CompiledSeverity::Generic(iri) => write!(f, "Generic({iri})"),
97        }
98    }
99}