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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::fmt::Write;

use pest::iterators::Pair;
use url::Url;

use crate::ast::*;
use crate::error::Error;
use crate::error::Result;
use crate::parser::FromPair;
use crate::parser::Rule;

/// A synonym scope specifier.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Ord, PartialOrd)]
pub enum SynonymScope {
    Broad,
    Exact,
    Narrow,
    Related,
}

impl Display for SynonymScope {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        use self::SynonymScope::*;
        match self {
            Exact => f.write_str("EXACT"),
            Broad => f.write_str("BROAD"),
            Narrow => f.write_str("NARROW"),
            Related => f.write_str("RELATED"),
        }
    }
}

impl<'i> FromPair<'i> for SynonymScope {
    const RULE: Rule = Rule::SynonymScope;
    unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
        match pair.as_str() {
            "EXACT" => Ok(SynonymScope::Exact),
            "BROAD" => Ok(SynonymScope::Broad),
            "NARROW" => Ok(SynonymScope::Narrow),
            "RELATED" => Ok(SynonymScope::Related),
            _ => unreachable!(),
        }
    }
}
impl_fromstr!(SynonymScope);

/// A synonym, denoting an alternative name for the embedding entity.
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Synonym {
    desc: QuotedString,
    scope: SynonymScope,
    ty: Option<SynonymTypeIdent>,
    xrefs: XrefList,
}

impl Synonym {
    /// Create a `Synonym` with the given description and scope.
    pub fn new<D>(desc: D, scope: SynonymScope) -> Self
    where
        D: Into<QuotedString>,
    {
        Self {
            desc: desc.into(),
            scope,
            ty: Default::default(),
            xrefs: Default::default(),
        }
    }

    /// Create a `Synonym` with the given description, scope, and type.
    pub fn with_type<D, T>(desc: D, scope: SynonymScope, ty: T) -> Self
    where
        D: Into<QuotedString>,
        T: Into<Option<SynonymTypeIdent>>,
    {
        Self {
            desc: desc.into(),
            scope,
            ty: ty.into(),
            xrefs: Default::default(),
        }
    }

    /// Create a `Synonym` with the given description, scope, and xrefs.
    pub fn with_xrefs<D, L>(desc: D, scope: SynonymScope, xrefs: L) -> Self
    where
        D: Into<QuotedString>,
        L: Into<XrefList>,
    {
        Self {
            desc: desc.into(),
            scope,
            ty: None,
            xrefs: xrefs.into(),
        }
    }

    /// Create a `Synonym` with the given description, scope, type, and xrefs.
    pub fn with_type_and_xrefs<D, T, L>(
        desc: D,
        scope: SynonymScope,
        ty: T,
        xrefs: L,
    ) -> Self
    where
        D: Into<QuotedString>,
        T: Into<Option<SynonymTypeIdent>>,
        L: Into<XrefList>,
    {
        Self {
            desc: desc.into(),
            scope,
            ty: ty.into(),
            xrefs: xrefs.into(),
        }
    }
}

impl Synonym {
    /// Get a reference to the description of the `Synonym`.
    pub fn description(&self) -> &QuotedString {
        &self.desc
    }

    /// Get a mutable reference to the description of the `Synonym`.
    pub fn description_mut(&mut self) -> &mut QuotedString {
        &mut self.desc
    }

    /// Get a reference to the scope of the `Synonym`.
    pub fn scope(&self) -> &SynonymScope {
        &self.scope
    }

    /// Get a mutable reference to the scope of the `Synonym`.
    pub fn scope_mut(&mut self) -> &mut SynonymScope {
        &mut self.scope
    }

    /// Get a reference to the type of the `Synonym`, if any.
    pub fn ty(&self) -> Option<&SynonymTypeIdent> {
        match self.ty {
            Some(ref id) => Some(id),
            None => None,
        }
    }

    /// Get a mutable reference to the type of the `Synonym`, if any.
    pub fn ty_mut(&mut self) -> Option<&mut SynonymTypeIdent> {
        match self.ty {
            Some(ref mut id) => Some(id),
            None => None,
        }
    }

    /// Get a reference to the xrefs of the `Synonym`.
    pub fn xrefs(&self) -> &XrefList {
        &self.xrefs
    }

    /// Get a mutable reference to the xrefs of the `Synonym`.
    pub fn xrefs_mut(&mut self) -> &mut XrefList {
        &mut self.xrefs
    }
}

impl Display for Synonym {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        self.desc
            .fmt(f)
            .and(f.write_char(' '))
            .and(self.scope.fmt(f))
            .and(f.write_char(' '))?;

        if let Some(ref syntype) = self.ty {
            syntype.fmt(f).and(f.write_char(' '))?;
        }

        self.xrefs.fmt(f)
    }
}

impl<'i> FromPair<'i> for Synonym {
    const RULE: Rule = Rule::Synonym;
    unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
        let mut inner = pair.into_inner();

        let desc = QuotedString::from_pair_unchecked(inner.next().unwrap())?;
        let scope = SynonymScope::from_pair_unchecked(inner.next().unwrap())?;

        let nxt = inner.next().unwrap();
        match nxt.as_rule() {
            Rule::SynonymTypeId => {
                let ty = Some(SynonymTypeIdent::from_pair_unchecked(nxt)?);
                let xrefs = XrefList::from_pair_unchecked(inner.next().unwrap())?;
                Ok(Synonym {
                    desc,
                    scope,
                    ty,
                    xrefs,
                })
            }
            Rule::XrefList => {
                let ty = None;
                let xrefs = XrefList::from_pair_unchecked(nxt)?;
                Ok(Synonym {
                    desc,
                    scope,
                    ty,
                    xrefs,
                })
            }
            _ => unreachable!(),
        }
    }
}
impl_fromstr!(Synonym);

#[cfg(test)]
mod tests {

    use std::str::FromStr;
    use pretty_assertions::assert_eq;
    use super::*;

    mod scope {

        use super::*;
        use self::SynonymScope::*;

        #[test]
        fn from_str() {
            self::assert_eq!(SynonymScope::from_str("EXACT").unwrap(), Exact);
            self::assert_eq!(SynonymScope::from_str("BROAD").unwrap(), Broad);
            self::assert_eq!(SynonymScope::from_str("NARROW").unwrap(), Narrow);
            self::assert_eq!(SynonymScope::from_str("RELATED").unwrap(), Related);
            assert!(SynonymScope::from_str("something").is_err());
        }

        #[test]
        fn to_string() {
            self::assert_eq!(Exact.to_string(), "EXACT");
            self::assert_eq!(Broad.to_string(), "BROAD");
            self::assert_eq!(Narrow.to_string(), "NARROW");
            self::assert_eq!(Related.to_string(), "RELATED");
        }
    }

    mod synonym {

        use super::*;

        #[test]
        fn from_str() {
            let actual = Synonym::from_str("\"ssDNA-specific endodeoxyribonuclease activity\" RELATED [GOC:mah]").unwrap();
            let expected = Synonym::with_xrefs(
                "ssDNA-specific endodeoxyribonuclease activity",
                SynonymScope::Related,
                vec![Xref::new(PrefixedIdent::new("GOC", "mah"))],
            );
            self::assert_eq!(actual, expected);
        }

        #[test]
        fn to_string() {
            let s = Synonym::with_xrefs(
                QuotedString::new(String::from("ssDNA-specific endodeoxyribonuclease activity")),
                SynonymScope::Related,
                vec![Xref::new(
                    Ident::from(
                        PrefixedIdent::new(
                            IdentPrefix::new(String::from("GOC")),
                            IdentLocal::new(String::from("mah"))
                        )
                    )
                )]
            );

            self::assert_eq!(
                s.to_string(),
                "\"ssDNA-specific endodeoxyribonuclease activity\" RELATED [GOC:mah]"
            );
        }

    }


}