mirror of
https://github.com/orhun/systeroid
synced 2024-07-21 10:25:00 +00:00
feat(sysctl): parallelize the documentation updates
This commit is contained in:
parent
8aca59a897
commit
354bc5dd77
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -316,7 +316,6 @@ name = "systeroid"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"getopts",
|
||||
"rayon",
|
||||
"systeroid-core",
|
||||
"systeroid-parser",
|
||||
]
|
||||
|
@ -326,6 +325,7 @@ name = "systeroid-core"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"rayon",
|
||||
"sysctl",
|
||||
"systeroid-parser",
|
||||
"thiserror",
|
||||
|
|
|
@ -8,6 +8,7 @@ edition = "2021"
|
|||
sysctl = "0.4.2"
|
||||
thiserror = "1.0.29"
|
||||
lazy_static = "1.4.0"
|
||||
rayon = "1.5.1"
|
||||
|
||||
[dependencies.systeroid-parser]
|
||||
version = "0.1.0"
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::error::Result;
|
||||
use rayon::prelude::*;
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
use std::path::Path;
|
||||
use std::result::Result as StdResult;
|
||||
|
@ -115,28 +116,30 @@ impl Sysctl {
|
|||
///
|
||||
/// [`parsed document`]: Document
|
||||
pub fn update_docs(&mut self, documents: Vec<Document>) {
|
||||
for param in self
|
||||
.parameters
|
||||
.iter_mut()
|
||||
self.parameters
|
||||
.par_iter_mut()
|
||||
.filter(|p| p.description.is_none() || p.description.as_deref() == Some("[N/A]"))
|
||||
{
|
||||
for document in documents
|
||||
.iter()
|
||||
.filter(|document| Section::from(document.path.as_path()) == param.section)
|
||||
{
|
||||
if let Some(paragraph) = document.paragraphs.iter().find(|paragraph| {
|
||||
match param.name.split('.').collect::<Vec<&str>>().last() {
|
||||
Some(absolute_name) => {
|
||||
absolute_name.len() > 2 && paragraph.title.contains(absolute_name)
|
||||
}
|
||||
_ => false,
|
||||
.for_each(|param| {
|
||||
for document in documents
|
||||
.iter()
|
||||
.filter(|document| Section::from(document.path.as_path()) == param.section)
|
||||
{
|
||||
if let Some(paragraph) =
|
||||
document.paragraphs.par_iter().find_first(|paragraph| {
|
||||
match param.name.split('.').collect::<Vec<&str>>().last() {
|
||||
Some(absolute_name) => {
|
||||
absolute_name.len() > 2
|
||||
&& paragraph.title.contains(absolute_name)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
})
|
||||
{
|
||||
param.description = Some(paragraph.contents.to_owned());
|
||||
param.document = Some(document.clone());
|
||||
continue;
|
||||
}
|
||||
}) {
|
||||
param.description = Some(paragraph.contents.to_owned());
|
||||
param.document = Some(document.clone());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ authors = ["Orhun Parmaksız <orhunparmaksiz@gmail.com>"]
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rayon = "1.5.1"
|
||||
getopts = "0.2.21"
|
||||
|
||||
[dependencies.systeroid-parser]
|
||||
|
|
|
@ -6,42 +6,22 @@
|
|||
pub mod args;
|
||||
|
||||
use crate::args::Args;
|
||||
use rayon::prelude::*;
|
||||
use std::sync::Mutex;
|
||||
use systeroid_core::error::{Error, Result};
|
||||
use systeroid_core::parsers::PARSERS;
|
||||
use systeroid_core::sysctl::Sysctl;
|
||||
use systeroid_parser::document::Document;
|
||||
use systeroid_parser::parser::Parser;
|
||||
|
||||
/// Runs `systeroid`.
|
||||
pub fn run(args: Args) -> Result<()> {
|
||||
let mut sysctl = Sysctl::init()?;
|
||||
|
||||
let documents = if let Some(kernel_docs) = args.kernel_docs {
|
||||
let documents = Mutex::new(Vec::new());
|
||||
PARSERS.par_iter().try_for_each(|s| {
|
||||
let mut documents = documents
|
||||
.lock()
|
||||
.map_err(|e| Error::ThreadLockError(e.to_string()))?;
|
||||
let mut parse = |parser: Parser| -> Result<()> {
|
||||
if let Some(kernel_docs) = args.kernel_docs {
|
||||
let documents = PARSERS
|
||||
.iter()
|
||||
.try_fold(Vec::new(), |mut documents, parser| {
|
||||
documents.extend(parser.parse(&kernel_docs)?);
|
||||
Ok(())
|
||||
};
|
||||
parse(s.clone())
|
||||
})?;
|
||||
let documents = documents
|
||||
.lock()
|
||||
.map_err(|e| Error::ThreadLockError(e.to_string()))?
|
||||
.clone()
|
||||
.into_iter()
|
||||
.collect::<Vec<Document>>();
|
||||
Some(documents)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some(documents) = documents {
|
||||
Ok::<Vec<Document>, Error>(documents)
|
||||
})?;
|
||||
sysctl.update_docs(documents);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue