chiark / gitweb /
Move TopLevelError up to the root of the crate.
authorSimon Tatham <anakin@pobox.com>
Wed, 3 Jan 2024 10:55:17 +0000 (10:55 +0000)
committerSimon Tatham <anakin@pobox.com>
Wed, 3 Jan 2024 10:55:17 +0000 (10:55 +0000)
If that's not a candidate for putting at the top level then I don't
know what is.

src/auth.rs
src/client.rs
src/config.rs
src/lib.rs
src/main.rs
src/tui.rs

index 2983e1cd21819f4c2fd1548eb9adb9cceca3c5bb..d65ad7f53e5fd63eece3115ea3989188b34d497d 100644 (file)
@@ -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>
index 4b7d4deca010cd47b10dcbfe0d8a1cf14605cb7b..b9e05895572091990edc08ec43d209a33cd73c0d 100644 (file)
@@ -67,6 +67,8 @@ pub enum ClientError {
     UrlError(String, String), // url, message
 }
 
+impl super::TopLevelErrorCandidate for ClientError {}
+
 impl From<AuthError> for ClientError {
     fn from(err: AuthError) -> Self { ClientError::Auth(err) }
 }
index 100c853c0a31be0c8bea8326f8505e5ee78b2cf3..9d047aa94f004c915a65f7a2d01f0192b9d2432b 100644 (file)
@@ -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>
index 5cf1fa8db099481d4101c9e2fbcbeece1fff07fe..33a8d90449c672d241f2d140e76ffbfc6224004f 100644 (file)
@@ -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<E: TopLevelErrorCandidate> From<E> 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 {}
index eb5334eab2d58a514f3c103cee1d5c470319a866..b07d56c2b905b4c292571615f6dd32c9a8f52734 100644 (file)
@@ -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<String>,
 }
 
-#[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<E: TopLevelErrorCandidate> From<E> 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 {
index f496ee83043c59a07a9ee4414b5928affe4fcbe7..c9c79fb4e5bff4b2291fab8d13bc413d99e62e5e 100644 (file)
@@ -164,6 +164,8 @@ impl TuiError {
     }
 }
 
+impl super::TopLevelErrorCandidate for TuiError {}
+
 impl From<std::io::Error> for TuiError {
     fn from(err: std::io::Error) -> Self {
         TuiError {