1use crate::{PrefixMap, PrefixMapError};
2use iri_s::{IriS, IriSError};
3use thiserror::Error;
4
5use crate::Underef;
6
7#[derive(Debug, Error)]
8pub enum DerefError {
9 #[error(transparent)]
10 IriSError(#[from] IriSError),
11
12 #[error(transparent)]
13 PrefixMapError(#[from] PrefixMapError),
14
15 #[error("No prefix map to dereference prefixed name {prefix}{local}")]
16 NoPrefixMapPrefixedName { prefix: String, local: String },
17
18 #[error(transparent)]
19 UnderefError(#[from] Underef),
20}
21
22pub trait Deref {
23 fn deref(&self, base: &Option<IriS>, prefixmap: &Option<PrefixMap>) -> Result<Self, DerefError>
24 where
25 Self: Sized;
26
27 fn deref_opt<T>(
28 maybe: &Option<T>,
29 base: &Option<IriS>,
30 prefixmap: &Option<PrefixMap>,
31 ) -> Result<Option<T>, DerefError>
32 where
33 T: Deref,
34 {
35 maybe.as_ref().map(|t| t.deref(base, prefixmap)).transpose()
36 }
37
38 fn deref_opt_box<T>(
39 maybe: &Option<Box<T>>,
40 base: &Option<IriS>,
41 prefixmap: &Option<PrefixMap>,
42 ) -> Result<Option<Box<T>>, DerefError>
43 where
44 T: Deref,
45 {
46 maybe
47 .as_ref()
48 .map(|t| t.deref(base, prefixmap))
49 .transpose()
50 .map(|t| t.map(|t| Box::new(t)))
51 }
52
53 fn deref_vec<T>(
54 ts: &[T],
55 base: &Option<IriS>,
56 prefixmap: &Option<PrefixMap>,
57 ) -> Result<Vec<T>, DerefError>
58 where
59 T: Deref,
60 {
61 ts.iter().map(|t| t.deref(base, prefixmap)).collect()
62 }
63
64 fn deref_vec_box<T>(
65 ts: &[Box<T>],
66 base: &Option<IriS>,
67 prefixmap: &Option<PrefixMap>,
68 ) -> Result<Vec<T>, DerefError>
69 where
70 T: Deref,
71 {
72 ts.iter().map(|t| t.deref(base, prefixmap)).collect()
73 }
74
75 fn deref_opt_vec<T>(
76 maybe_ts: &Option<Vec<T>>,
77 base: &Option<IriS>,
78 prefixmap: &Option<PrefixMap>,
79 ) -> Result<Option<Vec<T>>, DerefError>
80 where
81 T: Deref,
82 {
83 maybe_ts
84 .as_ref()
85 .map(|ts| ts.iter().map(|t| t.deref(base, prefixmap)).collect())
86 .transpose()
87 }
88}