David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 1 | use std::fmt::{self, Display}; |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 2 | |
| 3 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 4 | pub struct Ident(String); |
| 5 | |
| 6 | impl Ident { |
| 7 | pub fn new<T: Into<Ident>>(t: T) -> Self { |
| 8 | t.into() |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | impl<'a> From<&'a str> for Ident { |
| 13 | fn from(s: &str) -> Self { |
| 14 | Ident(s.to_owned()) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | impl From<String> for Ident { |
| 19 | fn from(s: String) -> Self { |
| 20 | Ident(s) |
| 21 | } |
| 22 | } |
| 23 | |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 24 | impl AsRef<str> for Ident { |
| 25 | fn as_ref(&self) -> &str { |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 26 | &self.0 |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl Display for Ident { |
| 31 | fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 32 | self.0.fmt(formatter) |
| 33 | } |
| 34 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 35 | |
David Tolnay | d502581 | 2016-09-04 14:21:46 -0700 | [diff] [blame] | 36 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 37 | pub enum Visibility { |
| 38 | Public, |
| 39 | Inherited, |
| 40 | } |
| 41 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 42 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 43 | pub mod parsing { |
| 44 | use super::*; |
| 45 | |
| 46 | fn ident_ch(ch: char) -> bool { |
| 47 | ch.is_alphanumeric() || ch == '_' |
| 48 | } |
| 49 | |
| 50 | named!(pub word<&str, Ident>, preceded!( |
| 51 | opt!(call!(::nom::multispace)), |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 52 | map!(take_while1_s!(ident_ch), Into::into) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 53 | )); |
| 54 | |
| 55 | named!(pub visibility<&str, Visibility>, preceded!( |
| 56 | opt!(call!(::nom::multispace)), |
| 57 | alt!( |
| 58 | terminated!(tag_s!("pub"), call!(::nom::multispace)) => { |_| Visibility::Public } |
| 59 | | |
| 60 | epsilon!() => { |_| Visibility::Inherited } |
| 61 | ) |
| 62 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 63 | } |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 64 | |
| 65 | #[cfg(feature = "printing")] |
| 66 | mod printing { |
| 67 | use super::*; |
| 68 | use quote::{Tokens, ToTokens}; |
| 69 | |
| 70 | impl ToTokens for Ident { |
| 71 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 72 | tokens.append(self.as_ref()) |
| 73 | } |
| 74 | } |
| 75 | } |