From: Simon Tatham Date: Thu, 1 Feb 2024 08:06:15 +0000 (+0000) Subject: Move TopLevelError out into its own source file. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=5fbe4fcf861e2c38c61e4698e5db340806114f11;p=mastodonochrome.git Move TopLevelError out into its own source file. Now lib.rs is just a list of declarations of other modules, which seems cleaner. --- diff --git a/src/auth.rs b/src/auth.rs index 69a6dca..578d655 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -9,7 +9,7 @@ pub enum AuthError { SaveFailed(String), } -impl super::TopLevelErrorCandidate for AuthError {} +impl super::top_level_error::TopLevelErrorCandidate for AuthError {} impl std::fmt::Display for AuthError { fn fmt( diff --git a/src/client.rs b/src/client.rs index 558e919..e85008e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -166,7 +166,7 @@ pub enum ClientError { Consistency(String), // just error message } -impl super::TopLevelErrorCandidate for ClientError {} +impl super::top_level_error::TopLevelErrorCandidate for ClientError {} impl From for ClientError { fn from(err: reqwest::Error) -> Self { diff --git a/src/config.rs b/src/config.rs index 3ea64a0..a59b793 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,7 +14,7 @@ pub enum ConfigError { Env(std::env::VarError), } -impl super::TopLevelErrorCandidate for ConfigError {} +impl super::top_level_error::TopLevelErrorCandidate for ConfigError {} impl std::fmt::Display for ConfigError { fn fmt( diff --git a/src/lib.rs b/src/lib.rs index 422ad0a..91110dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,44 +12,6 @@ pub mod options; pub mod posting; pub mod scan_re; pub mod text; +pub mod top_level_error; pub mod tui; pub mod types; - -#[derive(Debug)] -pub struct TopLevelError { - prefix: String, - message: String, -} - -impl std::fmt::Display for TopLevelError { - fn fmt( - &self, - f: &mut std::fmt::Formatter<'_>, - ) -> Result<(), std::fmt::Error> { - write!(f, "mastodonochrome: {}{}", self.prefix, self.message) - } -} - -trait TopLevelErrorCandidate: std::fmt::Display { - fn get_prefix() -> String { - "error: ".to_owned() - } -} - -impl From for TopLevelError { - fn from(err: E) -> Self { - TopLevelError { - prefix: E::get_prefix(), - message: err.to_string(), - } - } -} - -impl TopLevelErrorCandidate for clap::error::Error { - // clap prints its own "error: " - fn get_prefix() -> String { - "".to_owned() - } -} - -impl TopLevelErrorCandidate for std::io::Error {} diff --git a/src/main.rs b/src/main.rs index 9403267..6f2e75b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::process::ExitCode; use mastodonochrome::config::ConfigLocation; use mastodonochrome::tui::Tui; -use mastodonochrome::TopLevelError; +use mastodonochrome::top_level_error::TopLevelError; #[derive(Parser, Debug)] struct Args { diff --git a/src/top_level_error.rs b/src/top_level_error.rs new file mode 100644 index 0000000..e620ae3 --- /dev/null +++ b/src/top_level_error.rs @@ -0,0 +1,38 @@ +#[derive(Debug)] +pub struct TopLevelError { + prefix: String, + message: String, +} + +impl std::fmt::Display for TopLevelError { + fn fmt( + &self, + f: &mut std::fmt::Formatter<'_>, + ) -> Result<(), std::fmt::Error> { + write!(f, "mastodonochrome: {}{}", self.prefix, self.message) + } +} + +pub trait TopLevelErrorCandidate: std::fmt::Display { + fn get_prefix() -> String { + "error: ".to_owned() + } +} + +impl From for TopLevelError { + fn from(err: E) -> Self { + TopLevelError { + prefix: E::get_prefix(), + message: err.to_string(), + } + } +} + +impl TopLevelErrorCandidate for clap::error::Error { + // clap prints its own "error: " + fn get_prefix() -> String { + "".to_owned() + } +} + +impl TopLevelErrorCandidate for std::io::Error {} diff --git a/src/tui.rs b/src/tui.rs index dfeab48..fe1d615 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -219,7 +219,7 @@ impl TuiError { } } -impl super::TopLevelErrorCandidate for TuiError {} +impl super::top_level_error::TopLevelErrorCandidate for TuiError {} impl From for TuiError { fn from(message: String) -> Self {