nom_kconfig/entry/if.rs
1use nom::{
2 bytes::complete::tag,
3 combinator::{cut, map},
4 multi::many0,
5 sequence::{pair, terminated},
6 IResult, Parser,
7};
8#[cfg(feature = "deserialize")]
9use serde::Deserialize;
10#[cfg(feature = "serialize")]
11use serde::Serialize;
12
13use crate::{
14 attribute::expression::{parse_if_expression, Expression},
15 util::ws,
16 KconfigInput,
17};
18
19use super::{parse_entry, Entry};
20
21/// This defines an if block. The dependency expression [expr]((crate::attribute::expression)) is appended to all enclosed menu entries.
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 If {
27 pub condition: Expression,
28 pub entries: Vec<Entry>,
29}
30/// it parses a if block.
31///
32/// # Example
33/// ```
34/// use nom_kconfig::{
35/// assert_parsing_eq,
36/// Symbol,
37/// entry::{Entry, parse_if, Comment, If},
38/// attribute::{Term, Expression, AndExpression, Atom}
39/// };
40/// assert_parsing_eq!(
41/// parse_if,
42/// r#"if NET_VENDOR_AMD comment "Support of PCI" endif"#,
43/// Ok((
44/// "",
45/// If {
46/// condition: Expression::Term(AndExpression::Term(Term::Atom(Atom::Symbol(
47/// Symbol::Constant("NET_VENDOR_AMD".to_string())
48/// )))),
49/// entries: vec!(Entry::Comment(Comment { prompt: "Support of PCI".to_string(), dependencies: vec!() }))
50/// }
51/// ))
52/// )
53/// ```
54pub fn parse_if(input: KconfigInput) -> IResult<KconfigInput, If> {
55 map(
56 pair(
57 ws(parse_if_expression),
58 cut(terminated(many0(parse_entry), ws(tag("endif")))),
59 ),
60 |(condition, entries)| If { condition, entries },
61 )
62 .parse(input)
63}