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
mod input_field;

use self::input_field::InputField;
use std::ops::Deref;
use syn::{parse::Parse, punctuated::Punctuated, Token};

pub struct DialectInput {
    name: String,
    table_gen: Option<String>,
    td_file: Option<String>,
    include_directories: Vec<String>,
}

impl DialectInput {
    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn table_gen(&self) -> Option<&str> {
        self.table_gen.as_deref()
    }

    pub fn td_file(&self) -> Option<&str> {
        self.td_file.as_deref()
    }

    pub fn include_directories(&self) -> impl Iterator<Item = &str> {
        self.include_directories.iter().map(Deref::deref)
    }
}

impl Parse for DialectInput {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let mut name = None;
        let mut table_gen = None;
        let mut td_file = None;
        let mut includes = vec![];

        for item in Punctuated::<InputField, Token![,]>::parse_terminated(input)? {
            match item {
                InputField::Name(field) => name = Some(field.value()),
                InputField::TableGen(td) => table_gen = Some(td.value()),
                InputField::TdFile(file) => td_file = Some(file.value()),
                InputField::IncludeDirectories(field) => {
                    includes = field.into_iter().map(|literal| literal.value()).collect()
                }
            }
        }

        Ok(Self {
            name: name.ok_or(input.error("dialect name required"))?,
            table_gen,
            td_file,
            include_directories: includes,
        })
    }
}