blob: 9c5de75a5b1513943872ed9bf2b19dd8e628fbb9 [file] [log] [blame]
David Tolnay42f50292016-09-04 13:54:21 -07001use std::fmt::{self, Display};
2use std::ops::Deref;
3
4#[derive(Debug, Clone, Eq, PartialEq)]
5pub struct Ident(String);
6
7impl Ident {
8 pub fn new<T: Into<Ident>>(t: T) -> Self {
9 t.into()
10 }
11}
12
13impl<'a> From<&'a str> for Ident {
14 fn from(s: &str) -> Self {
15 Ident(s.to_owned())
16 }
17}
18
19impl From<String> for Ident {
20 fn from(s: String) -> Self {
21 Ident(s)
22 }
23}
24
25impl Deref for Ident {
26 type Target = str;
27
28 fn deref(&self) -> &str {
29 &self.0
30 }
31}
32
33impl Display for Ident {
34 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
35 self.0.fmt(formatter)
36 }
37}
David Tolnayb79ee962016-09-04 09:39:20 -070038
39#[derive(Debug, Clone, Eq, PartialEq)]
40pub enum Visibility {
41 Public,
42 Inherited,
43}
44
David Tolnay86eca752016-09-04 11:26:41 -070045#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -070046pub mod parsing {
47 use super::*;
48
49 fn ident_ch(ch: char) -> bool {
50 ch.is_alphanumeric() || ch == '_'
51 }
52
53 named!(pub word<&str, Ident>, preceded!(
54 opt!(call!(::nom::multispace)),
David Tolnay42f50292016-09-04 13:54:21 -070055 map!(take_while1_s!(ident_ch), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -070056 ));
57
58 named!(pub visibility<&str, Visibility>, preceded!(
59 opt!(call!(::nom::multispace)),
60 alt!(
61 terminated!(tag_s!("pub"), call!(::nom::multispace)) => { |_| Visibility::Public }
62 |
63 epsilon!() => { |_| Visibility::Inherited }
64 )
65 ));
David Tolnayb79ee962016-09-04 09:39:20 -070066}