nom_kconfig/attribute/
select.rs

1use nom::{branch::alt, bytes::complete::tag, combinator::map, IResult, Parser};
2#[cfg(feature = "deserialize")]
3use serde::Deserialize;
4#[cfg(feature = "serialize")]
5use serde::Serialize;
6#[cfg(feature = "display")]
7use std::fmt::Display;
8
9use crate::{symbol::parse_constant_symbol, util::ws, KconfigInput};
10
11use super::expression::{parse_if_attribute, Expression};
12
13/// While normal dependencies reduce the upper limit of a symbol, reverse dependencies can be used to force a lower limit of another symbol. The value of the current menu symbol is used as the minimal value [symbol](crate::Symbol) can be set to. If [symbol](crate::Symbol) is selected multiple times, the limit is set to the largest selection. Reverse dependencies can only be used with boolean or tristate symbols.
14#[derive(Debug, Default, Clone, PartialEq)]
15#[cfg_attr(feature = "hash", derive(Hash))]
16#[cfg_attr(feature = "serialize", derive(Serialize))]
17#[cfg_attr(feature = "deserialize", derive(Deserialize))]
18pub struct Select {
19    pub symbol: String,
20    #[cfg_attr(
21        any(feature = "serialize", feature = "deserialize"),
22        serde(skip_serializing_if = "Option::is_none")
23    )]
24    pub r#if: Option<Expression>,
25}
26
27#[cfg(feature = "display")]
28impl Display for Select {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        match &self.r#if {
31            Some(i) => write!(f, "{} if {}", self.symbol, i),
32            None => write!(f, "{}", self.symbol),
33        }
34    }
35}
36
37/// Parses a `select` attribute.
38/// # Example
39/// ```
40/// use nom_kconfig::{
41/// assert_parsing_eq,
42/// attribute::{parse_select, Select}
43/// };
44///
45/// assert_parsing_eq!(
46///     parse_select,
47///     "select MTK_INFRACFG",
48///     Ok(("", Select {
49///             r#if: None,
50///             symbol: "MTK_INFRACFG".to_string()
51///         }
52///     ))
53/// )
54/// ```
55pub fn parse_select(input: KconfigInput) -> IResult<KconfigInput, Select> {
56    map(
57        (
58            ws(alt((tag("select"), tag("enable")))),
59            ws(parse_constant_symbol),
60            parse_if_attribute,
61        ),
62        |(_, s, i)| Select {
63            symbol: s.to_string(),
64            r#if: i,
65        },
66    )
67    .parse(input)
68}