nom_kconfig/
kconfig.rs

1use nom::{
2    combinator::{eof, map},
3    multi::many0,
4    sequence::delimited,
5    IResult, Parser,
6};
7#[cfg(feature = "deserialize")]
8use serde::Deserialize;
9#[cfg(feature = "serialize")]
10use serde::Serialize;
11
12use crate::{
13    entry::{parse_entry, Entry},
14    util::{ws, ws_comment},
15    KconfigInput,
16};
17
18/// A Kconfig file.
19/// Field `file` is relative to the root directory defined in [KconfigFile](crate::KconfigFile).
20#[derive(Debug, Clone, PartialEq, Default)]
21#[cfg_attr(feature = "hash", derive(Hash))]
22#[cfg_attr(feature = "serialize", derive(Serialize))]
23#[cfg_attr(feature = "deserialize", derive(Deserialize))]
24pub struct Kconfig {
25    pub file: String,
26    pub entries: Vec<Entry>,
27}
28
29/// Parses a kconfig input.
30/// # Example
31/// ```
32/// use std::path::PathBuf;
33/// use nom_kconfig::{KconfigInput, KconfigFile, Entry, kconfig::parse_kconfig, Kconfig};
34///
35/// let kconfig_file = KconfigFile::new(PathBuf::from("path/to/root/dir"), PathBuf::from("Kconfig"));
36/// let content = "";
37/// let input = KconfigInput::new_extra(content, kconfig_file);
38/// assert_eq!(parse_kconfig(input).unwrap().1, Kconfig {file: "Kconfig".to_string(), entries: vec!() })
39/// ```
40pub fn parse_kconfig(input: KconfigInput) -> IResult<KconfigInput, Kconfig> {
41    let file: std::path::PathBuf = input.extra.file.clone();
42    let (input, result) = map(delimited(ws_comment, many0(parse_entry), ws(eof)), |d| {
43        Kconfig {
44            file: file.display().to_string(),
45            entries: d,
46        }
47    })
48    .parse(input)?;
49    Ok((input, result))
50}