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::*; |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame^] | 45 | use nom::multispace; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 46 | |
| 47 | fn ident_ch(ch: char) -> bool { |
| 48 | ch.is_alphanumeric() || ch == '_' |
| 49 | } |
| 50 | |
| 51 | named!(pub word<&str, Ident>, preceded!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame^] | 52 | option!(multispace), |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 53 | map!(take_while1_s!(ident_ch), Into::into) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 54 | )); |
| 55 | |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame^] | 56 | named!(pub visibility<&str, Visibility>, alt_complete!( |
| 57 | do_parse!( |
| 58 | punct!("pub") >> |
| 59 | multispace >> |
| 60 | (Visibility::Public) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 61 | ) |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame^] | 62 | | |
| 63 | epsilon!() => { |_| Visibility::Inherited } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 64 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 65 | } |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 66 | |
| 67 | #[cfg(feature = "printing")] |
| 68 | mod printing { |
| 69 | use super::*; |
| 70 | use quote::{Tokens, ToTokens}; |
| 71 | |
| 72 | impl ToTokens for Ident { |
| 73 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 74 | tokens.append(self.as_ref()) |
| 75 | } |
| 76 | } |
| 77 | } |