use nom::{
combinator::{eof, map},
multi::many0,
sequence::delimited,
IResult, Parser,
};
#[cfg(feature = "deserialize")]
use serde::Deserialize;
#[cfg(feature = "serialize")]
use serde::Serialize;
#[cfg(feature = "debug")]
use tracing::debug;
use crate::{
entry::{parse_entry, Entry},
error::Error,
util::{ws, ws_comment},
KconfigInput,
};
#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub struct Kconfig {
pub file: String,
pub entries: Vec<Entry>,
}
pub fn parse_kconfig(input: KconfigInput) -> Result<(KconfigInput, Kconfig), Error> {
match private_parse_kconfig(input) {
Ok((input, result)) => Ok((input, result)),
Err(nom_error) => Err(Error::from(nom_error)),
}
}
pub(crate) fn private_parse_kconfig(input: KconfigInput) -> IResult<KconfigInput, Kconfig> {
#[cfg(feature = "debug")]
debug!("parsing '{}'", input.extra.full_path().display());
let file: std::path::PathBuf = input.extra.file.clone();
let (input, result) = map(delimited(ws_comment, many0(parse_entry), ws(eof)), |d| {
Kconfig {
file: file.display().to_string(),
entries: d,
}
})
.parse(input)?;
Ok((input, result))
}