feat(docs): associate the documentation with the parameters

This commit is contained in:
Orhun Parmaksız 2021-10-18 20:39:45 +03:00
parent 1e6d80f973
commit cb1c0339a8
No known key found for this signature in database
GPG Key ID: F83424824B3E4B90
3 changed files with 51 additions and 10 deletions

View File

@ -2,7 +2,7 @@ use std::fmt::{self, Display, Formatter};
use std::path::{Path, PathBuf};
/// Sections of the sysctl documentation.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SysctlSection {
/// Documentation for `/proc/sys/abi/*`
Abi,

View File

@ -1,4 +1,4 @@
use crate::docs::SysctlSection;
use crate::docs::{Documentation, SysctlSection};
use crate::error::Result;
use std::result::Result as StdResult;
use sysctl::{CtlIter, Sysctl as SysctlImpl};
@ -13,6 +13,8 @@ pub struct Parameter {
pub description: Option<String>,
/// Section of the kernel parameter.
pub section: SysctlSection,
/// Documentation of the kernel parameter.
pub documentation: Option<Documentation>,
}
/// Sysctl wrapper for managing the kernel parameters.
@ -31,8 +33,36 @@ impl Sysctl {
value: ctl.value_string()?,
description: ctl.description().ok(),
section: SysctlSection::from(ctl.name()?),
documentation: None,
});
}
Ok(Self { parameters })
}
/// Updates the description of the kernel parameters based on the parsed documentation.
///
/// [`parsed documentation`]: Documentation
pub fn update_docs(&mut self, docs: Vec<Documentation>) {
for param in self
.parameters
.iter_mut()
.filter(|p| p.description.is_none() || p.description.as_deref() == Some("[N/A]"))
{
if let Some(documentation) =
docs.iter().find(
|doc| match param.name.split('.').collect::<Vec<&str>>().last() {
Some(absolute_name) => {
absolute_name.len() > 2
&& doc.name.contains(absolute_name)
&& doc.section == param.section
}
_ => false,
},
)
{
param.description = Some(documentation.description.to_owned());
param.documentation = Some(documentation.clone());
}
}
}
}

View File

@ -17,9 +17,9 @@ use systeroid_parser::parser::RstParser;
/// Runs `systeroid`.
pub fn run(args: Args) -> Result<()> {
let sysctl = Sysctl::init()?;
let mut sysctl = Sysctl::init()?;
if let Some(kernel_docs) = args.kernel_docs {
let param_docs = if let Some(kernel_docs) = args.kernel_docs {
let sysctl_docs = kernel_docs.join("admin-guide").join("sysctl");
if !sysctl_docs.exists() {
return Err(IoError::new(
@ -41,18 +41,29 @@ pub fn run(args: Args) -> Result<()> {
};
parse(*s)
})?;
let _param_docs = param_docs
let param_docs = param_docs
.lock()
.map_err(|e| Error::ThreadLockError(e.to_string()))?
.iter()
.collect::<Vec<&Documentation>>();
.clone()
.into_iter()
.collect::<Vec<Documentation>>();
Some(param_docs)
} else {
None
};
if let Some(param_docs) = param_docs {
sysctl.update_docs(param_docs);
}
for param in sysctl.parameters {
println!(
"{} -> {}: {} ({:?})",
param.section, param.name, param.value, param.description
"{} ({})\n===\n{}\n",
param.name,
param.documentation.map(|d| d.name).unwrap_or_default(),
param
.description
.unwrap_or_else(|| String::from("no documentation"))
);
}