nom_config_in/entry/
if.rs1use nom::{
2 bytes::complete::tag,
3 combinator::{map, opt},
4 multi::many0,
5 sequence::{preceded, tuple},
6 IResult,
7};
8use serde::Serialize;
9
10use crate::{util::ws, ConfigInInput};
11
12use super::{
13 expression::{parse_expression, Expression},
14 parse_entry, Entry,
15};
16
17pub fn parse_if(input: ConfigInInput) -> IResult<ConfigInInput, If> {
18 map(
19 tuple((
20 ws(parse_if_condition),
21 many0(parse_entry),
22 opt(parse_else),
23 ws(opt(tag("fi"))),
24 )),
25 |(condition, entries, e, _)| If {
26 condition,
27 if_block: entries,
28 else_block: e,
29 },
30 )(input)
31}
32
33#[derive(Debug, Clone, Serialize, PartialEq)]
34pub struct If {
35 pub condition: Expression,
36 pub if_block: Vec<Entry>,
37 #[serde(skip_serializing_if = "Option::is_none")]
38 pub else_block: Option<Vec<Entry>>,
39}
40
41pub fn parse_if_condition(input: ConfigInInput) -> IResult<ConfigInInput, Expression> {
42 map(
43 tuple((
44 ws(tag("if")),
45 ws(ws(parse_expression)),
46 opt(ws(tag(";"))),
47 opt(ws(tag("then"))),
48 )),
49 |(_, e, _, _)| e,
50 )(input)
51}
52
53pub fn parse_else(input: ConfigInInput) -> IResult<ConfigInInput, Vec<Entry>> {
54 preceded(ws(tag("else")), many0(parse_entry))(input)
55}