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