nom_config_in/
lib.rs

1use std::{fs, io, path::PathBuf};
2
3use config_in::ConfigIn;
4
5use nom::{
6    combinator::{eof, map},
7    multi::many0,
8    sequence::delimited,
9    IResult,
10};
11
12use nom_locate::LocatedSpan;
13use util::ws;
14
15use crate::{entry::parse_entry, util::ws_comment};
16
17pub mod config_in;
18pub mod entry;
19pub mod symbol;
20pub mod util;
21
22#[cfg(test)]
23pub mod config_test;
24#[cfg(test)]
25mod lib_test;
26#[cfg(test)]
27pub mod symbol_test;
28#[cfg(test)]
29pub mod util_test;
30
31pub type ConfigInInput<'a> = LocatedSpan<&'a str, ConfigInFile>;
32
33/// Represents a Kconfig file.
34/// - [root_dir] is the absolute path of the kernel root directory.
35/// - [file] is the path the the Kconfig you want to parse
36#[derive(Debug, Clone, Default)]
37pub struct ConfigInFile {
38    pub root_dir: PathBuf,
39    pub file: PathBuf,
40}
41
42impl ConfigInFile {
43    pub fn new(root_dir: PathBuf, file: PathBuf) -> Self {
44        Self { root_dir, file }
45    }
46
47    pub fn full_path(&self) -> PathBuf {
48        self.root_dir.join(&self.file)
49    }
50
51    pub fn read_to_string(&self) -> io::Result<String> {
52        fs::read_to_string(self.full_path())
53    }
54}
55
56pub fn parse_config_in(input: ConfigInInput) -> IResult<ConfigInInput, ConfigIn> {
57    let file = &input.extra.file.clone();
58    let (input, result) = map(delimited(ws_comment, many0(parse_entry), ws_comment), |d| {
59        ConfigIn {
60            file: file.display().to_string(),
61            entries: d,
62        }
63    })(input)?;
64    let (input, _) = ws(eof)(input)?;
65    // TODO
66    Ok((input, result))
67}