blob: f2db35c32870274f863de3a3b05f8e7d573c3a48 [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
David Tolnayd5025812016-09-04 14:21:46 -070036#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayb79ee962016-09-04 09:39:20 -070037pub 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::*;
David Tolnayf6ccb832016-09-04 15:00:56 -070045 use nom::multispace;
David Tolnay9d8f1972016-09-04 11:58:48 -070046
47 fn ident_ch(ch: char) -> bool {
48 ch.is_alphanumeric() || ch == '_'
49 }
50
51 named!(pub word<&str, Ident>, preceded!(
David Tolnayf6ccb832016-09-04 15:00:56 -070052 option!(multispace),
David Tolnay42f50292016-09-04 13:54:21 -070053 map!(take_while1_s!(ident_ch), Into::into)
David Tolnay9d8f1972016-09-04 11:58:48 -070054 ));
55
David Tolnayf6ccb832016-09-04 15:00:56 -070056 named!(pub visibility<&str, Visibility>, alt_complete!(
57 do_parse!(
58 punct!("pub") >>
59 multispace >>
60 (Visibility::Public)
David Tolnay9d8f1972016-09-04 11:58:48 -070061 )
David Tolnayf6ccb832016-09-04 15:00:56 -070062 |
63 epsilon!() => { |_| Visibility::Inherited }
David Tolnay9d8f1972016-09-04 11:58:48 -070064 ));
David Tolnayb79ee962016-09-04 09:39:20 -070065}
David Tolnay26469072016-09-04 13:59:48 -070066
67#[cfg(feature = "printing")]
68mod 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}