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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use std::cmp::Ordering;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::fmt::Write;

use opaque_typedef::OpaqueTypedefUnsized;
use pest::iterators::Pair;

use crate::share::Share;
use crate::share::Cow;
use crate::share::Redeem;
use crate::error::Error;
use crate::error::Result;
use crate::parser::FromPair;
use crate::parser::Rule;
use crate::parser::QuickFind;


fn escape<W: Write>(f: &mut W, s: &str) -> FmtResult {
    s.chars().try_for_each(|char| match char {
        '\r' => f.write_str("\\r"),
        '\n' => f.write_str("\\n"),
        '\u{000c}' => f.write_str("\\f"),
        ' ' => f.write_str("\\ "),
        '\t' => f.write_str("\\t"),
        ':' => f.write_str("\\:"),
        '"' => f.write_str("\\\""),
        '\\' => f.write_str("\\\\"),
        _ => f.write_char(char)
    })
}

fn unescape<W: Write>(f: &mut W, s: &str) -> FmtResult {
    let mut chars = s.chars();
    while let Some(char) = chars.next() {
        if char == '\\' {
            match chars.next() {
                Some('r') => f.write_char('\r')?,
                Some('n') => f.write_char('\n')?,
                Some('f') => f.write_char('\u{000c}')?,
                Some('t') => f.write_char('\t')?,
                Some(other) => f.write_char(other)?,
                None => panic!("invalid escape"), // FIXME(@althonos)
            }
        } else {
            f.write_char(char)?;
        }
    }
    Ok(())
}

fn is_canonical<S: AsRef<str>>(s: S) -> bool {
    let string = s.as_ref();
    let mut chars = string.chars();
    if let Some(c) = chars.next() {
        c.is_ascii_alphabetic() && chars.all(|ref c| char::is_ascii_alphanumeric(c))
    } else {
        false
    }
}


/// An identifier prefix, either canonical or non-canonical.
///
/// * A canonical ID prefix only contains alphabetic characters (`[a-zA-Z]`)
///   followed by either an underscore or other alphabetic characters.
/// * A non-canonical ID prefix can contain any character besides `:`.
///
/// # Example
/// ```rust
/// # extern crate fastobo;
/// # use fastobo::ast::PrefixedIdent;
/// # use std::str::FromStr;
/// let id = PrefixedIdent::from_str("GO:0046154").unwrap();
/// assert!(id.prefix().is_canonical());
/// assert_eq!(id.prefix(), "GO");
/// ```
#[derive(Clone, Debug, Eq, Hash, Ord)]
pub struct IdentPrefix {
    value: String,
    canonical: bool,
}

impl IdentPrefix {
    /// Create a new identifier prefix.
    pub fn new<S>(prefix: S) -> Self
    where
        S: Into<String>
    {
        let value = prefix.into();
        Self {
            canonical: is_canonical(&value),
            value,
        }
    }

    /// Check if the identifier prefix is canonical or not.
    ///
    /// # Example
    /// ```rust
    /// # extern crate fastobo;
    /// # use fastobo::ast::IdentPrefix;
    /// assert!(IdentPrefix::new(String::from("GO")).is_canonical());
    /// ```
    pub fn is_canonical(&self) -> bool {
        self.canonical
    }

    /// Create a new `IdPrefix` without checking if it is canonical or not.
    ///
    /// This is unsafe because the `canonical` flag will be used to determine
    /// if the prefix needs escaping. If not set right, the syntax of the
    /// produced serialization could be invalid.
    pub unsafe fn new_unchecked(s: String, canonical: bool) -> Self {
        Self { canonical, value: s }
    }

    /// Get the prefix as a string slice.
    pub fn as_str(&self) -> &str {
        &self.value
    }

    /// Extract the unescaped prefix as a `String`.
    pub fn into_string(self) -> String {
        self.value
    }
}

impl AsRef<str> for IdentPrefix {
    fn as_ref(&self) -> &str {
        &self.value
    }
}

impl Display for IdentPrefix {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        self.share().fmt(f)
    }
}

impl From<IdentPrefix> for String {
    fn from(prefix: IdentPrefix) -> String {
        prefix.value
    }
}

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

impl From<&str> for IdentPrefix {
    fn from(s: &str) -> Self {
        Self::new(s)
    }
}

impl<'i> FromPair<'i> for IdentPrefix {
    const RULE: Rule = Rule::IdPrefix;
    unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
        // Bail out if the local prefix is canonical (alphanumeric only)
        let inner = pair.into_inner().next().unwrap();
        if inner.as_rule() == Rule::CanonicalIdPrefix {
            return Ok(Self::new_unchecked(inner.as_str().to_string(), true));
        }

        // Unescape the prefix if it was not produced by CanonicalIdPrefix.
        let s = inner.as_str();
        let escaped = s.quickcount(b'\\');
        let mut local = String::with_capacity(s.len() + escaped);
        unescape(&mut local, s).expect("fmt::Write cannot fail on a String");
        // FIXME(@althonos): possible syntax issue, which uses a non-canonical
        //                   rule on canonical prefixes (workaround is to check
        //                   one more time if the prefix is canonical)
        Ok(Self::new(local))
    }
}
impl_fromstr!(IdentPrefix);

impl PartialEq for IdentPrefix {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl PartialEq<str> for IdentPrefix {
    fn eq(&self, other: &str) -> bool {
        self.value == other
    }
}

impl PartialOrd for IdentPrefix {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.value.partial_cmp(&other.value)
    }
}

impl<'a> Share<'a, IdPrefix<'a>> for IdentPrefix {
    fn share(&'a self) -> IdPrefix<'a> {
        unsafe { IdPrefix::new_unchecked(&self.value, self.canonical) }
    }
}


/// A borrowed `IdentPrefix`
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq)]
pub struct IdPrefix<'a> {
    value: &'a str,
    canonical: bool,
}

impl<'a> IdPrefix<'a> {
    /// Create a new `IdPrf` from a borrowed string slice.
    pub fn new(s: &'a str) -> Self {
        Self {
            canonical: is_canonical(s),
            value: s,
        }
    }

    pub fn as_str(&self) -> &'a str {
        self.value
    }

    /// Create a new `IdPrf` without checking if it is canonical or not.
    pub unsafe fn new_unchecked(s: &'a str, canonical: bool) -> Self {
        Self { value: s, canonical }
    }
}

impl<'a> AsRef<str> for IdPrefix<'a> {
    fn as_ref(&self) -> &str {
        &self.value
    }
}

impl<'a> Display for IdPrefix<'a> {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        if self.canonical {
            f.write_str(&self.value)
        } else {
            escape(f, &self.value)
        }
    }
}

impl<'a> Into<&'a str> for IdPrefix<'a> {
    fn into(self) -> &'a str {
        self.value
    }
}

impl<'i> FromPair<'i> for Cow<'i, IdPrefix<'i>> {
    const RULE: Rule = Rule::IdPrefix;
    unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
        let inner = pair.into_inner().next().unwrap();
        if inner.as_rule() == Rule::CanonicalIdPrefix {
            Ok(Cow::Borrowed(IdPrefix::new_unchecked(inner.as_str(), true)))
        } else if inner.as_str().find('\\').is_some() {
            IdentPrefix::from_pair_unchecked(inner).map(Cow::Owned)
        } else {
            Ok(Cow::Borrowed(IdPrefix::new_unchecked(inner.as_str(), false)))
        }
    }
}
impl_fromslice!('i, Cow<'i, IdPrefix<'i>>);

impl<'a> PartialEq<str> for IdPrefix<'a> {
    fn eq(&self, other: &str) -> bool {
        self.value == other
    }
}

impl<'a> PartialOrd for IdPrefix<'a> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.value.partial_cmp(&other.value)
    }
}

impl<'a> Redeem<'a> for IdPrefix<'a> {
    type Owned = IdentPrefix;
    fn redeem(&self) -> IdentPrefix {
        unsafe {
            IdentPrefix::new_unchecked(self.value.to_string(), self.canonical)
        }
    }
}


#[cfg(test)]
mod tests {

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

    #[test]
    fn is_canonical() {
        assert!(IdentPrefix::from_str("GO").unwrap().is_canonical());
        assert!(IdentPrefix::new("GO").is_canonical());

        assert!(!IdentPrefix::from_str("n°t").unwrap().is_canonical());
        assert!(!IdentPrefix::new("n°t").is_canonical());
    }

    #[test]
    fn from_str() {
        let prefix = IdentPrefix::from_str("GO").unwrap();
        self::assert_eq!(prefix, IdentPrefix::new(String::from("GO")));
        assert!(prefix.is_canonical());
        assert!(IdentPrefix::from_str("GO:").is_err());
    }

    #[test]
    fn to_string() {
        let prefix = IdentPrefix::new(String::from("GO"));
        self::assert_eq!(prefix.to_string(), "GO");

        let prefix = IdentPrefix::new(String::from("some thing"));
        self::assert_eq!(prefix.to_string(), "some\\ thing");
    }
}