nom_kconfig/lib.rs
1#![allow(clippy::result_large_err)]
2
3//! # nom-kconfig
4//!
5//! A parser for kconfig files. The parsing is done with [nom](https://github.com/rust-bakery/nom).
6//!
7//! ```no_run
8//! use std::path::PathBuf;
9//! use nom_kconfig::{parse_kconfig, KconfigInput, KconfigFile};
10//! use std::collections::HashMap;
11//!
12//! // curl https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.4.9.tar.xz | tar -xJ -C /tmp/
13//! fn main() -> Result<(), Box<dyn std::error::Error>> {
14//! let mut variables = HashMap::new();
15//! variables.insert("SRCARCH", "x86");
16//! let kconfig_file = KconfigFile::new_with_vars(
17//! PathBuf::from("/tmp/linux-6.4.9"),
18//! PathBuf::from("/tmp/linux-6.4.9/Kconfig"),
19//! &variables,
20//! &HashMap::default(),
21//! );
22//! let input = kconfig_file.read_to_string().unwrap();
23//! let kconfig = parse_kconfig(KconfigInput::new_extra(&input, kconfig_file));
24//! println!("{:?}", kconfig);
25//! Ok(())
26//! }
27//! ```
28
29pub mod attribute;
30pub mod entry;
31pub mod error;
32pub mod kconfig;
33pub mod kconfig_file;
34pub mod string;
35pub mod symbol;
36pub mod tristate;
37pub mod util;
38
39pub use self::attribute::Attribute;
40pub use self::entry::Entry;
41pub use self::kconfig::{parse_kconfig, Kconfig};
42pub use self::symbol::Symbol;
43pub use kconfig_file::KconfigFile;
44use nom_locate::LocatedSpan;
45
46/// [KconfigInput] is a struct gathering a [KconfigFile] and its associated content.
47pub type KconfigInput<'a> = LocatedSpan<&'a str, KconfigFile>;
48
49#[cfg(test)]
50pub mod kconfig_test;
51#[cfg(test)]
52pub mod lib_test;
53mod number;
54#[cfg(test)]
55pub mod symbol_test;
56#[cfg(test)]
57pub mod util_test;
58
59#[macro_export]
60macro_rules! assert_parsing_eq {
61 ($fn:ident, $input:expr, $expected:expr) => {{
62 use std::collections::HashMap;
63 use $crate::KconfigFile;
64 use $crate::KconfigInput;
65
66 let mut variables = HashMap::new();
67 variables.insert("SUBARCH", "x86");
68 let kconfig_file = KconfigFile::new_with_vars(
69 Default::default(),
70 Default::default(),
71 &variables,
72 &HashMap::default(),
73 );
74
75 let res = $fn(KconfigInput::new_extra($input, kconfig_file))
76 .map(|r| (r.0.fragment().to_owned(), r.1));
77 assert_eq!(res, $expected)
78 }};
79}