1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use contextual::DisplayWithContext;
use json_ld_context_processing::syntax::MalformedIri;
use rdf_types::BlankIdVocabulary;
use std::fmt;

#[derive(Debug)]
pub enum Warning<B> {
	MalformedIri(String),
	EmptyTerm,
	BlankNodeIdProperty(B),
	MalformedLanguageTag(String, langtag::Error),
}

impl<B> From<MalformedIri> for Warning<B> {
	fn from(MalformedIri(s): MalformedIri) -> Self {
		Self::MalformedIri(s)
	}
}

impl<B: fmt::Display> fmt::Display for Warning<B> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::MalformedIri(s) => write!(f, "malformed IRI `{}`", s),
			Self::EmptyTerm => write!(f, "empty term"),
			Self::BlankNodeIdProperty(b) => {
				write!(f, "blank node identifier `{}` used as property", b)
			}
			Self::MalformedLanguageTag(t, e) => write!(f, "invalid language tag `{}`: {}", t, e),
		}
	}
}

impl<B, N: BlankIdVocabulary<BlankId = B>> DisplayWithContext<N> for Warning<B> {
	fn fmt_with(&self, vocabulary: &N, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::MalformedIri(s) => write!(f, "malformed IRI `{}`", s),
			Self::EmptyTerm => write!(f, "empty term"),
			Self::BlankNodeIdProperty(b) => {
				write!(
					f,
					"blank node identifier `{}` used as property",
					vocabulary.blank_id(b).unwrap()
				)
			}
			Self::MalformedLanguageTag(t, e) => write!(f, "invalid language tag `{}`: {}", t, e),
		}
	}
}