From 318c59d4e817a1ac2be6104868a5e81996085b91 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Wed, 3 Jan 2024 10:55:17 +0000 Subject: [PATCH] Move TopLevelError up to the root of the crate. If that's not a candidate for putting at the top level then I don't know what is. --- src/auth.rs | 2 ++ src/client.rs | 2 ++ src/config.rs | 2 ++ src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 42 +++--------------------------------------- src/tui.rs | 2 ++ 6 files changed, 54 insertions(+), 39 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 2983e1c..d65ad7f 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -8,6 +8,8 @@ pub enum AuthError { Bad(String), } +impl super::TopLevelErrorCandidate for AuthError {} + impl std::fmt::Display for AuthError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> diff --git a/src/client.rs b/src/client.rs index 4b7d4de..b9e0589 100644 --- a/src/client.rs +++ b/src/client.rs @@ -67,6 +67,8 @@ pub enum ClientError { UrlError(String, String), // url, message } +impl super::TopLevelErrorCandidate for ClientError {} + impl From for ClientError { fn from(err: AuthError) -> Self { ClientError::Auth(err) } } diff --git a/src/config.rs b/src/config.rs index 100c853..9d047aa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,6 +12,8 @@ pub enum ConfigError { Env(std::env::VarError), } +impl super::TopLevelErrorCandidate for ConfigError {} + impl std::fmt::Display for ConfigError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> diff --git a/src/lib.rs b/src/lib.rs index 5cf1fa8..33a8d90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,3 +13,46 @@ pub mod menu; pub mod file; pub mod editor; pub mod posting; + +#[derive(Debug)] +pub struct TopLevelError { + prefix: String, + message: String, +} + +impl TopLevelError { + fn new(prefix: &str, message: &str) -> Self { + TopLevelError { + prefix: prefix.to_owned(), + message: message.to_owned(), + } + } +} + +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 eb5334e..b07d56c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ use clap::Parser; -use std::fmt::Display; use std::process::ExitCode; -use mastodonochrome::client::ClientError; -use mastodonochrome::config::{ConfigLocation, ConfigError}; -use mastodonochrome::tui::{Tui, TuiError}; +use mastodonochrome::TopLevelError; +use mastodonochrome::config::ConfigLocation; +use mastodonochrome::tui::Tui; use mastodonochrome::login::login; #[derive(Parser, Debug)] @@ -23,41 +22,6 @@ struct Args { login: Option, } -#[derive(Debug)] -pub struct TopLevelError { - prefix: String, - message: String, -} - -impl Display for TopLevelError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> - Result<(), std::fmt::Error> - { - write!(f, "mastodonochrome: {}{}", self.prefix, self.message) - } -} - -trait TopLevelErrorCandidate: 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 ConfigError {} -impl TopLevelErrorCandidate for TuiError {} -impl TopLevelErrorCandidate for ClientError {} -impl TopLevelErrorCandidate for clap::error::Error { - // clap prints its own "error: " - fn get_prefix() -> String { "".to_owned() } -} - fn main_inner() -> Result<(), TopLevelError> { let cli = Args::try_parse()?; let cfgloc = match cli.config { diff --git a/src/tui.rs b/src/tui.rs index f496ee8..c9c79fb 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -164,6 +164,8 @@ impl TuiError { } } +impl super::TopLevelErrorCandidate for TuiError {} + impl From for TuiError { fn from(err: std::io::Error) -> Self { TuiError { -- 2.30.2