Skip to main content

nom_kconfig/attribute/
depends_on.rs

1#[cfg(feature = "display")]
2use std::fmt::Display;
3
4use nom::{
5    bytes::complete::tag,
6    combinator::{map, opt},
7    IResult, Parser,
8};
9
10use super::expression::parse_expression;
11use crate::{
12    attribute::{expression::parse_if_expression, Expression},
13    util::wsi,
14    KconfigInput,
15};
16#[cfg(feature = "deserialize")]
17use serde::Deserialize;
18#[cfg(feature = "serialize")]
19use serde::Serialize;
20
21/// 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.
22#[derive(Debug, Clone, PartialEq)]
23#[cfg_attr(feature = "hash", derive(Hash))]
24#[cfg_attr(feature = "serialize", derive(Serialize))]
25#[cfg_attr(feature = "deserialize", derive(Deserialize))]
26pub struct DependsOn {
27    pub expression: Expression,
28    #[cfg_attr(
29        any(feature = "serialize", feature = "deserialize"),
30        serde(skip_serializing_if = "Option::is_none")
31    )]
32    pub r#if: Option<Expression>,
33}
34
35/// Parses a `depends on` attribute.
36/// If multiple dependencies are defined, they are connected with '&&'.
37/// Dependencies are applied to all other options within this menu entry (which also accept an "if" expression).
38/// See [https://www.kernel.org/doc/html/next/kbuild/kconfig-language.html#menu-attributes](https://www.kernel.org/doc/html/next/kbuild/kconfig-language.html#menu-attributes) for more information.
39///
40/// # Example
41/// ```
42/// use nom_kconfig::{
43///     assert_parsing_eq,
44///     attribute::{
45///         parse_depends_on,
46///         AndExpression, Atom, Expression, OrExpression, Term,
47///     },
48///     symbol::Symbol,
49///     Attribute
50/// };
51///
52/// assert_parsing_eq!(
53///     parse_depends_on,
54///     "depends on PCI",
55///     Ok((
56///         "",
57///         DependsOn { expression: Expression::Term(AndExpression::Term(
58///             Term::Atom(Atom::Symbol(Symbol::NonConstant("PCI".to_string())))
59///         )), r#if: None }
60///     ))
61/// )
62/// ```
63pub fn parse_depends_on(input: KconfigInput) -> IResult<KconfigInput, DependsOn> {
64    map(
65        (
66            tag("depends"),
67            wsi(opt(tag("on"))),
68            wsi(parse_expression),
69            opt(parse_if_expression),
70        ),
71        |(_, _, e, r#if)| DependsOn {
72            expression: e,
73            r#if,
74        },
75    )
76    .parse(input)
77}
78
79#[cfg(feature = "display")]
80impl Display for DependsOn {
81    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82        match &self.r#if {
83            Some(i) => write!(f, "{} if {}", self.expression, i),
84            None => write!(f, "{}", self.expression),
85        }
86    }
87}