nessie_lex/
lib.rs

1pub mod range;
2pub mod traits;
3mod token;
4mod lex;
5
6/// A derive macro that defines an enum as convertible to and from strings.
7///
8/// # Example
9/// ```
10/// use nessie_lex::Strings;
11/// use std::str::FromStr;
12///
13/// #[derive(Strings, Debug, PartialEq, Eq)]
14/// enum Keyword {
15///    #[string("let")]
16///    Let,
17///    #[string("fn")]
18///    Fn,
19/// }
20///
21/// assert_eq!(Keyword::Let.to_string(), "let");
22/// assert_eq!(Keyword::from_str("let"), Ok(Keyword::Let));
23/// // This will fail because "if" was not defined.
24/// // Keyword::from_str("if")
25pub use nessie_lex_proc_macros::Strings;
26pub use nessie_lex_proc_macros::{Keyword, Symbol};
27pub use token::*;
28pub use lex::lex;
29
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    /// This is a hack to allow the tests to access the super module with the
36    /// name `nessie_lex` so that the derive macros can be used in the tests.
37    mod nessie_lex {
38        pub use super::super::*;
39    }
40
41    #[derive(Keyword, Debug, PartialEq, Eq)]
42    enum Keyword {
43        #[string("let")]
44        Let,
45        #[string("fn")]
46        Fn,
47    }
48
49    #[derive(Symbol, Debug, PartialEq, Eq)]
50    enum Symbol {
51        #[string("(")]
52        OpenParen,
53        #[string(")")]
54        CloseParen,
55    }
56
57    type Token<'a> = super::Token<'a, Keyword, Symbol>;
58
59    #[test]
60    fn test_keyword_to_string() {
61        assert_eq!(Keyword::Let.to_string(), "let");
62        assert_eq!(Keyword::Fn.to_string(), "fn");
63    }
64
65    #[test]
66    fn test_from_traits() {
67        let token: Token = Token::Keyword(Keyword::Let);
68        assert_eq!(token, Keyword::Let.into());
69    }
70}