nom_kconfig/attribute/
imply.rs

1use nom::{bytes::complete::tag, combinator::map, IResult, Parser};
2#[cfg(feature = "deserialize")]
3use serde::Deserialize;
4#[cfg(feature = "serialize")]
5use serde::Serialize;
6
7use crate::{
8    symbol::{parse_symbol, Symbol},
9    util::ws,
10    KconfigInput,
11};
12
13use super::{expression::Expression, parse_if_attribute};
14
15/// Imply` is similar to "select" as it enforces a lower limit on another symbol except that the "implied" symbol's value may still be set to n from a direct dependency or with a visible prompt.
16#[derive(Debug, Clone, PartialEq)]
17#[cfg_attr(feature = "hash", derive(Hash))]
18#[cfg_attr(feature = "serialize", derive(Serialize))]
19#[cfg_attr(feature = "deserialize", derive(Deserialize))]
20pub struct Imply {
21    pub symbol: Symbol,
22    #[cfg_attr(
23        any(feature = "serialize", feature = "deserialize"),
24        serde(skip_serializing_if = "Option::is_none")
25    )]
26    pub r#if: Option<Expression>,
27}
28
29#[cfg(feature = "display")]
30use std::fmt::Display;
31#[cfg(feature = "display")]
32impl Display for Imply {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match &self.r#if {
35            Some(i) => write!(f, "{} if {}", self.symbol, i),
36            None => write!(f, "{}", self.symbol),
37        }
38    }
39}
40
41/// This parses a `imply` attribute.
42///
43/// # Example
44/// ```
45/// use nom_kconfig::{
46///     assert_parsing_eq,
47///     Symbol,
48///     attribute::{parse_imply, Imply}
49/// };
50///
51/// assert_parsing_eq!(
52///     parse_imply, "imply PCI",
53///     Ok((
54///         "",
55///         Imply {
56///             symbol: Symbol::Constant("PCI".to_string()),
57///             r#if: None
58///         }
59///     ))
60/// )
61/// ```
62pub fn parse_imply(input: KconfigInput) -> IResult<KconfigInput, Imply> {
63    map(
64        (ws(tag("imply")), ws(parse_symbol), parse_if_attribute),
65        |(_, s, i)| Imply { symbol: s, r#if: i },
66    )
67    .parse(input)
68}