David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame^] | 1 | use std::fmt::{self, Display}; |
| 2 | use std::ops::Deref; |
| 3 | |
| 4 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 5 | pub struct Ident(String); |
| 6 | |
| 7 | impl Ident { |
| 8 | pub fn new<T: Into<Ident>>(t: T) -> Self { |
| 9 | t.into() |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | impl<'a> From<&'a str> for Ident { |
| 14 | fn from(s: &str) -> Self { |
| 15 | Ident(s.to_owned()) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | impl From<String> for Ident { |
| 20 | fn from(s: String) -> Self { |
| 21 | Ident(s) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl Deref for Ident { |
| 26 | type Target = str; |
| 27 | |
| 28 | fn deref(&self) -> &str { |
| 29 | &self.0 |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl Display for Ident { |
| 34 | fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 35 | self.0.fmt(formatter) |
| 36 | } |
| 37 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 38 | |
| 39 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 40 | pub enum Visibility { |
| 41 | Public, |
| 42 | Inherited, |
| 43 | } |
| 44 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 45 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 46 | pub 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 Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame^] | 55 | map!(take_while1_s!(ident_ch), Into::into) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 56 | )); |
| 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 Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 66 | } |