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
use crate::parser::kconfig_parser_state::{Building, Done, KconfigParserState, Parsing};
use std::marker::PhantomData;
use std::path::Path;
use crate::errors::parser_error::ParserError;
use crate::parser::parser_config::ParserConfig;
use crate::parser::utils::get_string_from_path::get_string_from_path;
use crate::parser::utils::read_file_to_string::read_file_to_string;
use crate::structure::kconfig::Kconfig;

pub struct KconfigParser<State: KconfigParserState> {
    state: PhantomData<State>,

    pub(crate) top_kconfig_path: String,
    pub(crate) top_kconfig_source: String,

    pub(crate) config: ParserConfig,

    pub(crate) result: Option<Result<Kconfig, ParserError>>,
}

impl KconfigParser<Building> {
    pub fn new() -> Self {
        KconfigParser {
            state: Default::default(),
            top_kconfig_path: ".".to_string(),
            top_kconfig_source: "".to_string(),
            config: Default::default(),
            result: None,
        }
    }

    pub fn set_kconfig_path(mut self, path: &Path) -> Result<Self, ParserError> {
        self.top_kconfig_path = get_string_from_path(path);
        self.top_kconfig_source = read_file_to_string(path)?;
        Ok(self)
    }

    pub fn set_kconfig_source(mut self, source: String) -> Self {
        self.top_kconfig_path = ".".to_string();
        self.top_kconfig_source = source;

        self
    }

    pub fn allow_sourcing(mut self, allow: bool) -> Self {
        self.config.can_source = allow;

        self
    }

    pub fn set_variable(mut self, variable: &str, value: &str) -> Self {
        self.config.variables.insert(variable.to_string(), value.to_string());

        self
    }

    pub fn parse(self) -> KconfigParser<Done> {
        let mut parsing_parser: KconfigParser<Parsing> = KconfigParser {
            state: Default::default(),
            top_kconfig_path: self.top_kconfig_path,
            top_kconfig_source: self.top_kconfig_source,
            config: self.config,
            result: None,
        };

        parsing_parser.config.root_path = parsing_parser.top_kconfig_path.clone();
        parsing_parser.parse();

        KconfigParser {
            state: Default::default(),
            top_kconfig_path: parsing_parser.top_kconfig_path,
            top_kconfig_source: parsing_parser.top_kconfig_source,
            config: parsing_parser.config,
            result: parsing_parser.result,
        }
    }
}

impl KconfigParser<Done> {
    pub fn take_result(self) -> Result<Kconfig, ParserError> {
        self.result.unwrap()
    }
}