From 91081cb86a0e0696956e54b3220a757867ad043e Mon Sep 17 00:00:00 2001 From: veeso Date: Sat, 3 Apr 2021 16:33:18 +0200 Subject: [PATCH] Use thiserror to format error messages --- Cargo.toml | 3 +-- src/bookmarks/mod.rs | 17 +++++++---------- src/config/mod.rs | 17 +++++++---------- src/filetransfer/mod.rs | 35 ++++++++++++++++------------------- 4 files changed, 31 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7c73626..8f30fbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ readme = "README.md" repository = "https://github.com/veeso/termscp" version = "0.4.1" -[metadata] [metadata.rpm] package = "termscp" @@ -23,6 +22,7 @@ buildflags = ["--release"] [metadata.rpm.targets] [metadata.rpm.targets.termscp] path = "/usr/bin/termscp" + [[bin]] name = "termscp" path = "src/main.rs" @@ -70,7 +70,6 @@ version = "2.0.2" [features] githubActions = [] -[target] [target."cfg(any(target_os = \"unix\", target_os = \"macos\", target_os = \"linux\"))"] [target."cfg(any(target_os = \"unix\", target_os = \"macos\", target_os = \"linux\"))".dependencies] users = "0.11.0" diff --git a/src/bookmarks/mod.rs b/src/bookmarks/mod.rs index f593ee0..77c0c78 100644 --- a/src/bookmarks/mod.rs +++ b/src/bookmarks/mod.rs @@ -29,6 +29,7 @@ pub mod serializer; use serde::{Deserialize, Serialize}; use std::collections::HashMap; +use thiserror::Error; #[derive(Deserialize, Serialize, std::fmt::Debug)] /// ## UserHosts @@ -66,10 +67,13 @@ pub struct SerializerError { /// ## SerializerErrorKind /// /// Describes the kind of error for the serializer/deserializer -#[derive(std::fmt::Debug, PartialEq)] +#[derive(Error, Debug)] pub enum SerializerErrorKind { + #[error("IO error")] IoError, + #[error("Serialization error")] SerializationError, + #[error("Syntax error")] SyntaxError, } @@ -102,14 +106,9 @@ impl SerializerError { impl std::fmt::Display for SerializerError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - let err: String = match &self.kind { - SerializerErrorKind::IoError => String::from("IO error"), - SerializerErrorKind::SerializationError => String::from("Serialization error"), - SerializerErrorKind::SyntaxError => String::from("Syntax error"), - }; match &self.msg { - Some(msg) => write!(f, "{} ({})", err, msg), - None => write!(f, "{}", err), + Some(msg) => write!(f, "{} ({})", self.kind, msg), + None => write!(f, "{}", self.kind), } } } @@ -172,12 +171,10 @@ mod tests { #[test] fn test_bookmarks_bookmark_errors() { let error: SerializerError = SerializerError::new(SerializerErrorKind::SyntaxError); - assert_eq!(error.kind, SerializerErrorKind::SyntaxError); assert!(error.msg.is_none()); assert_eq!(format!("{}", error), String::from("Syntax error")); let error: SerializerError = SerializerError::new_ex(SerializerErrorKind::SyntaxError, String::from("bad syntax")); - assert_eq!(error.kind, SerializerErrorKind::SyntaxError); assert!(error.msg.is_some()); assert_eq!( format!("{}", error), diff --git a/src/config/mod.rs b/src/config/mod.rs index 4c97aa1..f4fa64d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -38,6 +38,7 @@ use crate::filetransfer::FileTransferProtocol; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::path::PathBuf; +use thiserror::Error; #[derive(Deserialize, Serialize, std::fmt::Debug)] /// ## UserConfig @@ -117,10 +118,13 @@ pub struct SerializerError { /// ## SerializerErrorKind /// /// Describes the kind of error for the serializer/deserializer -#[derive(std::fmt::Debug, PartialEq)] +#[derive(Error, Debug)] pub enum SerializerErrorKind { + #[error("IO error")] IoError, + #[error("Serialization error")] SerializationError, + #[error("Syntax error")] SyntaxError, } @@ -144,14 +148,9 @@ impl SerializerError { impl std::fmt::Display for SerializerError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - let err: String = match &self.kind { - SerializerErrorKind::IoError => String::from("IO error"), - SerializerErrorKind::SerializationError => String::from("Serialization error"), - SerializerErrorKind::SyntaxError => String::from("Syntax error"), - }; match &self.msg { - Some(msg) => write!(f, "{} ({})", err, msg), - None => write!(f, "{}", err), + Some(msg) => write!(f, "{} ({})", self.kind, msg), + None => write!(f, "{}", self.kind), } } } @@ -214,12 +213,10 @@ mod tests { #[test] fn test_config_mod_errors() { let error: SerializerError = SerializerError::new(SerializerErrorKind::SyntaxError); - assert_eq!(error.kind, SerializerErrorKind::SyntaxError); assert!(error.msg.is_none()); assert_eq!(format!("{}", error), String::from("Syntax error")); let error: SerializerError = SerializerError::new_ex(SerializerErrorKind::SyntaxError, String::from("bad syntax")); - assert_eq!(error.kind, SerializerErrorKind::SyntaxError); assert!(error.msg.is_some()); assert_eq!( format!("{}", error), diff --git a/src/filetransfer/mod.rs b/src/filetransfer/mod.rs index 3d6525d..b07cb76 100644 --- a/src/filetransfer/mod.rs +++ b/src/filetransfer/mod.rs @@ -32,6 +32,7 @@ use crate::fs::{FsEntry, FsFile}; // ext use std::io::{Read, Write}; use std::path::{Path, PathBuf}; +use thiserror::Error; use wildmatch::WildMatch; // exports pub mod ftp_transfer; @@ -62,19 +63,31 @@ pub struct FileTransferError { /// /// FileTransferErrorType defines the possible errors available for a file transfer #[allow(dead_code)] -#[derive(std::fmt::Debug)] +#[derive(Error, Debug)] pub enum FileTransferErrorType { + #[error("Authentication failed")] AuthenticationFailed, + #[error("Bad address syntax")] BadAddress, + #[error("Connection error")] ConnectionError, + #[error("SSL error")] SslError, + #[error("Could not stat directory")] DirStatFailed, + #[error("Failed to create file")] FileCreateDenied, + #[error("IO error: {0}")] IoErr(std::io::Error), + #[error("No such file or directory")] NoSuchFileOrDirectory, + #[error("Not enough permissions")] PexError, + #[error("Protocol error")] ProtocolError, + #[error("Uninitialized session")] UninitializedSession, + #[error("Unsupported feature")] UnsupportedFeature, } @@ -98,25 +111,9 @@ impl FileTransferError { impl std::fmt::Display for FileTransferError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - let err: String = match &self.code { - FileTransferErrorType::AuthenticationFailed => String::from("Authentication failed"), - FileTransferErrorType::BadAddress => String::from("Bad address syntax"), - FileTransferErrorType::ConnectionError => String::from("Connection error"), - FileTransferErrorType::DirStatFailed => String::from("Could not stat directory"), - FileTransferErrorType::FileCreateDenied => String::from("Failed to create file"), - FileTransferErrorType::IoErr(err) => format!("IO error: {}", err), - FileTransferErrorType::NoSuchFileOrDirectory => { - String::from("No such file or directory") - } - FileTransferErrorType::PexError => String::from("Not enough permissions"), - FileTransferErrorType::ProtocolError => String::from("Protocol error"), - FileTransferErrorType::SslError => String::from("SSL error"), - FileTransferErrorType::UninitializedSession => String::from("Uninitialized session"), - FileTransferErrorType::UnsupportedFeature => String::from("Unsupported feature"), - }; match &self.msg { - Some(msg) => write!(f, "{} ({})", err, msg), - None => write!(f, "{}", err), + Some(msg) => write!(f, "{} ({})", self.code, msg), + None => write!(f, "{}", self.code), } } }