refactor(sysctl): Refactor some controller code (#146)

* Refactor if-let-Some into less complex expression

This makes the code a bit less "mutable", so to say.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>

* Simplify boolean expression

This patch refactors the two nested if expressions into one if.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>

* Refactor for-iteration into fn chain

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>

* Replace if-else with fn chaining

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>

* Replace if-else with fn chaining

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>

* Use explicit import for error type

---------

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
This commit is contained in:
Matthias Beyer 2023-10-28 15:18:09 +02:00 committed by GitHub
parent 46240e4a9c
commit f0b811d5f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
use crate::cache::{Cache, CacheData}; use crate::cache::{Cache, CacheData};
use crate::config::Config; use crate::config::Config;
use crate::error::Result; use crate::error::{Error, Result};
use crate::parsers::{parse_kernel_docs, KERNEL_DOCS_PATH}; use crate::parsers::{parse_kernel_docs, KERNEL_DOCS_PATH};
use crate::sysctl::parameter::Parameter; use crate::sysctl::parameter::Parameter;
use crate::sysctl::section::Section; use crate::sysctl::section::Section;
@ -14,6 +14,7 @@ use std::convert::TryFrom;
use std::env; use std::env;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::Write; use std::io::Write;
use std::ops::Not;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::result::Result as StdResult; use std::result::Result as StdResult;
use sysctl::{CtlFlags, CtlIter, Sysctl as SysctlImpl}; use sysctl::{CtlFlags, CtlIter, Sysctl as SysctlImpl};
@ -30,31 +31,36 @@ pub struct Sysctl {
impl Sysctl { impl Sysctl {
/// Constructs a new instance by fetching the available kernel parameters. /// Constructs a new instance by fetching the available kernel parameters.
pub fn init(config: Config) -> Result<Self> { pub fn init(config: Config) -> Result<Self> {
let mut parameters = Vec::new(); let parameters = CtlIter::root()
for ctl in CtlIter::root().filter_map(StdResult::ok).filter(|ctl| { .filter_map(StdResult::ok)
ctl.flags() .filter(|ctl| {
.map(|flags| !flags.contains(CtlFlags::SKIP)) ctl.flags()
.unwrap_or(false) .map(|flags| !flags.contains(CtlFlags::SKIP))
}) { .unwrap_or(false)
match Parameter::try_from(&ctl) { })
.filter_map(|ctl| match Parameter::try_from(&ctl) {
Ok(parameter) => { Ok(parameter) => {
if !config.display_deprecated { if !config.display_deprecated {
let mut skip_param = false; parameter
if let Some(param_name) = parameter.get_absolute_name() { .get_absolute_name()
skip_param = DEPRECATED_PARAMS.contains(&param_name); .map(|pname| DEPRECATED_PARAMS.contains(&pname))
} .unwrap_or(false)
if !skip_param { .not()
parameters.push(parameter); .then_some(Ok(parameter))
}
} else { } else {
parameters.push(parameter); Some(Ok(parameter))
} }
} }
Err(e) => { Err(e) => match ctl.name() {
log::trace!(target: "sysctl", "{} ({})", e, ctl.name()?); Ok(name) => {
} log::trace!(target: "sysctl", "{} ({})", e, name);
} None
} }
Err(e) => Some(Err(Error::from(e))),
},
})
.collect::<Result<Vec<_>>>()?;
Ok(Self { parameters, config }) Ok(Self { parameters, config })
} }
@ -92,11 +98,13 @@ impl Sysctl {
/// Updates the descriptions of the kernel parameters using the given cached data. /// Updates the descriptions of the kernel parameters using the given cached data.
pub fn update_docs_from_cache(&mut self, cache: &Cache) -> Result<()> { pub fn update_docs_from_cache(&mut self, cache: &Cache) -> Result<()> {
log::trace!(target: "cache", "{:?}", cache); log::trace!(target: "cache", "{:?}", cache);
let mut kernel_docs_path = if let Some(path) = &self.config.kernel_docs { let mut kernel_docs_path = self
vec![path.to_path_buf()] .config
} else { .kernel_docs
Vec::new() .as_ref()
}; .map(|p| vec![p.to_path_buf()])
.unwrap_or_default();
for path in KERNEL_DOCS_PATH { for path in KERNEL_DOCS_PATH {
if let Some(mut path) = globwalk::glob(path).ok().and_then(|glob| { if let Some(mut path) = globwalk::glob(path).ok().and_then(|glob| {
glob.filter_map(StdResult::ok) glob.filter_map(StdResult::ok)