flexgen 0.4.5

A flexible, yet simple quote-based code generator for creating beautiful Rust code
Documentation
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::collections::HashMap;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::{fs, io};

use flexstr::SharedStr;

use crate::var::Vars;
use crate::{CodeFragments, Error, TokenVars};

const BUF_SIZE: usize = u16::MAX as usize;

const DEFAULT_FILENAME: &str = "flexgen.toml";

// *** FragmentItem ***

/// An enum that is either a reference to a code fragment or a fragment list
#[derive(Clone, Debug, serde::Deserialize, PartialEq)]
#[serde(untagged)]
pub enum FragmentItem {
    // Must be first so Serde uses this one always
    /// A single code fragment
    Fragment(SharedStr),
    /// A reference to a list of code fragments
    FragmentListRef(SharedStr),
}

// *** Fragment Lists ***

#[derive(Clone, Debug, Default, serde::Deserialize, PartialEq)]
struct FragmentLists(HashMap<SharedStr, Vec<FragmentItem>>);

impl FragmentLists {
    pub fn build(&self) -> Self {
        let mut lists = HashMap::with_capacity(self.0.len());

        for (key, fragments) in &self.0 {
            let mut new_fragments = Vec::with_capacity(fragments.len());

            for fragment in fragments {
                match fragment {
                    FragmentItem::Fragment(s) | FragmentItem::FragmentListRef(s) => {
                        // If it is also a key, that means it is a list reference
                        if self.0.contains_key(s) {
                            new_fragments.push(FragmentItem::FragmentListRef(s.clone()));
                        } else {
                            new_fragments.push(FragmentItem::Fragment(s.clone()));
                        }
                    }
                }
            }

            lists.insert(key.clone(), new_fragments);
        }

        Self(lists)
    }

    pub fn validate_code_fragments(&self, code: &CodeFragments) -> Result<(), Error> {
        let mut missing = Vec::new();

        // Loop over each fragment list searching for each item in the code fragments
        for fragments in self.0.values() {
            let v: Vec<_> = fragments
                .iter()
                .filter_map(|fragment| match fragment {
                    FragmentItem::Fragment(name) if !code.contains_key(name) => Some(name.clone()),
                    _ => None,
                })
                .collect();

            // Store all missing fragments
            missing.extend(v);
        }

        if missing.is_empty() {
            Ok(())
        } else {
            Err(Error::MissingFragments(missing))
        }
    }

    pub fn validate_file(&self, name: &SharedStr, f: &File) -> Result<(), Error> {
        // Ensure the file's fragment list exists
        if !self.0.contains_key(&f.fragment_list) {
            return Err(Error::MissingFragmentList(
                f.fragment_list.clone(),
                name.clone(),
            ));
        }

        let mut missing = Vec::new();

        'top: for exception in &f.fragment_list_exceptions {
            // If it is the name of a list, we can bypass the 2nd scan entirely
            if self.0.contains_key(exception) {
                continue;
            }

            // If it might be the name of an actual fragment we will need to scan them all
            for fragment_list in self.0.values() {
                // As soon as we find a match jump to looking for next exception
                if fragment_list.iter().any(|fragment| match fragment {
                    FragmentItem::Fragment(name) => name == exception,
                    _ => false,
                }) {
                    continue 'top;
                }
            }

            // If we didn't find as a list or via scan, it is missing
            missing.push(exception.clone());
        }

        if missing.is_empty() {
            Ok(())
        } else {
            Err(Error::MissingFragmentListExceptions(missing, name.clone()))
        }
    }

    #[inline]
    pub fn fragment_list(&self, name: &SharedStr) -> Result<&Vec<FragmentItem>, Error> {
        self.0
            .get(name)
            .ok_or_else(|| Error::FragmentListNotFound(name.clone()))
    }
}

// *** Config ***

#[derive(Clone, Debug, Default, serde::Deserialize, PartialEq)]
struct General {
    #[serde(default)]
    base_path: PathBuf,
    #[serde(default)]
    rust_fmt: RustFmt,
    #[serde(default)]
    vars: Vars,
}

impl General {
    #[inline]
    fn build_rust_fmt(&self) -> Option<rust_format::RustFmt> {
        self.rust_fmt.build_rust_fmt()
    }
}

#[derive(Clone, Debug, Default, serde::Deserialize, PartialEq)]
struct RustFmt {
    #[serde(default)]
    omit_final_format: bool,
    #[serde(default)]
    path: Option<PathBuf>,
    #[serde(default)]
    options: HashMap<SharedStr, SharedStr>,
}

impl RustFmt {
    fn build_rust_fmt(&self) -> Option<rust_format::RustFmt> {
        if !self.omit_final_format {
            let mut config = if !self.options.is_empty() {
                let map = self.options.iter().map(|(k, v)| (&**k, &**v)).collect();
                rust_format::Config::from_hash_map(map)
            } else {
                rust_format::Config::new()
            };
            if let Some(path) = &self.path {
                config = config.rust_fmt_path(path.clone())
            }

            Some(rust_format::RustFmt::from_config(config))
        } else {
            None
        }
    }
}

#[derive(Clone, Debug, Default, serde::Deserialize, PartialEq)]
struct File {
    path: PathBuf,
    fragment_list: SharedStr,
    #[serde(default)]
    fragment_list_exceptions: Vec<SharedStr>,
    vars: Vars,
}

/// The `flexgen` configuration
#[derive(Clone, Debug, Default, serde::Deserialize, PartialEq)]
pub struct Config {
    #[serde(default)]
    general: General,
    fragment_lists: FragmentLists,
    files: HashMap<SharedStr, File>,
}

impl Config {
    /// Try to load the `Config` from the given TOML reader
    pub fn from_toml_reader(r: impl io::Read) -> Result<Config, Error> {
        let mut reader = io::BufReader::new(r);
        let mut buffer = String::with_capacity(BUF_SIZE);
        reader.read_to_string(&mut buffer)?;

        Ok(toml::from_str(&buffer)?)
    }

    /// Try to load the `Config` from the default TOML file (flexgen.toml)
    pub fn from_default_toml_file() -> Result<Config, Error> {
        let f = fs::File::open(DEFAULT_FILENAME)?;
        Self::from_toml_reader(f)
    }

    /// Try to load the `Config` from the given TOML file
    pub fn from_toml_file(cfg_name: impl AsRef<Path>) -> Result<Config, Error> {
        let f = fs::File::open(cfg_name)?;
        Self::from_toml_reader(f)
    }

    pub(crate) fn build_and_validate(&mut self, code: &CodeFragments) -> Result<(), Error> {
        // Build and validate fragment lists against code fragments and files
        self.fragment_lists = self.fragment_lists.build();

        self.fragment_lists.validate_code_fragments(code)?;
        for (name, file) in &self.files {
            self.fragment_lists.validate_file(name, file)?;
        }

        Ok(())
    }

    /// Return all the files names specified in the config
    #[inline]
    pub fn file_names(&self) -> Vec<&SharedStr> {
        self.files.keys().collect()
    }

    /// Return the specified file configuration
    #[inline]
    fn file(&self, name: &SharedStr) -> Result<&File, Error> {
        self.files
            .get(name)
            .ok_or_else(|| Error::FileNotFound(name.clone()))
    }

    /// Build the full file path to the file given as a parameter
    pub fn file_path(&self, name: &SharedStr) -> Result<PathBuf, Error> {
        let file = self.file(name)?;
        let base_path = self.general.base_path.as_os_str();

        let mut path = PathBuf::with_capacity(base_path.len() + file.path.as_os_str().len());
        path.push(base_path);
        path.push(&file.path);
        Ok(path)
    }

    #[inline]
    fn convert_vars(vars: &Vars) -> Result<TokenVars, Error> {
        vars.iter()
            .map(|(key, value)| match value.to_token_item() {
                Ok(value) => Ok((key.clone(), value)),
                Err(err) => Err(err),
            })
            .collect()
    }

    #[inline]
    fn general_vars(&self) -> Result<TokenVars, Error> {
        Self::convert_vars(&self.general.vars)
    }

    #[inline]
    fn file_vars(&self, name: &SharedStr) -> Result<TokenVars, Error> {
        Self::convert_vars(&self.file(name)?.vars)
    }

    /// Return the complete vars for the file name given as a parameter
    #[inline]
    pub fn vars(&self, name: &SharedStr) -> Result<TokenVars, Error> {
        let mut vars = self.general_vars()?;
        vars.extend(self.file_vars(name)?);
        Ok(vars)
    }

    /// Return the given named fragment list
    #[inline]
    pub fn fragment_list(&self, name: &SharedStr) -> Result<&Vec<FragmentItem>, Error> {
        self.fragment_lists.fragment_list(name)
    }

    /// Return the fragment list used by the file given a parameter
    #[inline]
    pub fn file_fragment_list(&self, name: &SharedStr) -> Result<&Vec<FragmentItem>, Error> {
        let name = &self.file(name)?.fragment_list;
        self.fragment_list(name)
    }

    /// Return all the fragment exceptions for the given file
    #[inline]
    pub fn file_fragment_exceptions(&self, name: &SharedStr) -> Result<&Vec<SharedStr>, Error> {
        Ok(&self.file(name)?.fragment_list_exceptions)
    }

    /// Return a [RustFmt](rust_format::RustFmt) instance configured as specified in this configuration
    #[inline]
    pub fn build_rust_fmt(&self) -> Option<rust_format::RustFmt> {
        self.general.build_rust_fmt()
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::path::PathBuf;
    use std::str::FromStr;

    use flexstr::{shared_str, SharedStr};
    use pretty_assertions::assert_eq;

    use crate::config::{Config, File, FragmentItem, FragmentLists, General, RustFmt};
    use crate::var::{CodeValue, VarItem, VarValue};

    const CONFIG: &str = r#"
        [general]
        base_path = "src/"
        
        [general.rust_fmt]
        path = "rustfmt"
        
        [general.vars]
        product = "FlexStr"
        generate = true
        count = 5
        suffix = "$ident$Str"
        list = [ "FlexStr", true, 5, "$ident$Str" ]
                
        [fragment_lists]
        impl = [ "impl_struct", "impl_core_ref" ]
        impl_struct = [ "empty", "from_ref" ]
        
        [files.str]
        path = "strings/generated/std_str.rs"
        fragment_list = "impl"
        fragment_list_exceptions = [ "impl_core_ref" ]
        
        [files.str.vars]
        str_type = "str"
    "#;

    fn general() -> General {
        let mut vars = HashMap::new();

        let product = VarValue::String(shared_str!("FlexStr"));
        vars.insert(shared_str!("product"), VarItem::Single(product.clone()));

        let generate = VarValue::Bool(true);
        vars.insert(shared_str!("generate"), VarItem::Single(generate.clone()));

        let count = VarValue::Number(5);
        vars.insert(shared_str!("count"), VarItem::Single(count.clone()));

        let suffix = VarValue::CodeValue(CodeValue::from_str("$ident$Str").unwrap());
        vars.insert(shared_str!("suffix"), VarItem::Single(suffix.clone()));

        vars.insert(
            shared_str!("list"),
            VarItem::List(vec![product, generate, count, suffix]),
        );

        let rust_fmt = RustFmt {
            omit_final_format: false,
            path: Some("rustfmt".into()),
            options: Default::default(),
        };

        General {
            base_path: PathBuf::from("src/"),
            rust_fmt,
            vars,
        }
    }

    fn fragment_lists() -> FragmentLists {
        use FragmentItem::*;

        let mut lists = HashMap::new();
        lists.insert(
            shared_str!("impl"),
            vec![
                Fragment(shared_str!("impl_struct")),
                Fragment(shared_str!("impl_core_ref")),
            ],
        );
        lists.insert(
            shared_str!("impl_struct"),
            vec![
                Fragment(shared_str!("empty")),
                Fragment(shared_str!("from_ref")),
            ],
        );
        FragmentLists(lists)
    }

    fn files() -> HashMap<SharedStr, File> {
        let mut str_vars = HashMap::new();
        str_vars.insert(
            shared_str!("str_type"),
            VarItem::Single(VarValue::String(shared_str!("str"))),
        );

        let files_str = File {
            path: PathBuf::from("strings/generated/std_str.rs"),
            fragment_list: shared_str!("impl"),
            fragment_list_exceptions: vec![shared_str!("impl_core_ref")],
            vars: str_vars,
        };

        let mut files = HashMap::new();
        files.insert(shared_str!("str"), files_str);
        files
    }

    #[test]
    fn from_reader() {
        let actual = Config::from_toml_reader(CONFIG.as_bytes()).unwrap();
        let expected = Config {
            general: general(),
            fragment_lists: fragment_lists(),
            files: files(),
        };

        assert_eq!(expected, actual);
    }
}