use nom::{
branch::alt,
bytes::complete::tag,
combinator::map,
sequence::{pair, preceded, tuple},
IResult,
};
#[cfg(feature = "deserialize")]
use serde::Deserialize;
#[cfg(feature = "serialize")]
use serde::Serialize;
#[cfg(feature = "display")]
use std::fmt::Display;
use crate::{
symbol::{parse_symbol, Symbol},
util::ws,
KconfigInput,
};
use super::expression::{parse_if_attribute, parse_number, Expression};
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub struct Range {
pub lower_bound: Symbol,
pub upper_bound: Symbol,
#[cfg_attr(
any(feature = "serialize", feature = "deserialize"),
serde(skip_serializing_if = "Option::is_none")
)]
pub r#if: Option<Expression>,
}
#[cfg(feature = "display")]
impl Display for Range {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.r#if {
Some(i) => write!(f, "{} {} if {}", self.lower_bound, self.upper_bound, i),
None => write!(f, "{} {}", self.lower_bound, self.upper_bound),
}
}
}
fn parse_bounds(input: KconfigInput) -> IResult<KconfigInput, (Symbol, Symbol)> {
alt((
map(tuple((ws(parse_number), ws(parse_number))), |(l, r)| {
(
Symbol::Constant(l.to_string()),
Symbol::Constant(r.to_string()),
)
}),
tuple((ws(parse_symbol), ws(parse_symbol))),
))(input)
}
pub fn parse_range(input: KconfigInput) -> IResult<KconfigInput, Range> {
map(
preceded(ws(tag("range")), pair(ws(parse_bounds), parse_if_attribute)),
|((l, r), i)| Range {
lower_bound: l,
upper_bound: r,
r#if: i,
},
)(input)
}