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
use nom::{
    branch::alt,
    bytes::complete::tag,
    combinator::{map, value},
    sequence::{delimited, tuple},
    IResult,
};
#[cfg(feature = "deserialize")]
use serde::Deserialize;
#[cfg(feature = "serialize")]
use serde::Serialize;

use crate::{symbol::parse_constant_symbol, util::ws, KconfigInput};

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub enum OptionValues {
    #[cfg_attr(
        any(feature = "serialize", feature = "deserialize"),
        serde(rename = "defconfig_list")
    )]
    DefconfigList,
    #[cfg_attr(
        any(feature = "serialize", feature = "deserialize"),
        serde(rename = "modules")
    )]
    Modules,
    AllNoConfigY,
    Env(String),
}

pub fn parse_option(input: KconfigInput) -> IResult<KconfigInput, OptionValues> {
    map(
        tuple((ws(tag("option")), ws(parse_option_value))),
        |(_, i)| i,
    )(input)
}

pub fn parse_option_value(input: KconfigInput) -> IResult<KconfigInput, OptionValues> {
    alt((
        value(OptionValues::DefconfigList, ws(tag("defconfig_list"))),
        value(OptionValues::Modules, ws(tag("modules"))),
        value(OptionValues::AllNoConfigY, ws(tag("allnoconfig_y"))),
        map(
            tuple((
                ws(tag("env")),
                ws(tag("=")),
                delimited(tag("\""), parse_constant_symbol, tag("\"")),
            )),
            |(_, _, env)| OptionValues::Env(env.to_string()),
        ),
    ))(input)
}