refactor(sysctl): move section to sysctl module

This commit is contained in:
Orhun Parmaksız 2021-10-26 20:15:51 +03:00
parent 1efc8cf504
commit b95b0ebf06
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 79 additions and 79 deletions

View file

@ -1,69 +1,4 @@
use std::fmt::{self, Display, Formatter};
use std::path::Path;
/// Sections of the sysctl documentation.
#[derive(Clone, Copy, Debug, PartialEq)]
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,
/// Unknown.
Unknown,
}
impl From<String> for SysctlSection {
fn from(value: String) -> Self {
for section in Self::variants() {
if value.starts_with(&format!("{}.", section)) {
return *section;
}
}
Self::Unknown
}
}
impl<'a> From<&'a Path> for SysctlSection {
fn from(value: &'a Path) -> Self {
for section in Self::variants() {
if value.file_stem().map(|v| v.to_str()).flatten() == Some(&section.to_string()) {
return *section;
}
}
Self::Unknown
}
}
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 variants() -> &'static [SysctlSection] {
&[
Self::Abi,
Self::Fs,
Self::Kernel,
Self::Net,
Self::Sunrpc,
Self::User,
Self::Vm,
]
}
}
use crate::sysctl::Section;
/// Documentation of a kernel parameter.
#[derive(Clone, Debug)]
@ -73,12 +8,12 @@ pub struct Documentation {
/// Description of the kernel parameter.
pub description: String,
/// Section of the kernel parameter.
pub section: SysctlSection,
pub section: Section,
}
impl Documentation {
/// Constructs a new instance.
pub fn new(name: String, description: String, section: SysctlSection) -> Self {
pub fn new(name: String, description: String, section: Section) -> Self {
Self {
name,
description,

View file

@ -1,8 +1,74 @@
use crate::docs::{Documentation, SysctlSection};
use crate::docs::Documentation;
use crate::error::Result;
use std::fmt::{self, Display, Formatter};
use std::path::Path;
use std::result::Result as StdResult;
use sysctl::{CtlFlags, CtlIter, Sysctl as SysctlImpl};
/// Sections of the sysctl documentation.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Section {
/// 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,
/// Unknown.
Unknown,
}
impl From<String> for Section {
fn from(value: String) -> Self {
for section in Self::variants() {
if value.starts_with(&format!("{}.", section)) {
return *section;
}
}
Self::Unknown
}
}
impl<'a> From<&'a Path> for Section {
fn from(value: &'a Path) -> Self {
for section in Self::variants() {
if value.file_stem().map(|v| v.to_str()).flatten() == Some(&section.to_string()) {
return *section;
}
}
Self::Unknown
}
}
impl Display for Section {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", format!("{:?}", self).to_lowercase())
}
}
impl Section {
/// Returns the variants.
pub fn variants() -> &'static [Section] {
&[
Self::Abi,
Self::Fs,
Self::Kernel,
Self::Net,
Self::Sunrpc,
Self::User,
Self::Vm,
]
}
}
/// Representation of a kernel parameter.
#[derive(Debug)]
pub struct Parameter {
@ -13,7 +79,7 @@ pub struct Parameter {
/// Description of the kernel parameter
pub description: Option<String>,
/// Section of the kernel parameter.
pub section: SysctlSection,
pub section: Section,
/// Documentation of the kernel parameter.
pub documentation: Option<Documentation>,
}
@ -38,7 +104,7 @@ impl Sysctl {
name: ctl.name()?,
value: ctl.value_string()?,
description: ctl.description().ok(),
section: SysctlSection::from(ctl.name()?),
section: Section::from(ctl.name()?),
documentation: None,
});
}

View file

@ -1,9 +1,10 @@
use regex::{Captures, RegexBuilder};
use std::path::Path;
use std::result::Result as StdResult;
use systeroid_core::docs::{Documentation, SysctlSection};
use systeroid_core::docs::Documentation;
use systeroid_core::error::{Error, Result};
use systeroid_core::reader;
use systeroid_core::sysctl::Section;
/// Parser for the reStructuredText format.
#[derive(Clone, Copy, Debug)]
@ -13,7 +14,7 @@ pub struct RstParser<'a> {
/// Regular expression to use for parsing.
pub regex: &'a str,
/// Section of the parsed documents.
pub section: Option<SysctlSection>,
pub section: Option<Section>,
}
impl RstParser<'_> {
@ -36,9 +37,7 @@ impl RstParser<'_> {
.map_err(|e| Error::GlobError(e.to_string()))?
.filter_map(StdResult::ok)
{
let section = self
.section
.unwrap_or_else(|| SysctlSection::from(file.path()));
let section = self.section.unwrap_or_else(|| Section::from(file.path()));
let input = reader::read_to_string(file.path())?;
let capture_group = regex.captures_iter(&input).collect::<Vec<Captures<'_>>>();

View file

@ -8,9 +8,9 @@ pub mod args;
use crate::args::Args;
use rayon::prelude::*;
use std::sync::Mutex;
use systeroid_core::docs::{Documentation, SysctlSection};
use systeroid_core::docs::Documentation;
use systeroid_core::error::{Error, Result};
use systeroid_core::sysctl::Sysctl;
use systeroid_core::sysctl::{Section, Sysctl};
use systeroid_parser::parser::RstParser;
/// Runs `systeroid`.
@ -26,7 +26,7 @@ pub fn run(args: Args) -> Result<()> {
RstParser {
glob_path: "networking/*-sysctl.rst",
regex: "^([a-zA-Z0-9_/-]+)[ ]-[ ][a-zA-Z].*$",
section: Some(SysctlSection::Net),
section: Some(Section::Net),
},
];