shex_ast/ast/
triple_expr_label.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
use iri_s::IriSError;
use serde_derive::{Deserialize, Serialize};
use std::fmt::Display;

use prefixmap::{Deref, DerefError, IriRef};

use super::bnode::BNode;

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Hash, Clone)]
#[serde(try_from = "&str", into = "String")]
pub enum TripleExprLabel {
    IriRef { value: IriRef },
    BNode { value: BNode },
}

impl Deref for TripleExprLabel {
    fn deref(
        &self,
        base: &Option<iri_s::IriS>,
        prefixmap: &Option<prefixmap::PrefixMap>,
    ) -> Result<Self, DerefError>
    where
        Self: Sized,
    {
        match self {
            TripleExprLabel::IriRef { value } => {
                let new_value = value.deref(base, prefixmap)?;
                Ok(TripleExprLabel::IriRef { value: new_value })
            }
            TripleExprLabel::BNode { value } => Ok(TripleExprLabel::BNode {
                value: value.clone(),
            }),
        }
    }
}

impl TryFrom<&str> for TripleExprLabel {
    type Error = IriSError;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        let iri_ref = IriRef::try_from(s)?;
        Ok(TripleExprLabel::IriRef { value: iri_ref })
    }
}

impl Display for TripleExprLabel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let str = match self {
            TripleExprLabel::IriRef { value } => value.to_string(),
            TripleExprLabel::BNode { value } => value.to_string(),
        };
        write!(f, "{}", str)
    }
}

impl From<TripleExprLabel> for String {
    fn from(val: TripleExprLabel) -> Self {
        val.to_string()
    }
}