From 6769376ae26c7885d944e0b5c03fccd219f184b3 Mon Sep 17 00:00:00 2001 From: Zykino Date: Sat, 28 Jan 2023 22:24:39 +0100 Subject: [PATCH] Better errors in summary --- src/command.rs | 31 +++++++++++++++++-------------- src/errors.rs | 30 +++++++++++++++++------------- src/lib.rs | 49 +++++++++++++++++++++++++++---------------------- src/main.rs | 2 +- 4 files changed, 62 insertions(+), 50 deletions(-) diff --git a/src/command.rs b/src/command.rs index ba3c6e2..ae3a801 100644 --- a/src/command.rs +++ b/src/command.rs @@ -109,7 +109,7 @@ impl Updater { // self.systems.iter().collect(); let mut systems: Vec<_> = vec![]; for sys in &self.systems { - systems.push(self.update(&sys, opt)); + systems.push(self.update(&sys, opt).into()); } Summary { systems } } @@ -145,10 +145,10 @@ impl System { let cmd = fetch.clone().prepare(opt); let exit_status = cmd .execute(opt) - .map_err(|err| MyError::new(ErrorKind::Fetch, err, cmd.clone()))?; + .map_err(|err| MyError::new(MyErrorKind::Fetch, err, cmd.clone()))?; if !exit_status.success() { return Err(MyError::new( - ErrorKind::Fetch, + MyErrorKind::Fetch, io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), cmd.clone(), )); @@ -162,10 +162,10 @@ impl System { let cmd = compile.clone().prepare(opt); let exit_status = cmd .execute(opt) - .map_err(|err| MyError::new(ErrorKind::Compile, err, cmd.clone()))?; + .map_err(|err| MyError::new(MyErrorKind::Compile, err, cmd.clone()))?; if !exit_status.success() { return Err(MyError::new( - ErrorKind::Fetch, + MyErrorKind::Fetch, io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), cmd.clone(), )); @@ -178,10 +178,10 @@ impl System { let cmd = self.install.clone().prepare(opt); let exit_status = cmd .execute(opt) - .map_err(|err| MyError::new(ErrorKind::Install, err, cmd.clone()))?; + .map_err(|err| MyError::new(MyErrorKind::Install, err, cmd.clone()))?; if !exit_status.success() { return Err(MyError::new( - ErrorKind::Fetch, + MyErrorKind::Fetch, io::Error::new(io::ErrorKind::Other, format!("{}", exit_status)), cmd.clone(), )); @@ -201,7 +201,7 @@ impl Cmd { } fn prepare(self, opt: &Opt) -> ActualCmd { - let mut params = self.params; + let params = self.params; let mut env = self.env; if !env.contains_key("PATH") { @@ -254,11 +254,14 @@ impl fmt::Display for ActualCmd { write!(f, " in {:?}", cdir)?; } - if !self.env.is_empty() { - writeln!(f, " with the following environment variable:")?; - writeln!(f, "{:#?}", self.env) - } else { - write!(f, " without any environment variable. ") - } + Ok(()) + + // // TODO: remove me (too verbose) + // if !self.env.is_empty() { + // writeln!(f, " with the following environment variable:")?; + // writeln!(f, "{:#?}", self.env) + // } else { + // write!(f, " without any environment variable. ") + // } } } diff --git a/src/errors.rs b/src/errors.rs index 9e89991..6a1b7aa 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,8 +1,12 @@ use crate::*; use std::error::Error; -use std::fmt::{Display, Formatter, Result}; +use std::fmt; +use std::fmt::{Display, Formatter}; use std::io; +use std::result; + +pub type Result = result::Result; /// An error that can occur in this crate. /// @@ -10,13 +14,13 @@ use std::io; #[derive(Debug)] pub struct MyError { source: io::Error, - kind: ErrorKind, + kind: MyErrorKind, cmd: ActualCmd, } #[derive(Debug)] #[non_exhaustive] -pub enum ErrorKind { +pub enum MyErrorKind { Config, Fetch, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? Compile, // TODO: merge into "Update" or "Command" type of error? => Have this as an other level of error? @@ -24,12 +28,12 @@ pub enum ErrorKind { } impl MyError { - pub(crate) fn new(kind: ErrorKind, source: io::Error, cmd: ActualCmd) -> MyError { + pub(crate) fn new(kind: MyErrorKind, source: io::Error, cmd: ActualCmd) -> MyError { MyError { source, kind, cmd } } /// Return the kind of this error. - pub fn kind(&self) -> &ErrorKind { + pub fn kind(&self) -> &MyErrorKind { &self.kind } } @@ -41,28 +45,28 @@ impl Error for MyError { } impl Display for MyError { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self.kind { - ErrorKind::Config => write!( + MyErrorKind::Config => write!( f, "Could not read configuration file: {}", self.source().unwrap() ), - ErrorKind::Fetch => write!( + MyErrorKind::Fetch => write!( f, - "Could not fetch with command `{}`: {}", + "Could not fetch with command {}: {}", self.cmd, self.source().unwrap() ), - ErrorKind::Compile => write!( + MyErrorKind::Compile => write!( f, - "Could not compile with command `{}`: {}", + "Could not compile with command {}: {}", self.cmd, self.source().unwrap() ), - ErrorKind::Install => write!( + MyErrorKind::Install => write!( f, - "Could not install with command `{}`: {}", + "Could not install with command {}: {}", self.cmd, self.source().unwrap() ), diff --git a/src/lib.rs b/src/lib.rs index 688ee36..56d6a13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,9 +7,6 @@ use errors::*; use clap::Parser; use std::fmt::Display; use std::path::PathBuf; -use std::result; - -pub type Result = result::Result; #[derive(Parser)] pub struct Opt { @@ -31,27 +28,36 @@ pub struct Opt { pub steps: Vec, } -// enum State { -// /// All steps asked were successful -// Ok, -// Err(MyError), -// } +enum State { + /// All steps asked were successful + Ok, + Err(MyError), +} -// impl Display for State { -// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -// match self { -// State::Ok => write!(f, "Ok"), -// State::Err(e) => write!(f, "Error: {}", e), -// // State::Fetch(e) => write!(f, "Fetch error: {}", e), -// // State::Compile(e) => write!(f, "Compile error: {}", e), -// // State::Install(e) => write!(f, "Install error: {}", e), -// } -// } -// } +impl Display for State { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + State::Ok => write!(f, "Ok"), + State::Err(e) => write!(f, "Error: {}", e), + // State::Fetch(e) => write!(f, "Fetch error: {}", e), + // State::Compile(e) => write!(f, "Compile error: {}", e), + // State::Install(e) => write!(f, "Install error: {}", e), + } + } +} + +impl From> for State { + fn from(value: Result) -> Self { + match value { + Ok(_) => State::Ok, + Err(e) => State::Err(e), + } + } +} pub struct Summary { // TODO: Go back to vectors to keep order? - systems: Vec>, + systems: Vec, } impl Display for Summary { @@ -59,9 +65,8 @@ impl Display for Summary { writeln!(f, "Summary:")?; for system in &self.systems { // TODO: also print version before/after, update time - // writeln!(f, "\t{}\t{}", system.0, system.1)?; - writeln!(f, "\t{:?}", system)?; + writeln!(f, "\t{}", system)?; } Ok(()) } diff --git a/src/main.rs b/src/main.rs index 3a9601d..3222b1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use clap::Parser; use system_updater::*; fn main() { - let mut opt = Opt::parse(); + let opt = Opt::parse(); let summary = run(&opt);