jsonld_expansion/
warning.rs1use contextual::DisplayWithContext;
2use jsonld_context_processing::algorithm::MalformedIri;
3use langtag::InvalidLangTag;
4use rdfx::vocabulary::BlankIdVocabulary;
5use std::fmt;
6
7#[derive(Debug)]
8pub enum Warning<B> {
10 MalformedIri(String),
14 EmptyTerm,
16 BlankNodeIdProperty(B),
19 MalformedLanguageTag(String, InvalidLangTag<String>),
23 ContextProcessing(jsonld_context_processing::Warning),
25}
26
27impl<B> Warning<B> {
28 pub fn map_blank_id<C>(self, map: impl FnOnce(B) -> C) -> Warning<C> {
35 match self {
36 Self::MalformedIri(s) => Warning::MalformedIri(s),
37 Self::EmptyTerm => Warning::EmptyTerm,
38 Self::BlankNodeIdProperty(b) => Warning::BlankNodeIdProperty(map(b)),
39 Self::MalformedLanguageTag(t, e) => Warning::MalformedLanguageTag(t, e),
40 Self::ContextProcessing(w) => Warning::ContextProcessing(w),
41 }
42 }
43}
44
45impl<B> From<jsonld_context_processing::Warning> for Warning<B> {
46 fn from(w: jsonld_context_processing::Warning) -> Self {
47 Self::ContextProcessing(w)
48 }
49}
50
51pub(crate) struct ContextWarnings<'a, W>(pub &'a mut W);
61
62impl<N, W> jsonld_core::warning::Handler<N, jsonld_context_processing::Warning> for ContextWarnings<'_, W>
63where
64 N: BlankIdVocabulary,
65 W: crate::WarningHandler<N>,
66{
67 fn handle(&mut self, vocabulary: &N, warning: jsonld_context_processing::Warning) {
68 self.0.handle(vocabulary, Warning::ContextProcessing(warning));
69 }
70}
71
72impl<B> From<MalformedIri> for Warning<B> {
73 fn from(MalformedIri(s): MalformedIri) -> Self {
74 Self::MalformedIri(s)
75 }
76}
77
78impl<B: fmt::Display> fmt::Display for Warning<B> {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 match self {
81 Self::MalformedIri(s) => write!(f, "malformed IRI `{s}`"),
82 Self::EmptyTerm => write!(f, "empty term"),
83 Self::BlankNodeIdProperty(b) => {
84 write!(f, "blank node identifier `{b}` used as property")
85 }
86 Self::MalformedLanguageTag(t, e) => write!(f, "invalid language tag `{t}`: {e}"),
87 Self::ContextProcessing(w) => w.fmt(f),
88 }
89 }
90}
91
92impl<B, N: BlankIdVocabulary<BlankId = B>> DisplayWithContext<N> for Warning<B> {
93 fn fmt_with(&self, vocabulary: &N, f: &mut fmt::Formatter) -> fmt::Result {
94 match self {
95 Self::MalformedIri(s) => write!(f, "malformed IRI `{s}`"),
96 Self::EmptyTerm => write!(f, "empty term"),
97 Self::BlankNodeIdProperty(b) => match vocabulary.blank_id(b) {
98 Some(s) => write!(f, "blank node identifier `{s}` used as property"),
99 None => f.write_str("blank node identifier `<unresolved blank>` used as property"),
100 },
101 Self::MalformedLanguageTag(t, e) => write!(f, "invalid language tag `{t}`: {e}"),
102 Self::ContextProcessing(w) => write!(f, "{w}"),
103 }
104 }
105}