From f050daf035fdfc23a403716b53dea3878af05672 Mon Sep 17 00:00:00 2001 From: Didier Date: Mon, 17 Jul 2023 21:23:59 +0200 Subject: [PATCH] format: fix formatting --- manifest/src/pkgr.rs | 4 +-- pkgr/src/commands.rs | 17 +++++++---- pkgr/src/logging.rs | 43 +++++++++++----------------- pkgr/src/main.rs | 12 ++++---- pkgr/src/package/fetch.rs | 11 ++++--- pkgr/src/package/identifier.rs | 2 +- pkgr/src/package/installer/errors.rs | 4 +-- pkgr/src/package/installer/mod.rs | 19 ++++++------ pkgr/src/package/mod.rs | 28 ++++++++++-------- pkgr/src/process/mod.rs | 13 ++++----- pkgr/src/process/output.rs | 6 ++-- pkgr/src/tmpfs.rs | 6 ++-- 12 files changed, 80 insertions(+), 85 deletions(-) diff --git a/manifest/src/pkgr.rs b/manifest/src/pkgr.rs index 700c6cb..530a37c 100644 --- a/manifest/src/pkgr.rs +++ b/manifest/src/pkgr.rs @@ -1,5 +1,5 @@ -use std::fmt::{Display, Formatter}; use serde::{Deserialize, Serialize}; +use std::fmt::{Display, Formatter}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct PKGR {} @@ -8,4 +8,4 @@ impl Display for PKGR { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "") } -} \ No newline at end of file +} diff --git a/pkgr/src/commands.rs b/pkgr/src/commands.rs index 2265330..8c7996e 100644 --- a/pkgr/src/commands.rs +++ b/pkgr/src/commands.rs @@ -1,10 +1,10 @@ -use std::process::exit; use crate::package::identifier::PackageIdentifier; +use crate::package::installer::{InstallType, PackageInstaller}; +use crate::package::Package; use clap::{Parser, Subcommand}; use log::{debug, error, info, trace, warn}; use manifest::Manifest; -use crate::package::installer::{InstallType, PackageInstaller}; -use crate::package::Package; +use std::process::exit; #[derive(Parser, Debug)] #[clap(name = "pkgr", version)] @@ -86,9 +86,11 @@ impl Command { info!("Install complete."); info!("Install took {}ms.", duration.as_nanos() as f64 / 1000000.0); - } - Command::Remove { package_identifier, index } => { + Command::Remove { + package_identifier, + index, + } => { if let PackageIdentifier::URI(_) = package_identifier { error!("URI is unsupported when removing applications."); exit(1); @@ -113,7 +115,10 @@ impl Command { info!("PKGR AUTHORS: {}", env!("CARGO_PKG_AUTHORS")); info!("PKGR DESCRIPTION: {}", env!("CARGO_PKG_DESCRIPTION")); info!(""); - info!("PKGR_LOG_LEVEL: {}", std::env::var("PKGR_LOG_LEVEL").unwrap_or_else(|_| "info".to_string())); + info!( + "PKGR_LOG_LEVEL: {}", + std::env::var("PKGR_LOG_LEVEL").unwrap_or_else(|_| "info".to_string()) + ); } Command::None => { error!("No command was specified."); diff --git a/pkgr/src/logging.rs b/pkgr/src/logging.rs index 6ee98a5..225ef27 100644 --- a/pkgr/src/logging.rs +++ b/pkgr/src/logging.rs @@ -1,26 +1,18 @@ -use log::SetLoggerError; -use fern::Dispatch; -use std::env; use colored::Colorize; +use fern::Dispatch; +use log::SetLoggerError; +use std::env; pub fn setup_logger() -> Result<(), SetLoggerError> { Dispatch::new() .format(|out, message, record| { match record.metadata().target() { "command:stdout" => { - out.finish(format_args!( - "{} {}", - "::".cyan(), - message.to_string() - )); + out.finish(format_args!("{} {}", "::".cyan(), message.to_string())); return; } "command:stderr" => { - out.finish(format_args!( - "{} {}", - "!!".red(), - message.to_string() - )); + out.finish(format_args!("{} {}", "!!".red(), message.to_string())); return; } _ => { @@ -28,30 +20,29 @@ pub fn setup_logger() -> Result<(), SetLoggerError> { "{} {}", // Some logic so messages look nice if message.to_string().len() > 0 { - match record - .level() - .to_string() - .chars() - .nth(0) - .unwrap_or('T') - { + match record.level().to_string().chars().nth(0).unwrap_or('T') { 'T' => "[TRACE]".bright_blue(), 'D' => "??".green(), 'I' => "=>".blue(), 'W' => "##".yellow(), 'E' => "!!".red(), _ => "**".blue(), - }.to_string() - } else { "".to_string() }, + } + .to_string() + } else { + "".to_string() + }, message.to_string().bright_white() )) } } }) - .level(env::var("PKGR_LOG_LEVEL") - .unwrap_or_else(|_| "info".to_string()) - .parse() - .unwrap_or(log::LevelFilter::Info)) + .level( + env::var("PKGR_LOG_LEVEL") + .unwrap_or_else(|_| "info".to_string()) + .parse() + .unwrap_or(log::LevelFilter::Info), + ) .chain(std::io::stdout()) .apply() } diff --git a/pkgr/src/main.rs b/pkgr/src/main.rs index aaa5387..cb73e53 100644 --- a/pkgr/src/main.rs +++ b/pkgr/src/main.rs @@ -1,15 +1,15 @@ -use std::process::Command; +use crate::commands::Cli; +use crate::process::Process; use clap::Parser; use colored::Colorize; -use crate::commands::Cli; use log::trace; -use crate::process::Process; +use std::process::Command; -mod process; mod commands; -mod package; -mod tmpfs; mod logging; +mod package; +mod process; +mod tmpfs; fn main() { logging::setup_logger().expect("Unable to setup logger."); diff --git a/pkgr/src/package/fetch.rs b/pkgr/src/package/fetch.rs index 099c5b2..a97dfb6 100644 --- a/pkgr/src/package/fetch.rs +++ b/pkgr/src/package/fetch.rs @@ -1,9 +1,9 @@ +use crate::package::identifier::PackageLocator; +use pkgfile::PKGFile; +use reqwest::blocking::get; use std::error::Error; use std::fmt::Display; use std::io::Read; -use pkgfile::PKGFile; -use reqwest::blocking::get; -use crate::package::identifier::PackageLocator; #[derive(Debug)] pub enum FetchError { @@ -26,9 +26,8 @@ impl Error for FetchError {} pub fn fetch_by_path>(path: S) -> Result { std::fs::read(path.into()) - .map_err(|e| FetchError::IOError(e)).and_then(|bytes| { - PKGFile::try_from(bytes).map_err(|_| FetchError::ParseError) - }) + .map_err(|e| FetchError::IOError(e)) + .and_then(|bytes| PKGFile::try_from(bytes).map_err(|_| FetchError::ParseError)) } pub fn fetch_by_uri>(uri: S) -> Result { diff --git a/pkgr/src/package/identifier.rs b/pkgr/src/package/identifier.rs index fa457d8..17064e4 100644 --- a/pkgr/src/package/identifier.rs +++ b/pkgr/src/package/identifier.rs @@ -88,7 +88,7 @@ impl FromStr for PackageLocator { fn from_str(s: &str) -> Result { #[allow(unused_assignments)] // false positive - let mut name = None; + let mut name = None; let mut version = None; let mut tags = None; diff --git a/pkgr/src/package/installer/errors.rs b/pkgr/src/package/installer/errors.rs index a6801c0..46cd402 100644 --- a/pkgr/src/package/installer/errors.rs +++ b/pkgr/src/package/installer/errors.rs @@ -15,7 +15,7 @@ impl Display for BinError { #[derive(Debug)] pub enum BuildError { - InvalidManifest + InvalidManifest, } impl Display for BuildError { @@ -30,7 +30,7 @@ impl Display for BuildError { pub enum InstallError { BuildError(BuildError), BinError(BinError), - Generic + Generic, } impl ToString for InstallError { diff --git a/pkgr/src/package/installer/mod.rs b/pkgr/src/package/installer/mod.rs index 8f75941..0d474aa 100644 --- a/pkgr/src/package/installer/mod.rs +++ b/pkgr/src/package/installer/mod.rs @@ -1,23 +1,23 @@ -use std::fmt::Display; -use log::{debug, trace}; +use crate::tmpfs::TempDir; use errors::{BinError, BuildError, InstallError}; +use log::{debug, trace}; use manifest::Manifest; use pkgfile::PKGFile; -use crate::tmpfs::TempDir; +use std::fmt::Display; pub mod errors; #[derive(Debug)] pub enum InstallType { Build, - Bin + Bin, } #[derive(Debug)] pub struct PackageInstaller { manifest: Manifest, pkgfile: PKGFile, - install_type: InstallType + install_type: InstallType, } impl PackageInstaller { @@ -25,7 +25,7 @@ impl PackageInstaller { PackageInstaller { manifest: m, pkgfile: p, - install_type: i + install_type: i, } } @@ -46,8 +46,8 @@ impl PackageInstaller { tmpdir.push(&self.manifest.package.name); trace!("extracting package into: {}", tmpdir.to_string()); match self.extract_to(tmpdir.to_string()) { - Ok(_) => {}, - Err(e) => return Err(e) + Ok(_) => {} + Err(e) => return Err(e), } debug!("extracted package in: {}", tmpdir.to_string()); Ok(()) @@ -60,14 +60,13 @@ impl PackageInstaller { let build_manifest = self.manifest.build.clone().unwrap(); // TODO: Check dependencies - Ok(()) } pub fn install(&self) -> Result<(), InstallError> { match self.install_type { InstallType::Bin => self.bin().map_err(|e| InstallError::BinError(e)), - InstallType::Build => self.build().map_err(|e| InstallError::BuildError(e)) + InstallType::Build => self.build().map_err(|e| InstallError::BuildError(e)), } } } diff --git a/pkgr/src/package/mod.rs b/pkgr/src/package/mod.rs index 7a7d6b2..e23a191 100644 --- a/pkgr/src/package/mod.rs +++ b/pkgr/src/package/mod.rs @@ -1,8 +1,8 @@ use log::trace; +pub mod fetch; pub mod identifier; pub mod installer; -pub mod fetch; pub struct Package { pub identifier: identifier::PackageIdentifier, @@ -23,43 +23,47 @@ impl Package { } /// Fetch a package from a package identifier. - pub fn fetch(package_identifier: identifier::PackageIdentifier) -> Result { + pub fn fetch( + package_identifier: identifier::PackageIdentifier, + ) -> Result { match &package_identifier { identifier::PackageIdentifier::Path(path) => { trace!("fetching package from path: {}", path); let pkgfile = fetch::fetch_by_path(path).unwrap(); Ok(Package::new(package_identifier, pkgfile)) - }, + } identifier::PackageIdentifier::URI(url) => { trace!("fetching package from uri: {}", url); let pkgfile = fetch::fetch_by_uri(url).unwrap(); Ok(Package::new(package_identifier, pkgfile)) - }, + } identifier::PackageIdentifier::PackageLocator(locator) => { trace!("fetching package from locator: {}", locator); let pkgfile = fetch::fetch_by_package_locator(locator.clone()).unwrap(); Ok(Package::new(package_identifier, pkgfile)) - }, + } } } /// Get the package manifest. pub fn manifest(&self) -> manifest::Manifest { - manifest::Manifest::try_from(self.pkgfile.manifest.clone()) - .unwrap() + manifest::Manifest::try_from(self.pkgfile.manifest.clone()).unwrap() } - /// Install the package. pub fn install(&mut self) -> Result<(), installer::errors::InstallError> { let manifest = self.manifest(); - let installer = installer::PackageInstaller::new(manifest.clone(), self.pkgfile.clone(), installer::InstallType::Bin); + let installer = installer::PackageInstaller::new( + manifest.clone(), + self.pkgfile.clone(), + installer::InstallType::Bin, + ); match installer.install() { Ok(_) => { self.is_installed = true; Ok(()) - }, - Err(e) => Err(e) + } + Err(e) => Err(e), } } @@ -77,4 +81,4 @@ impl Package { pub fn remove(&mut self) -> Result<(), ()> { unimplemented!(); } -} \ No newline at end of file +} diff --git a/pkgr/src/process/mod.rs b/pkgr/src/process/mod.rs index 70ab93b..21586c0 100644 --- a/pkgr/src/process/mod.rs +++ b/pkgr/src/process/mod.rs @@ -10,11 +10,7 @@ pub struct Process { impl Process { pub fn new(command: Vec, cwd: Option, env: Vec) -> Process { - Process { - command, - cwd, - env, - } + Process { command, cwd, env } } pub fn command>(cmd: Vec) -> Process { @@ -27,7 +23,10 @@ impl Process { .current_dir(self.cwd.clone().unwrap_or(".".to_string())) .envs(self.env.iter().map(|v| { let mut split = v.split('='); - (split.next().unwrap().to_string(), split.next().unwrap().to_string()) + ( + split.next().unwrap().to_string(), + split.next().unwrap().to_string(), + ) })) .stdout(std::process::Stdio::piped()) .stderr(std::process::Stdio::piped()) @@ -41,4 +40,4 @@ impl Default for Process { fn default() -> Process { Process::new(vec![], None, vec![]) } -} \ No newline at end of file +} diff --git a/pkgr/src/process/output.rs b/pkgr/src/process/output.rs index 09e788e..68d91fd 100644 --- a/pkgr/src/process/output.rs +++ b/pkgr/src/process/output.rs @@ -1,10 +1,10 @@ // create functions that spawn threads for Stdout and Stderr // that print the output to info!(target: "command:stdout", ...) and info!(target: "command:stderr", ...) respectively -use std::process::Child; use std::io::{BufRead, BufReader}; +use std::process::Child; -use log::{info, error}; +use log::{error, info}; pub fn spawn_output_handlers(child: &mut Child) { let stdout = child.stdout.take().unwrap(); @@ -23,4 +23,4 @@ pub fn spawn_output_handlers(child: &mut Child) { error!(target: "command:stderr", "{}", line.unwrap()); } }); -} \ No newline at end of file +} diff --git a/pkgr/src/tmpfs.rs b/pkgr/src/tmpfs.rs index 509d3f7..c9ea6f8 100644 --- a/pkgr/src/tmpfs.rs +++ b/pkgr/src/tmpfs.rs @@ -9,9 +9,7 @@ impl TempDir { if !path.exists() { std::fs::create_dir_all(&path).unwrap(); } - TempDir { - path - } + TempDir { path } } pub fn push>(&mut self, path: S) { @@ -45,4 +43,4 @@ impl ToString for TempDir { fn to_string(&self) -> String { self.path.to_str().unwrap().to_string() } -} \ No newline at end of file +}