nom_kconfig/attribute/
option.rs1use 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::{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
34use nom::bytes::complete::take_while1;
35#[cfg(feature = "display")]
36use std::fmt::Display;
37
38#[cfg(feature = "display")]
39impl Display for OptionValues {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 match &self {
42 OptionValues::DefconfigList => write!(f, "defconfig_list"),
43 OptionValues::Modules => write!(f, "modules"),
44 OptionValues::AllNoConfigY => write!(f, "allnoconfig_y"),
45 OptionValues::Env(s) => write!(f, r#"env="{}""#, s),
46 }
47 }
48}
49
50pub fn parse_option(input: KconfigInput) -> IResult<KconfigInput, OptionValues> {
52 map((ws(tag("option")), ws(parse_option_value)), |(_, i)| i).parse(input)
53}
54
55pub fn parse_option_value(input: KconfigInput) -> IResult<KconfigInput, OptionValues> {
56 alt((
57 value(OptionValues::DefconfigList, ws(tag("defconfig_list"))),
58 value(OptionValues::Modules, ws(tag("modules"))),
59 value(OptionValues::AllNoConfigY, ws(tag("allnoconfig_y"))),
60 map(
61 (
62 ws(tag::<&str, KconfigInput, _>("env")),
63 ws(tag("=")),
64 delimited(
65 tag("\""),
66 take_while1(|c: char| c.is_alphanumeric() || c == '_'),
67 tag("\""),
68 ),
69 ),
70 |(_, _, env)| OptionValues::Env(env.to_string()),
71 ),
72 ))
73 .parse(input)
74}