blob: b2f498da0f33e7864b70d8fa6efaf7ccac0b6192 [file] [log] [blame]
David Tolnay42f50292016-09-04 13:54:21 -07001use std::fmt::{self, Display};
David Tolnay42f50292016-09-04 13:54:21 -07002
3#[derive(Debug, Clone, Eq, PartialEq)]
4pub struct Ident(String);
5
6impl Ident {
7 pub fn new<T: Into<Ident>>(t: T) -> Self {
8 t.into()
9 }
10}
11
12impl<'a> From<&'a str> for Ident {
13 fn from(s: &str) -> Self {
14 Ident(s.to_owned())
15 }
16}
17
18impl From<String> for Ident {
19 fn from(s: String) -> Self {
20 Ident(s)
21 }
22}
23
David Tolnay26469072016-09-04 13:59:48 -070024impl AsRef<str> for Ident {
25 fn as_ref(&self) -> &str {
David Tolnay42f50292016-09-04 13:54:21 -070026 &self.0
27 }
28}
29
30impl Display for Ident {
31 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
32 self.0.fmt(formatter)
33 }
34}
David Tolnayb79ee962016-09-04 09:39:20 -070035
36#[derive(Debug, Clone, Eq, PartialEq)]
37pub enum Visibility {
38 Public,
39 Inherited,
40}
41
David Tolnay86eca752016-09-04 11:26:41 -070042#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -070043pub 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 Tolnay42f50292016-09-04 13:54:21 -070052 map!(take_while1_s!(ident_ch), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -070053 ));
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 Tolnayb79ee962016-09-04 09:39:20 -070063}
David Tolnay26469072016-09-04 13:59:48 -070064
65#[cfg(feature = "printing")]
66mod 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}