json_ld_core/
warning.rs

1use contextual::{DisplayWithContext, WithContext};
2
3/// Warning handler.
4///
5/// This trait is implemented by the unit type `()` which ignores warnings.
6/// You can use [`Print`] or [`PrintWith`] to print warnings on the standard
7/// output or implement your own handler.
8pub trait Handler<N, W> {
9	/// Handle a warning with the given `vocabulary`.
10	fn handle(&mut self, vocabulary: &N, warning: W);
11}
12
13impl<N, W> Handler<N, W> for () {
14	fn handle(&mut self, _vocabulary: &N, _warning: W) {}
15}
16
17impl<'a, N, W, H: Handler<N, W>> Handler<N, W> for &'a mut H {
18	fn handle(&mut self, vocabulary: &N, warning: W) {
19		H::handle(*self, vocabulary, warning)
20	}
21}
22
23/// Prints warnings that can be displayed without vocabulary on the standard
24/// output.
25pub struct Print;
26
27impl<N, W: std::fmt::Display> Handler<N, W> for Print {
28	fn handle(&mut self, _vocabulary: &N, warning: W) {
29		eprintln!("{warning}")
30	}
31}
32
33/// Prints warnings with a given vocabulary on the standard output.
34pub struct PrintWith;
35
36impl<N, W: DisplayWithContext<N>> Handler<N, W> for PrintWith {
37	fn handle(&mut self, vocabulary: &N, warning: W) {
38		eprintln!("{}", warning.with(vocabulary))
39	}
40}