prefixmap/
deref.rs

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::{PrefixMap, PrefixMapError};
use iri_s::{IriS, IriSError};
use thiserror::Error;

use crate::Underef;

#[derive(Debug, Error)]
pub enum DerefError {
    #[error(transparent)]
    IriSError(#[from] IriSError),

    #[error(transparent)]
    PrefixMapError(#[from] PrefixMapError),

    #[error("No prefix map to dereference prefixed name {prefix}{local}")]
    NoPrefixMapPrefixedName { prefix: String, local: String },

    #[error(transparent)]
    UnderefError(#[from] Underef),
}

pub trait Deref {
    fn deref(&self, base: &Option<IriS>, prefixmap: &Option<PrefixMap>) -> Result<Self, DerefError>
    where
        Self: Sized;

    fn deref_opt<T>(
        maybe: &Option<T>,
        base: &Option<IriS>,
        prefixmap: &Option<PrefixMap>,
    ) -> Result<Option<T>, DerefError>
    where
        T: Deref,
    {
        maybe.as_ref().map(|t| t.deref(base, prefixmap)).transpose()
    }

    fn deref_opt_box<T>(
        maybe: &Option<Box<T>>,
        base: &Option<IriS>,
        prefixmap: &Option<PrefixMap>,
    ) -> Result<Option<Box<T>>, DerefError>
    where
        T: Deref,
    {
        maybe
            .as_ref()
            .map(|t| t.deref(base, prefixmap))
            .transpose()
            .map(|t| t.map(|t| Box::new(t)))
    }

    fn deref_vec<T>(
        ts: &[T],
        base: &Option<IriS>,
        prefixmap: &Option<PrefixMap>,
    ) -> Result<Vec<T>, DerefError>
    where
        T: Deref,
    {
        ts.iter().map(|t| t.deref(base, prefixmap)).collect()
    }

    fn deref_vec_box<T>(
        ts: &[Box<T>],
        base: &Option<IriS>,
        prefixmap: &Option<PrefixMap>,
    ) -> Result<Vec<T>, DerefError>
    where
        T: Deref,
    {
        ts.iter().map(|t| t.deref(base, prefixmap)).collect()
    }

    fn deref_opt_vec<T>(
        maybe_ts: &Option<Vec<T>>,
        base: &Option<IriS>,
        prefixmap: &Option<PrefixMap>,
    ) -> Result<Option<Vec<T>>, DerefError>
    where
        T: Deref,
    {
        maybe_ts
            .as_ref()
            .map(|ts| ts.iter().map(|t| t.deref(base, prefixmap)).collect())
            .transpose()
    }
}