1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Code generation for a compile-time trie-based mapping from strings to arbitrary values.

mod trie;

use std::collections::BTreeMap;
use std::io;

use quote::{quote, ToTokens};
use proc_macro2::{Literal, Ident, TokenStream, Span};

/// Generates a lookup function for all the key-value pairs contained in the tree.
///
/// # Examples
///
/// ```rust
/// # #![recursion_limit="128"]
/// # use quote::quote;
/// use enum_utils_from_str::StrMapFunc;
///
/// # fn main() {
/// // Compiling this trie into a lookup function...
/// let mut code = vec![];
/// StrMapFunc::new("custom_lookup", "bool")
///     .entries(vec![
///         ("yes", true),
///         ("yep", true),
///         ("no", false),
///     ])
///     .compile(&mut code);
///
/// // results in the following generated code.
///
/// # let generated = quote! {
/// fn custom_lookup(s: &[u8]) -> Option<bool> {
///     match s.len() {
///         2 => if s[0] == b'n' && s[1] == b'o' {
///             return Some(false);
///         },
///         3 => if s[0] == b'y' && s[1] == b'e' {
///             if s[2] == b'p' {
///                  return Some(true);
///             } else if s[2] == b's' {
///                 return Some(true);
///             }
///         },
///
///         _ => {}
///     }
///
///     None
/// }
/// # };
///
/// # assert_eq!(String::from_utf8(code).unwrap(), format!("{}", generated));
/// # }
/// ```
#[derive(Clone)]
pub struct StrMapFunc {
    atoms: Forest<TokenStream>,
    func_name: Ident,
    ret_ty: TokenStream,
    case: Case,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Case {
    Sensitive,
    Insensitive,
}

impl StrMapFunc {
    pub fn new(func_name: &str, ret_ty: &str) -> Self {
        StrMapFunc {
            atoms: Default::default(),
            func_name: Ident::new(func_name, Span::call_site()),
            ret_ty: ret_ty.parse().unwrap(),
            case: Case::Sensitive,
        }
    }

    pub fn case(&mut self, case: Case) -> &mut Self {
        self.case = case;
        self
    }

    pub fn entry(&mut self, k: &str, v: impl ToTokens) -> &mut Self {
        self.atoms.insert(k.as_bytes(), v.into_token_stream());
        self
    }

    pub fn entries<'a, V: 'a>(&mut self, entries: impl IntoIterator<Item = (&'a str, V)>) -> &mut Self
        where V: ToTokens,
    {
        for (s, v) in entries.into_iter() {
            self.entry(s, v);
        }

        self
    }

    pub fn compile(&self, mut w: impl io::Write) -> io::Result<()> {
        let tokens = self.into_token_stream();
        w.write_all(format!("{}", tokens).as_bytes())
    }
}

impl ToTokens for StrMapFunc {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let StrMapFunc { func_name, ret_ty, atoms, case } = self;

        let match_arms = atoms.0.iter()
            .map(|(&len, trie)| {
                let branch = Forest::branch_tokens(trie, *case == Case::Insensitive);
                let len = Literal::usize_unsuffixed(len);

                quote!(#len => #branch)
            });

        let body = quote! {
            match s.len() {
                #( #match_arms, )*
                _ => {}
            }

            None
        };

        tokens.extend(quote! {
            fn #func_name(s: &[u8]) -> Option<#ret_ty> {
                #body
            }
        });
    }
}

/// A set of tries where each trie only stores strings of a single length.
#[derive(Debug, Clone)]
pub struct Forest<T>(BTreeMap<usize, trie::Node<T>>);

impl<T> Default for Forest<T> {
    fn default() -> Self {
        Forest(Default::default())
    }
}

impl<T> Forest<T> {
    pub fn get(&mut self, bytes: &[u8]) -> Option<&T> {
        self.0.get(&bytes.len())
            .and_then(|n| n.get(bytes))
    }

    pub fn insert(&mut self, bytes: &[u8], value: T) -> Option<T> {
        let node = self.0.entry(bytes.len()).or_default();
        node.insert(bytes, value)
    }
}

fn byte_literal(b: u8) -> TokenStream {
    if b < 128 {
        let c: String = char::from(b).escape_default().collect();
        format!("b'{}'", c).parse().unwrap()
    } else {
        Literal::u8_unsuffixed(b).into_token_stream()
    }
}

impl<T> Forest<T>
    where T: ToTokens
{
    fn branch_tokens(node: &trie::Node<T>, ignore_case: bool) -> TokenStream {
        use trie::TraversalOrder::*;

        let mut tok = vec![TokenStream::new()];
        let mut depth = 0;
        let mut is_first_child = true;
        let mut dfs = node.dfs();
        while let Some((order, node)) = dfs.next() {
            if node.bytes.is_empty() {
                continue;
            }

            match order {
                Pre => {
                    if !is_first_child {
                        tok.last_mut().unwrap().extend(quote!(else));
                        is_first_child = true;
                    }

                    let i = (depth..depth+node.bytes.len()).map(Literal::usize_unsuffixed);
                    let b = node.bytes.iter().cloned().map(byte_literal);

                    if !ignore_case {
                        tok.last_mut().unwrap().extend(quote!(if #( s[#i] == #b )&&*));
                    } else {
                        tok.last_mut().unwrap().extend(quote!(if #( s[#i].eq_ignore_ascii_case(&#b) )&&*));
                    }

                    tok.push(TokenStream::new());
                    depth += node.bytes.len();

                    if let Some(v) = node.value {
                        // TODO: debug_assert_eq!(dfs.next().0, Post);

                        tok.last_mut().unwrap().extend(quote!(return Some(#v);));
                    }
                }

                Post => {
                    let body = tok.pop().unwrap();
                    tok.last_mut().unwrap().extend(quote!({ #body }));
                    depth -= node.bytes.len();
                    is_first_child = false;
                }
            }
        }

        let ret = tok.pop().unwrap();
        assert!(tok.is_empty());
        ret
    }
}