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
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 {
    s.as_ref().chars().all(|c| c.is_ascii_digit())
}


/// A local identifier, preceded by a prefix in prefixed IDs.
///
/// * A canonical local ID only contains digits (`[0-9]`).
/// * A non-canonical local ID can contain any character excepting
///   whitespaces and newlines.
///
/// # Example
/// ```rust
/// # extern crate fastobo;
/// # use fastobo::ast::PrefixedIdent;
/// # use std::str::FromStr;
/// let id = PrefixedIdent::from_str("GO:0046154").unwrap();
/// assert!(id.local().is_canonical());
/// assert_eq!(id.local(), "0046154");
/// ```
#[derive(Clone, Debug, Ord, Hash, Eq)]
pub struct IdentLocal {
    value: String,
    canonical: bool,
}

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

    /// Check if the local identifier is canonical or not.
    pub fn is_canonical(&self) -> bool {
        self.canonical
    }

    /// Create a new `IdLocal` without checking if it is canonical.
    pub unsafe fn new_unchecked(s: String, canonical: bool) -> Self {
        Self { value: s, canonical }
    }

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

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

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

impl From<IdentLocal> for String {
    fn from(id: IdentLocal) -> Self {
        id.value
    }
}

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

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

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

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

        // Unescape the local ID if it is non canonical.
        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 local IDs (workaround is to check
        //                   one more time if the local ID is canonical)
        Ok(Self::new(local))
    }
}
impl_fromstr!(IdentLocal);

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

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

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

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

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

impl<'a> IdLocal<'a> {
    pub fn new(s: &'a str) -> Self {
        Self {
            canonical: is_canonical(s),
            value: s,
        }
    }

    pub unsafe fn new_unchecked(s: &'a str, canonical: bool) -> Self {
        Self {
            value: s,
            canonical,
        }
    }
}

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

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

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

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

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

impl<'a> Redeem<'a> for IdLocal<'a> {
    type Owned = IdentLocal;
    fn redeem(&self) -> Self::Owned {
        unsafe {
            IdentLocal::new_unchecked(self.value.to_owned(), self.canonical)
        }
    }
}

#[cfg(test)]
mod tests {

    use std::str::FromStr;
    use std::string::ToString;

    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn from_str() {
        let local = IdentLocal::from_str("0001").unwrap();
        self::assert_eq!(local.as_ref(), "0001");
        assert!(local.is_canonical());

        let local = IdentLocal::from_str("\\0001").unwrap();
        self::assert_eq!(local.as_ref(), "0001");
        assert!(local.is_canonical());

        let local = IdentLocal::from_str("0F").unwrap();
        self::assert_eq!(local.as_ref(), "0F");
        assert!(!local.is_canonical());

        assert!(IdentLocal::from_str("ABC\nDEF").is_err());
    }

    #[test]
    fn to_string() {
        self::assert_eq!(IdentLocal::new("0001").to_string(), "0001");
        self::assert_eq!(IdentLocal::new(":001").to_string(), "\\:001");
        self::assert_eq!(IdentLocal::new("0 01").to_string(), "0\\ 01");
    }

}