From 51e8777c5c4a0512f062f08ee323a5a016c458f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Thu, 21 Oct 2021 22:13:45 +0300 Subject: [PATCH] refactor(kernel): construct the path in-place for sysctl sections --- systeroid-core/src/docs.rs | 9 ++++++--- systeroid/src/lib.rs | 12 +----------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/systeroid-core/src/docs.rs b/systeroid-core/src/docs.rs index 4d69a8a..b19622f 100644 --- a/systeroid-core/src/docs.rs +++ b/systeroid-core/src/docs.rs @@ -53,9 +53,12 @@ impl SysctlSection { ] } - /// Returns the sysctl section as a file with `.rst` extension. - pub fn as_file(&self) -> PathBuf { - Path::new(&self.to_string()).with_extension("rst") + /// Returns the path of the sysctl section. + pub fn as_path(&self, kernel_docs: &Path) -> PathBuf { + kernel_docs + .join("admin-guide") + .join("sysctl") + .join(Path::new(&self.to_string()).with_extension("rst")) } } diff --git a/systeroid/src/lib.rs b/systeroid/src/lib.rs index 7a4bb43..fca851b 100644 --- a/systeroid/src/lib.rs +++ b/systeroid/src/lib.rs @@ -7,7 +7,6 @@ pub mod args; use crate::args::Args; use rayon::prelude::*; -use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::sync::Mutex; use systeroid_core::docs::{Documentation, SysctlSection}; use systeroid_core::error::{Error, Result}; @@ -20,22 +19,13 @@ pub fn run(args: Args) -> Result<()> { let mut sysctl = Sysctl::init()?; 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()); SysctlSection::variants().par_iter().try_for_each(|s| { let mut param_docs = param_docs .lock() .map_err(|e| Error::ThreadLockError(e.to_string()))?; let mut parse = |section: SysctlSection| -> Result<()> { - let docs = reader::read_to_string(&sysctl_docs.join(section.as_file()))?; + let docs = reader::read_to_string(§ion.as_path(&kernel_docs))?; param_docs.extend(RstParser::parse_docs(&docs, section)?); Ok(()) };