nom_kconfig/attribute/
option.rs

1use nom::{
2    branch::alt,
3    bytes::complete::tag,
4    combinator::{map, value},
5    sequence::delimited,
6    IResult, Parser,
7};
8#[cfg(feature = "deserialize")]
9use serde::Deserialize;
10#[cfg(feature = "serialize")]
11use serde::Serialize;
12
13use crate::{symbol::parse_constant_symbol, util::ws, KconfigInput};
14
15#[derive(Debug, Clone, PartialEq)]
16#[cfg_attr(feature = "hash", derive(Hash))]
17#[cfg_attr(feature = "serialize", derive(Serialize))]
18#[cfg_attr(feature = "deserialize", derive(Deserialize))]
19pub enum OptionValues {
20    #[cfg_attr(
21        any(feature = "serialize", feature = "deserialize"),
22        serde(rename = "defconfig_list")
23    )]
24    DefconfigList,
25    #[cfg_attr(
26        any(feature = "serialize", feature = "deserialize"),
27        serde(rename = "modules")
28    )]
29    Modules,
30    AllNoConfigY,
31    Env(String),
32}
33
34#[cfg(feature = "display")]
35use std::fmt::Display;
36#[cfg(feature = "display")]
37impl Display for OptionValues {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match &self {
40            OptionValues::DefconfigList => write!(f, "defconfig_list"),
41            OptionValues::Modules => write!(f, "modules"),
42            OptionValues::AllNoConfigY => write!(f, "allnoconfig_y"),
43            OptionValues::Env(s) => write!(f, r#"env="{}""#, s),
44        }
45    }
46}
47
48pub fn parse_option(input: KconfigInput) -> IResult<KconfigInput, OptionValues> {
49    map((ws(tag("option")), ws(parse_option_value)), |(_, i)| i).parse(input)
50}
51
52pub fn parse_option_value(input: KconfigInput) -> IResult<KconfigInput, OptionValues> {
53    alt((
54        value(OptionValues::DefconfigList, ws(tag("defconfig_list"))),
55        value(OptionValues::Modules, ws(tag("modules"))),
56        value(OptionValues::AllNoConfigY, ws(tag("allnoconfig_y"))),
57        map(
58            (
59                ws(tag("env")),
60                ws(tag("=")),
61                delimited(tag("\""), parse_constant_symbol, tag("\"")),
62            ),
63            |(_, _, env)| OptionValues::Env(env.to_string()),
64        ),
65    ))
66    .parse(input)
67}