diff --git a/Cargo.lock b/Cargo.lock index 0e1916c..a0a656d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -483,6 +483,16 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" +[[package]] +name = "parseit" +version = "0.1.0" +dependencies = [ + "flate2", + "globwalk", + "regex", + "thiserror", +] + [[package]] name = "pkg-config" version = "0.3.24" @@ -707,8 +717,8 @@ name = "systeroid" version = "0.1.0" dependencies = [ "getopts", + "parseit", "systeroid-core", - "systeroid-parser", ] [[package]] @@ -718,21 +728,11 @@ dependencies = [ "colored", "dirs-next", "lazy_static", + "parseit", "rayon", "serde", "serde_json", "sysctl", - "systeroid-parser", - "thiserror", -] - -[[package]] -name = "systeroid-parser" -version = "0.1.0" -dependencies = [ - "flate2", - "globwalk", - "regex", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index db9ca84..bdd07f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [workspace] members = [ - "systeroid-parser", "systeroid-core", "systeroid-tui", - "systeroid" + "systeroid", + "parseit", ] [profile.dev] diff --git a/systeroid-parser/Cargo.toml b/parseit/Cargo.toml similarity index 58% rename from systeroid-parser/Cargo.toml rename to parseit/Cargo.toml index 56864b6..080f02d 100644 --- a/systeroid-parser/Cargo.toml +++ b/parseit/Cargo.toml @@ -1,13 +1,17 @@ [package] -name = "systeroid-parser" +name = "parseit" version = "0.1.0" +description = "Simple text file parsing library powered by regex and glob patterns" authors = ["Orhun Parmaksız "] repository = "https://github.com/orhun/systeroid" license = "MIT OR Apache-2.0" edition = "2021" +[features] +gzip = ["flate2"] + [dependencies] regex = "1.5.4" globwalk = "0.8.1" thiserror = "1.0.29" -flate2 = "1.0.22" +flate2 = { version = "1.0.22", optional = true } diff --git a/systeroid-parser/examples/parse_manifest.rs b/parseit/examples/parse_manifest.rs similarity index 90% rename from systeroid-parser/examples/parse_manifest.rs rename to parseit/examples/parse_manifest.rs index 2eeee6e..1f14194 100644 --- a/systeroid-parser/examples/parse_manifest.rs +++ b/parseit/examples/parse_manifest.rs @@ -1,6 +1,6 @@ +use parseit::error::Error; +use parseit::parser::Parser; use std::path::PathBuf; -use systeroid_parser::error::Error; -use systeroid_parser::parser::Parser; // Parse Cargo manifest and print sections. fn main() -> Result<(), Error> { diff --git a/systeroid-parser/src/document.rs b/parseit/src/document.rs similarity index 100% rename from systeroid-parser/src/document.rs rename to parseit/src/document.rs diff --git a/systeroid-parser/src/error.rs b/parseit/src/error.rs similarity index 100% rename from systeroid-parser/src/error.rs rename to parseit/src/error.rs diff --git a/systeroid-parser/src/lib.rs b/parseit/src/lib.rs similarity index 57% rename from systeroid-parser/src/lib.rs rename to parseit/src/lib.rs index 27a7ee6..e4add38 100644 --- a/systeroid-parser/src/lib.rs +++ b/parseit/src/lib.rs @@ -1,4 +1,4 @@ -//! systeroid-parser +//! Simple text file parsing library powered by [regex](https://en.wikipedia.org/wiki/Regular_expression) and [glob patterns](https://en.wikipedia.org/wiki/Glob_(programming)). #![warn(missing_docs, clippy::unwrap_used)] @@ -11,7 +11,7 @@ pub use globwalk; /// Document parser. pub mod parser; -/// Parse results. +/// Parser results. pub mod document; /// Error implementation. diff --git a/systeroid-parser/src/parser.rs b/parseit/src/parser.rs similarity index 88% rename from systeroid-parser/src/parser.rs rename to parseit/src/parser.rs index 6f890f0..1208a74 100644 --- a/systeroid-parser/src/parser.rs +++ b/parseit/src/parser.rs @@ -6,7 +6,7 @@ use regex::{Captures, Regex, RegexBuilder}; use std::path::Path; use std::result::Result as StdResult; -/// Regex-powered parser for text documents. +/// Parser for text files. /// /// It is responsible for traversing the path specified with /// a glob pattern and parsing the contents of the files. @@ -59,11 +59,16 @@ impl<'a> Parser<'a> { .ok_or_else(|| Error::MissingFileError(file_name.to_string())) })?; for file in glob_files { - let input = if file.path().extension().and_then(|ext| ext.to_str()) == Some("gz") { - reader::read_gzip(file.path())? - } else { - reader::read_to_string(file.path())? - }; + let input = { + #[cfg(feature = "gzip")] + if file.path().extension().and_then(|ext| ext.to_str()) == Some("gz") { + reader::read_gzip(file.path()) + } else { + reader::read_to_string(file.path()) + } + #[cfg(not(feature = "gzip"))] + reader::read_to_string(file.path()) + }?; let capture_group = self .regex .captures_iter(&input) diff --git a/systeroid-parser/src/reader.rs b/parseit/src/reader.rs similarity index 95% rename from systeroid-parser/src/reader.rs rename to parseit/src/reader.rs index bc2f4a0..ccd91a8 100644 --- a/systeroid-parser/src/reader.rs +++ b/parseit/src/reader.rs @@ -1,7 +1,6 @@ -use flate2::read::GzDecoder; use std::fs::File; use std::io::{ - BufRead, BufReader as IoBufReader, Error as IoError, ErrorKind as IoErrorKind, Read, + BufRead, BufReader as IoBufReader, Error as IoError, ErrorKind as IoErrorKind, Result as IoResult, }; use std::path::Path; @@ -80,12 +79,14 @@ pub fn read_to_string>(path: P) -> IoResult { /// Reads (decodes) the given gzip file into a string. /// /// Uses [`BufReader`] under the hood. +#[cfg(feature = "gzip")] pub fn read_gzip>(path: P) -> IoResult { + use std::io::Read; let mut bytes = Vec::::new(); for read_bytes in BufReader::open(path, None)? { bytes.extend(read_bytes?.to_vec()); } - let mut gz = GzDecoder::new(&bytes[..]); + let mut gz = flate2::read::GzDecoder::new(&bytes[..]); let mut data = String::new(); gz.read_to_string(&mut data)?; Ok(data) diff --git a/systeroid-parser/tests/integration_test.rs b/parseit/tests/integration_test.rs similarity index 93% rename from systeroid-parser/tests/integration_test.rs rename to parseit/tests/integration_test.rs index faa83e6..4041270 100644 --- a/systeroid-parser/tests/integration_test.rs +++ b/parseit/tests/integration_test.rs @@ -1,6 +1,6 @@ +use parseit::error::Error; +use parseit::parser::Parser; use std::path::PathBuf; -use systeroid_parser::error::Error; -use systeroid_parser::parser::Parser; #[test] fn test_parser() -> Result<(), Error> { diff --git a/systeroid-core/Cargo.toml b/systeroid-core/Cargo.toml index c5c1ba4..685dd1c 100644 --- a/systeroid-core/Cargo.toml +++ b/systeroid-core/Cargo.toml @@ -16,6 +16,7 @@ serde = { version = "1.0.135", features = ["derive"] } serde_json = "1.0.78" dirs-next = "2.0.0" -[dependencies.systeroid-parser] +[dependencies.parseit] version = "0.1.0" -path = "../systeroid-parser" +path = "../parseit" +features = ["gzip"] diff --git a/systeroid-core/src/cache.rs b/systeroid-core/src/cache.rs index 5c0bd14..18fefd2 100644 --- a/systeroid-core/src/cache.rs +++ b/systeroid-core/src/cache.rs @@ -1,11 +1,11 @@ use crate::error::{Error, Result}; +use parseit::reader; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use std::fs::{self, File}; use std::io::Write; use std::path::{Path, PathBuf}; use std::time::SystemTime; -use systeroid_parser::reader; /// Cache data to store on the file system. #[derive(Debug, Serialize, Deserialize)] diff --git a/systeroid-core/src/error.rs b/systeroid-core/src/error.rs index 365a082..9d3f5ed 100644 --- a/systeroid-core/src/error.rs +++ b/systeroid-core/src/error.rs @@ -20,7 +20,7 @@ pub enum Error { SystemTimeError(#[from] std::time::SystemTimeError), /// Error that may occur while parsing documents. #[error(transparent)] - ParseError(#[from] systeroid_parser::error::Error), + ParseError(#[from] parseit::error::Error), /// Error that may occur while handling sysctl operations. #[error("sysctl error: `{0}`")] SysctlError(#[from] sysctl::SysctlError), diff --git a/systeroid-core/src/lib.rs b/systeroid-core/src/lib.rs index 69b1aa6..0dd3a77 100644 --- a/systeroid-core/src/lib.rs +++ b/systeroid-core/src/lib.rs @@ -6,7 +6,7 @@ extern crate lazy_static; /// Export regex crate. -pub use systeroid_parser::regex; +pub use parseit::regex; /// Sysctl implementation. pub mod sysctl; diff --git a/systeroid-core/src/parsers.rs b/systeroid-core/src/parsers.rs index 599082d..7c0189b 100644 --- a/systeroid-core/src/parsers.rs +++ b/systeroid-core/src/parsers.rs @@ -1,9 +1,9 @@ use crate::error::{Error, Result}; +use parseit::document::Document; +use parseit::parser::Parser; +use parseit::regex::RegexBuilder; use rayon::prelude::*; use std::path::Path; -use systeroid_parser::document::Document; -use systeroid_parser::parser::Parser; -use systeroid_parser::regex::RegexBuilder; lazy_static! { /// Possible locations for the Linux kernel documentation. diff --git a/systeroid/Cargo.toml b/systeroid/Cargo.toml index 1196b21..642dedc 100644 --- a/systeroid/Cargo.toml +++ b/systeroid/Cargo.toml @@ -14,9 +14,9 @@ live-tests = [] [dependencies] getopts = "0.2.21" -[dependencies.systeroid-parser] +[dependencies.parseit] version = "0.1.0" -path = "../systeroid-parser" +path = "../parseit" [dependencies.systeroid-core] version = "0.1.0" diff --git a/systeroid/src/app.rs b/systeroid/src/app.rs index 9ab7d25..48ef935 100644 --- a/systeroid/src/app.rs +++ b/systeroid/src/app.rs @@ -1,4 +1,6 @@ use crate::output::OutputType; +use parseit::globwalk; +use parseit::reader; use std::env; use std::io::Write; use std::path::PathBuf; @@ -10,8 +12,6 @@ use systeroid_core::sysctl::controller::Sysctl; use systeroid_core::sysctl::parameter::Parameter; use systeroid_core::sysctl::{DEPRECATED_PARAMS, SYSTEM_PRELOAD}; use systeroid_core::tree::{Tree, TreeNode}; -use systeroid_parser::globwalk; -use systeroid_parser::reader; /// Application controller. #[derive(Debug)]