chiark / gitweb /
Make a Client and put it in the Tui.
authorSimon Tatham <anakin@pobox.com>
Fri, 29 Dec 2023 11:08:52 +0000 (11:08 +0000)
committerSimon Tatham <anakin@pobox.com>
Fri, 29 Dec 2023 11:08:52 +0000 (11:08 +0000)
This involved souping up the error handling so that we can absorb
errors from various different sources during Tui construction.

src/auth.rs
src/tui.rs

index 1e0ea7e53328e3df6293ac2f783b0e1c4a4cd7be..3cfae9774e16892c46ba5d16faa9c72dd28826df 100644 (file)
@@ -7,6 +7,21 @@ pub enum AuthError {
     Bad(String),
 }
 
+impl std::fmt::Display for AuthError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) ->
+        Result<(), std::fmt::Error>
+    {
+        match self {
+            AuthError::Nonexistent(_) => {
+                write!(f, "not logged in")
+            },
+            AuthError::Bad(ref msg) => {
+                write!(f, "unable to read authentication: {}", msg)
+            },
+        }
+    }
+}
+
 #[derive(Serialize, Deserialize, Debug)]
 pub struct AuthConfig {
     pub account_id: String,
@@ -21,7 +36,7 @@ pub struct AuthConfig {
 impl AuthConfig {
     pub fn load() -> Result<Self, AuthError> {
         let xdg_dirs = match BaseDirectories::with_prefix("mastodonochrome") {
-            Err(e) => Err(AuthError::Nonexistent(
+            Err(e) => Err(AuthError::Bad(
                 format!("unable to get config directory: {}", e))),
             Ok(d) => Ok(d),
         }?;
index d1ef5124cb26dff8207771aa66dc4f92266dc57c..e3177570daee704fc733a86bf6147feba42c9ed6 100644 (file)
@@ -14,9 +14,10 @@ use std::io::{Stdout, Write, stdout};
 use unicode_width::UnicodeWidthStr;
 
 use super::activity_stack::*;
-//use super::client::Client;
+use super::client::Client;
 use super::coloured_string::{ColouredString, ColouredStringSlice};
 use super::menu::*;
+use super::auth::AuthError;
 
 fn ratatui_style_from_colour(colour: char) -> Style {
     match colour {
@@ -130,7 +131,7 @@ pub struct Tui {
     subthread_sender: std::sync::mpsc::SyncSender<SubthreadEvent>,
     subthread_receiver: std::sync::mpsc::Receiver<SubthreadEvent>,
     state: TuiLogicalState,
-    // FIXME: we'll need a Client here
+    client: Client,
 }
 
 #[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
@@ -143,8 +144,43 @@ pub enum OurKey {
     PgUp, PgDn, Home, End, Ins, Del,
 }
 
+pub struct TuiError {
+    message: String,
+}
+
+impl TuiError {
+    pub fn new(message: &str) -> Self {
+        TuiError {
+            message: message.to_owned(),
+        }
+    }
+}
+
+impl From<std::io::Error> for TuiError {
+    fn from(err: std::io::Error) -> Self {
+        TuiError {
+            message: err.to_string(),
+        }
+    }
+}
+impl From<AuthError> for TuiError {
+    fn from(err: AuthError) -> Self {
+        TuiError {
+            message: err.to_string(),
+        }
+    }
+}
+
+impl std::fmt::Display for TuiError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) ->
+        Result<(), std::fmt::Error>
+    {
+        write!(f, "unable to read authentication: {}", self.message)
+    }
+}
+
 impl Tui {
-    pub fn run() -> std::io::Result<()> {
+    pub fn run() -> Result<(), TuiError> {
         stdout().execute(EnterAlternateScreen)?;
         enable_raw_mode()?;
         let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
@@ -163,11 +199,14 @@ impl Tui {
             }
         });
 
+        let client = Client::new()?;
+
         let mut tui = Tui {
             terminal: terminal,
             subthread_sender: sender,
             subthread_receiver: receiver,
             state: TuiLogicalState::new(),
+            client: client,
         };
         let result = tui.main_loop();
 
@@ -226,7 +265,7 @@ impl Tui {
         }
     }
 
-    fn main_loop(&mut self) -> std::io::Result<()> {
+    fn main_loop(&mut self) -> Result<(), TuiError> {
         'outer: loop {
             self.terminal.draw(|frame| {
                 let area = frame.size();