refactor(lib): remove references in kernel types

This commit is contained in:
Orhun Parmaksız 2021-10-09 23:27:30 +03:00
parent 6e440b31c8
commit 0795f8c8bc
No known key found for this signature in database
GPG Key ID: F83424824B3E4B90
3 changed files with 30 additions and 30 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, Debug)]
#[derive(Clone, Copy, Debug)]
pub enum SysctlSection {
/// Documentation for `/proc/sys/abi/*`
Abi,
@ -28,15 +28,15 @@ impl Display for SysctlSection {
impl SysctlSection {
/// Returns the variants.
pub fn iter() -> &'static [&'static SysctlSection] {
pub fn variants() -> &'static [SysctlSection] {
&[
&Self::Abi,
&Self::Fs,
&Self::Kernel,
&Self::Net,
&Self::Sunrpc,
&Self::User,
&Self::Vm,
Self::Abi,
Self::Fs,
Self::Kernel,
Self::Net,
Self::Sunrpc,
Self::User,
Self::Vm,
]
}
@ -48,18 +48,18 @@ impl SysctlSection {
/// Representation of a kernel parameter.
#[derive(Clone, Debug)]
pub struct Parameter<'a> {
pub struct Parameter {
/// Name of the kernel parameter.
pub name: &'a str,
pub name: String,
/// Description of the kernel parameter.
pub description: &'a str,
pub description: String,
/// Section of the kernel parameter.
pub section: &'a SysctlSection,
pub section: SysctlSection,
}
impl<'a> Parameter<'a> {
impl Parameter {
/// Constructs a new instance.
pub fn new(name: &'a str, description: &'a str, section: &'a SysctlSection) -> Self {
pub fn new(name: String, description: String, section: SysctlSection) -> Self {
Self {
name,
description,

View File

@ -15,10 +15,7 @@ impl RstParser {
/// Parses the given reStructuredText input and returns the [`kernel parameters`].
///
/// [`kernel parameters`]: Parameter
pub fn parse_docs<'a>(
input: &'a str,
section: &'a SysctlSection,
) -> Result<Vec<Parameter<'a>>> {
pub fn parse_docs(input: &str, section: SysctlSection) -> Result<Vec<Parameter>> {
let mut kernel_parameters = Vec::new();
let rst_document =
Self::parse(Rule::document, input).map_err(|e| Error::ParseError(e.to_string()))?;
@ -27,11 +24,13 @@ impl RstParser {
.collect::<Vec<Title<'_>>>();
for (i, title) in titles.iter().enumerate() {
kernel_parameters.push(Parameter::new(
title.value,
title.value.to_string(),
if let Some(next_title) = titles.get(i + 1) {
(input[title.end_pos..next_title.start_pos]).trim()
(input[title.end_pos..next_title.start_pos])
.trim()
.to_string()
} else {
(input[title.end_pos..]).trim()
(input[title.end_pos..]).trim().to_string()
},
section,
));

View File

@ -13,14 +13,15 @@ pub fn run() -> Result<()> {
let kernel_docs = PathBuf::from("/usr/share/doc/linux");
let sysctl_docs = kernel_docs.join("admin-guide").join("sysctl");
for sysctl_section in SysctlSection::iter() {
let sysctl_section_docs =
reader::read_to_string(&sysctl_docs.join(sysctl_section.as_file()))?;
let kernel_parameters = RstParser::parse_docs(&sysctl_section_docs, sysctl_section)?;
for param in kernel_parameters {
println!("## {}::{}\n", param.section, param.name);
println!("{}\n", param.description);
}
let mut kernel_parameters = Vec::new();
for section in SysctlSection::variants().iter() {
let docs = reader::read_to_string(&sysctl_docs.join(section.as_file()))?;
kernel_parameters.extend(RstParser::parse_docs(&docs, *section)?);
}
for param in kernel_parameters {
println!("## {}::{}\n", param.section, param.name);
println!("{}\n", param.description);
}
Ok(())