feat(kernel): parse all the sysctl sections

This commit is contained in:
Orhun Parmaksız 2021-10-09 13:15:37 +03:00
parent 4ba7177181
commit 86cb65245b
No known key found for this signature in database
GPG Key ID: F83424824B3E4B90
3 changed files with 68 additions and 20 deletions

View File

@ -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<Parameter<'a>>,
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<Parameter<'a>>) -> 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,
}
}
}

View File

@ -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<Documentation> {
/// [`kernel parameters`]: Parameter
pub fn parse_docs<'a>(
input: &'a str,
section: &'a SysctlSection,
) -> Result<Vec<Parameter<'a>>> {
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)
}
}

View File

@ -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(())