refactor(kernel): construct the path in-place for sysctl sections

This commit is contained in:
Orhun Parmaksız 2021-10-21 22:13:45 +03:00
parent 8813bb9486
commit 51e8777c5c
No known key found for this signature in database
GPG Key ID: F83424824B3E4B90
2 changed files with 7 additions and 14 deletions

View File

@ -53,9 +53,12 @@ impl SysctlSection {
] ]
} }
/// Returns the sysctl section as a file with `.rst` extension. /// Returns the path of the sysctl section.
pub fn as_file(&self) -> PathBuf { pub fn as_path(&self, kernel_docs: &Path) -> PathBuf {
Path::new(&self.to_string()).with_extension("rst") kernel_docs
.join("admin-guide")
.join("sysctl")
.join(Path::new(&self.to_string()).with_extension("rst"))
} }
} }

View File

@ -7,7 +7,6 @@ pub mod args;
use crate::args::Args; use crate::args::Args;
use rayon::prelude::*; use rayon::prelude::*;
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::sync::Mutex; use std::sync::Mutex;
use systeroid_core::docs::{Documentation, SysctlSection}; use systeroid_core::docs::{Documentation, SysctlSection};
use systeroid_core::error::{Error, Result}; use systeroid_core::error::{Error, Result};
@ -20,22 +19,13 @@ pub fn run(args: Args) -> Result<()> {
let mut sysctl = Sysctl::init()?; let mut sysctl = Sysctl::init()?;
let param_docs = 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(
IoErrorKind::Other,
format!("cannot find sysctl documentation: {:?}", sysctl_docs),
)
.into());
}
let param_docs = Mutex::new(Vec::new()); let param_docs = Mutex::new(Vec::new());
SysctlSection::variants().par_iter().try_for_each(|s| { SysctlSection::variants().par_iter().try_for_each(|s| {
let mut param_docs = param_docs let mut param_docs = param_docs
.lock() .lock()
.map_err(|e| Error::ThreadLockError(e.to_string()))?; .map_err(|e| Error::ThreadLockError(e.to_string()))?;
let mut parse = |section: SysctlSection| -> Result<()> { let mut parse = |section: SysctlSection| -> Result<()> {
let docs = reader::read_to_string(&sysctl_docs.join(section.as_file()))?; let docs = reader::read_to_string(&section.as_path(&kernel_docs))?;
param_docs.extend(RstParser::parse_docs(&docs, section)?); param_docs.extend(RstParser::parse_docs(&docs, section)?);
Ok(()) Ok(())
}; };