diff --git a/kparams-core/src/kernel.rs b/kparams-core/src/kernel.rs index 713af43..a23930e 100644 --- a/kparams-core/src/kernel.rs +++ b/kparams-core/src/kernel.rs @@ -1,14 +1,48 @@ -/// Documentation of the Linux kernel. +use std::fmt::{self, Display, Formatter}; +use std::path::{Path, PathBuf}; + +/// Sections of the sysctl documentation. #[derive(Clone, Debug)] -pub struct Documentation<'a> { - /// Kernel parameters. - pub parameters: Vec>, +pub enum SysctlSection { + /// Documentation for `/proc/sys/abi/*` + Abi, + /// Documentation for `/proc/sys/fs/*` + Fs, + /// Documentation for `/proc/sys/kernel/*` + Kernel, + /// Documentation for `/proc/sys/net/*` + Net, + /// Documentation for `/proc/sys/sunrpc/*` + Sunrpc, + /// Documentation for `/proc/sys/user/*` + User, + /// Documentation for `/proc/sys/vm/*` + Vm, } -impl<'a> Documentation<'a> { - /// Constructs a new instance. - pub fn new(parameters: Vec>) -> Self { - Self { parameters } +impl Display for SysctlSection { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{}", format!("{:?}", self).to_lowercase()) + } +} + +impl SysctlSection { + /// Returns the variants. + pub fn iter() -> &'static [&'static SysctlSection] { + &[ + &Self::Abi, + &Self::Fs, + &Self::Kernel, + &Self::Net, + &Self::Sunrpc, + &Self::User, + &Self::Vm, + ] + } + + /// Returns the sysctl section as a file with `.rst` extension. + pub fn as_file(&self) -> PathBuf { + Path::new(&self.to_string()).with_extension("rst") } } @@ -19,11 +53,17 @@ pub struct Parameter<'a> { pub name: &'a str, /// Description of the kernel parameter. pub description: &'a str, + /// Section of the kernel parameter. + pub section: &'a SysctlSection, } impl<'a> Parameter<'a> { /// Constructs a new instance. - pub fn new(name: &'a str, description: &'a str) -> Self { - Self { name, description } + pub fn new(name: &'a str, description: &'a str, section: &'a SysctlSection) -> Self { + Self { + name, + description, + section, + } } } diff --git a/kparams-parser/src/parser.rs b/kparams-parser/src/parser.rs index 0dc3182..8f53a4c 100644 --- a/kparams-parser/src/parser.rs +++ b/kparams-parser/src/parser.rs @@ -2,7 +2,7 @@ use crate::title::Title; use kparams_core::error::{Error, Result}; -use kparams_core::kernel::{Documentation, Parameter}; +use kparams_core::kernel::{Parameter, SysctlSection}; use pest::Parser; use std::convert::TryFrom; @@ -12,10 +12,13 @@ use std::convert::TryFrom; pub struct RstParser; impl RstParser { - /// Parses the given reStructuredText input and returns the [`kernel documentation`]. + /// Parses the given reStructuredText input and returns the [`kernel parameters`]. /// - /// [`kernel documentation`]: Documentation - pub fn parse_docs(input: &str) -> Result { + /// [`kernel parameters`]: Parameter + pub fn parse_docs<'a>( + input: &'a str, + section: &'a SysctlSection, + ) -> Result>> { let mut kernel_parameters = Vec::new(); let rst_document = Self::parse(Rule::document, input).map_err(|e| Error::ParseError(e.to_string()))?; @@ -30,8 +33,9 @@ impl RstParser { } else { (input[title.end_pos..]).as_ref() }, + section, )); } - Ok(Documentation::new(kernel_parameters)) + Ok(kernel_parameters) } } diff --git a/kparams/src/lib.rs b/kparams/src/lib.rs index 73357cb..0e53ca7 100644 --- a/kparams/src/lib.rs +++ b/kparams/src/lib.rs @@ -3,6 +3,7 @@ #![warn(missing_docs, clippy::unwrap_used)] use kparams_core::error::Result; +use kparams_core::kernel::SysctlSection; use kparams_core::reader; use kparams_parser::parser::RstParser; use std::path::PathBuf; @@ -11,12 +12,15 @@ use std::path::PathBuf; pub fn run() -> Result<()> { let kernel_docs = PathBuf::from("/usr/share/doc/linux"); let sysctl_docs = kernel_docs.join("admin-guide").join("sysctl"); - let kernel_section = reader::read_to_string(&sysctl_docs.join("fs.rst"))?; - let kernel_section_docs = RstParser::parse_docs(&kernel_section)?; - for kernel_parameter in kernel_section_docs.parameters { - println!("## {}", kernel_parameter.name); - println!("{}", kernel_parameter.description); + for sysctl_section in SysctlSection::iter() { + let sysctl_section_docs = + reader::read_to_string(&sysctl_docs.join(sysctl_section.as_file()))?; + let kernel_parameters = RstParser::parse_docs(&sysctl_section_docs, sysctl_section)?; + for param in kernel_parameters { + println!("## {}::{}", param.section, param.name); + println!("{}", param.description); + } } Ok(())