summaryrefslogtreecommitdiffstats
path: root/src/error
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2021-05-02 13:45:44 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2021-05-02 13:45:44 -0400
commit431d6a1f08b0ea9507e78a22d25ab8a73aa136d1 (patch)
treede1a60da812b8af964fcdc3bb619578d634ce431 /src/error
parente1975f67ae9e680818327d5c9257931b29d7ce4d (diff)
rework error types
- this should increase the amount of ? operators we can use
Diffstat (limited to 'src/error')
-rw-r--r--src/error/error.rs67
-rw-r--r--src/error/error_kind.rs33
-rw-r--r--src/error/mod.rs7
3 files changed, 107 insertions, 0 deletions
diff --git a/src/error/error.rs b/src/error/error.rs
new file mode 100644
index 0000000..f0f89ea
--- /dev/null
+++ b/src/error/error.rs
@@ -0,0 +1,67 @@
+use std::io;
+
+use super::JoshutoErrorKind;
+
+pub struct JoshutoError {
+ _kind: JoshutoErrorKind,
+ _cause: String,
+}
+
+#[allow(dead_code)]
+impl JoshutoError {
+ pub fn new(_kind: JoshutoErrorKind, _cause: String) -> Self {
+ Self { _kind, _cause }
+ }
+
+ pub fn kind(&self) -> JoshutoErrorKind {
+ self._kind
+ }
+}
+
+impl std::fmt::Display for JoshutoError {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ write!(f, "{}", self._cause)
+ }
+}
+
+impl std::convert::From<io::Error> for JoshutoError {
+ fn from(err: io::Error) -> Self {
+ Self {
+ _kind: JoshutoErrorKind::from(err.kind()),
+ _cause: err.to_string(),
+ }
+ }
+}
+
+impl std::convert::From<globset::Error> for JoshutoError {
+ fn from(err: globset::Error) -> Self {
+ Self {
+ _kind: JoshutoErrorKind::from(err.kind()),
+ _cause: err.to_string(),
+ }
+ }
+}
+
+impl std::convert::From<trash::Error> for JoshutoError {
+ fn from(err: trash::Error) -> Self {
+ let err = match err {
+ trash::Error::Unknown => std::io::Error::new(std::io::ErrorKind::Other, "Unknown Error"),
+ trash::Error::TargetedRoot => {
+ std::io::Error::new(std::io::ErrorKind::Other, "Targeted Root")
+ }
+ trash::Error::CanonicalizePath { code: _ } => {
+ std::io::Error::new(std::io::ErrorKind::NotFound, "Not found")
+ }
+ trash::Error::Remove { code: Some(1) } => std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ "Cannot move files to trash from mounted system",
+ ),
+ _ => std::io::Error::new(std::io::ErrorKind::Other, "Unknown Error"),
+ };
+ Self {
+ _kind: JoshutoErrorKind::from(err.kind()),
+ _cause: err.to_string(),
+ }
+ }
+}
+
diff --git a/src/error/error_kind.rs b/src/error/error_kind.rs
new file mode 100644
index 0000000..b5aa835
--- /dev/null
+++ b/src/error/error_kind.rs
@@ -0,0 +1,33 @@
+use std::io;
+
+#[derive(Copy, Clone, Debug)]
+pub enum JoshutoErrorKind {
+ // io related
+ Io(io::ErrorKind),
+
+ // environment variable not found
+ EnvVarNotPresent,
+
+ // parse error
+ ParseError,
+ ClipboardError,
+
+ Glob,
+
+ InvalidParameters,
+
+ UnrecognizedArgument,
+ UnrecognizedCommand,
+}
+
+impl std::convert::From<io::ErrorKind> for JoshutoErrorKind {
+ fn from(err: io::ErrorKind) -> Self {
+ Self::Io(err)
+ }
+}
+
+impl std::convert::From<&globset::ErrorKind> for JoshutoErrorKind {
+ fn from(_: &globset::ErrorKind) -> Self {
+ Self::Glob
+ }
+}
diff --git a/src/error/mod.rs b/src/error/mod.rs
new file mode 100644
index 0000000..f00a527
--- /dev/null
+++ b/src/error/mod.rs
@@ -0,0 +1,7 @@
+mod error;
+mod error_kind;
+
+pub use self::error::JoshutoError;
+pub use self::error_kind::JoshutoErrorKind;
+
+pub type JoshutoResult<T> = Result<T, JoshutoError>;