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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::{convert::TryFrom, fmt};

use serde::{Deserialize, Serialize};

use crate::{
    FactQualifier, GedcomxError, Id, NamePartQualifier, PlaceDescription, SourceDescription,
    SourceReferenceQualifier,
};

/// Specified by [RFC 3986](https://tools.ietf.org/html/rfc3986).
///
/// GEDCOM X resources use the URI to reference other entities.
/// For example, a GEDCOM X Relationship identifies a person in the relationship
/// by referencing a URI that identifies the person. When a property (such as
/// the person1 property of Relationship) is of data type URI, the value of the
/// property is interpreted as a "URI Reference" as defined by [RFC 3986, section 4](https://tools.ietf.org/html/rfc3986#section-4).
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Default)]
pub struct Uri(String);

impl_characters_yaserialize_yadeserialize!(Uri, "Uri");

impl From<&str> for Uri {
    fn from(s: &str) -> Self {
        Self(s.to_owned())
    }
}

impl From<String> for Uri {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<Id> for Uri {
    fn from(id: Id) -> Self {
        Self(format!("#{}", id.to_string()))
    }
}

impl From<&Id> for Uri {
    fn from(id: &Id) -> Self {
        Self(format!("#{}", id.to_string()))
    }
}

impl TryFrom<&PlaceDescription> for Uri {
    type Error = GedcomxError;

    fn try_from(pd: &PlaceDescription) -> Result<Self, Self::Error> {
        match &pd.id {
            Some(id) => Ok(id.into()),
            None => Err(GedcomxError::no_id_error(&pd)),
        }
    }
}

impl TryFrom<&SourceDescription> for Uri {
    type Error = GedcomxError;

    fn try_from(sd: &SourceDescription) -> Result<Self, Self::Error> {
        match &sd.id {
            Some(id) => Ok(id.into()),
            None => Err(GedcomxError::no_id_error(&sd)),
        }
    }
}

impl From<SourceReferenceQualifier> for Uri {
    fn from(s: SourceReferenceQualifier) -> Self {
        s.to_string().into()
    }
}

impl From<NamePartQualifier> for Uri {
    fn from(n: NamePartQualifier) -> Self {
        n.to_string().into()
    }
}

impl From<FactQualifier> for Uri {
    fn from(f: FactQualifier) -> Self {
        f.to_string().into()
    }
}

impl fmt::Display for Uri {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        self.0.fmt(f)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::SourceCitation;

    #[test]
    fn from_source_description() {
        let source_description =
            SourceDescription::builder(SourceCitation::new("source_citation", None))
                .id("test")
                .build();
        let uri = Uri::try_from(&source_description);
        assert_eq!(uri.unwrap(), Uri::from("#test"))
    }

    #[test]
    fn from_source_description_no_id() {
        let source_description =
            SourceDescription::builder(SourceCitation::new("source_citation", None)).build();
        let uri = Uri::try_from(&source_description);
        assert_eq!(
            uri.unwrap_err().to_string(),
            GedcomxError::no_id_error(&source_description).to_string()
        )
    }

    #[test]
    fn from_place_description() {
        let place_description = PlaceDescription::builder("name").id("test").build();
        let uri = Uri::try_from(&place_description);
        assert_eq!(uri.unwrap(), Uri::from("#test"))
    }

    #[test]
    fn from_place_description_no_id() {
        let place_description = PlaceDescription::builder("name").build();
        let uri = Uri::try_from(&place_description);
        assert_eq!(
            uri.unwrap_err().to_string(),
            GedcomxError::no_id_error(&place_description).to_string()
        )
    }

    #[test]
    fn from_id() {
        let id = Id::from("hi");
        let id2 = id.clone();
        let uri: Uri = id.into();
        assert_eq!(uri, Uri("#hi".to_string()));

        let uri2: Uri = (&id2).into();
        assert_eq!(uri2, Uri("#hi".to_string()));
    }
}