Skip to main content

ekg_identifier/iri/
t_box_base.rs

1use {
2    crate::{ABoxNamespaceIRI, iri::NamespaceIRI},
3    serde::{Deserialize, Serialize},
4    std::str::FromStr,
5};
6
7/// An `TBoxNamespaceIRI` is a namespace IRI for TBox resources in an EKG where
8/// we can accept either a `#` or `/` at the end of the IRI.
9/// See also [ABoxNamespaceIRI](crate::ABoxNamespaceIRI).
10#[derive(Serialize, Deserialize, Clone, Debug)]
11pub struct TBoxNamespaceIRI(
12    #[serde(deserialize_with = "ekg_util::serde_util::deserialize_uri")]
13    #[serde(serialize_with = "ekg_util::serde_util::serialize_base_uri")]
14    pub iri_string::types::IriReferenceString,
15);
16
17impl TBoxNamespaceIRI {
18    #[inline]
19    pub fn as_str(&self) -> &str { self.0.as_str() }
20
21    // noinspection DuplicatedCode
22    pub fn as_base_iri(&self) -> String {
23        let str = self.as_str();
24        let last_char = str.chars().last().unwrap();
25        if last_char == '/' || last_char == '#' {
26            str.to_string()
27        } else {
28            format!("{str}/")
29        }
30    }
31}
32
33impl NamespaceIRI for TBoxNamespaceIRI {
34    fn as_str(&self) -> &str { self.0.as_str() }
35}
36
37impl std::fmt::Display for TBoxNamespaceIRI {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) }
39}
40
41impl From<iri_string::types::IriReferenceString> for TBoxNamespaceIRI {
42    fn from(uri: iri_string::types::IriReferenceString) -> Self { Self(uri) }
43}
44
45impl std::str::FromStr for TBoxNamespaceIRI {
46    type Err = ekg_error::Error;
47
48    fn from_str(uri_str: &str) -> Result<Self, Self::Err> {
49        Ok(Self(
50            iri_string::types::IriReferenceString::try_from(uri_str.to_owned())?,
51        ))
52    }
53}
54
55impl TryFrom<&str> for TBoxNamespaceIRI {
56    type Error = ekg_error::Error;
57
58    fn try_from(iri_str: &str) -> Result<Self, Self::Error> { Self::from_str(iri_str) }
59}
60
61impl TryFrom<String> for TBoxNamespaceIRI {
62    type Error = ekg_error::Error;
63
64    fn try_from(iri_str: String) -> Result<Self, Self::Error> {
65        Ok(Self(
66            iri_string::types::IriReferenceString::try_from(iri_str)?,
67        ))
68    }
69}
70
71impl TryFrom<&iri_string::types::IriReferenceStr> for TBoxNamespaceIRI {
72    type Error = ekg_error::Error;
73
74    fn try_from(iri: &iri_string::types::IriReferenceStr) -> Result<Self, Self::Error> {
75        iri.to_string().try_into()
76    }
77}
78
79impl TryFrom<&iref::Iri> for TBoxNamespaceIRI {
80    type Error = ekg_error::Error;
81
82    fn try_from(iri: &iref::Iri) -> Result<Self, Self::Error> {
83        Ok(Self(
84            iri_string::types::IriReferenceString::try_from(iri.as_str())?.to_owned(),
85        ))
86    }
87}
88
89impl TryFrom<ABoxNamespaceIRI> for TBoxNamespaceIRI {
90    type Error = ekg_error::Error;
91
92    fn try_from(iri: ABoxNamespaceIRI) -> Result<Self, Self::Error> {
93        TBoxNamespaceIRI::from_str(iri.as_str())
94    }
95}