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
use std::path::Path;

use case::CaseExt;

use crate::{
    error::{Error, GenerationError},
    files,
    generation,
    options::{EnumOptions},
    validation,
};

pub fn generate_files_enum<P: AsRef<Path>>(
    dirpath: P,
    options: &EnumOptions,
) -> Result<String, Error> {
    let mut keys: Vec<String> = vec![];
    for entry in std::fs::read_dir(dirpath)? {
        let entry = entry?;
        if entry.file_type()?.is_file() {
            let variant_name = entry
                    .path()
                    .file_stem()
                    .expect("TODO:")
                    .to_string_lossy()
                    .to_camel();

            if !variant_name.starts_with('.') {
                keys.push(variant_name);
            }
        }
    }

    // NOTE: Important that they stay sorted by Vec::sort
    keys.sort();

    // TODO: Refactor, Validate other identifiers, DRY-fail - see lib.rs
    {
        for key in &keys {
            if !validation::valid_identifier(key) {
                return Err(GenerationError::InvalidVariantName(key.into()))?;
            }
        }
    }

    let enum_code = generation::generate_enum(&keys, options);

    Ok(enum_code)
}

pub fn create_files_enum<SrcPath: AsRef<Path>, DstPath: AsRef<Path>>(
    dirpath: SrcPath,
    destination: DstPath,
    options: &EnumOptions,
) -> Result<(), Error> {
    let output = generate_files_enum(dirpath, options)?;
    files::ensure_destination(destination.as_ref(), options.create_dirs)?;
    files::write_destination(destination.as_ref(), output, options.write_only_if_changed)?;

    Ok(())
}