lunar-mining 4 лет назад
Родитель
Сommit
ba7a5cc21b
2 измененных файлов с 68 добавлено и 29 удалено
  1. 15 0
      example/dchat/src/error.rs
  2. 53 29
      example/dchat/src/main.rs

+ 15 - 0
example/dchat/src/error.rs

@@ -0,0 +1,15 @@
+use std::{error, fmt};
+
+pub type Error = Box<dyn error::Error>;
+pub type Result<T> = std::result::Result<T, Error>;
+
+#[derive(Debug, Clone)]
+pub struct MissingSpecifier;
+
+impl fmt::Display for MissingSpecifier {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "missing node specifier")
+    }
+}
+
+impl error::Error for MissingSpecifier {}

+ 53 - 29
example/dchat/src/main.rs

@@ -1,28 +1,32 @@
 use async_executor::Executor;
 use async_executor::Executor;
 use async_std::sync::{Arc, Mutex};
 use async_std::sync::{Arc, Mutex};
 use easy_parallel::Parallel;
 use easy_parallel::Parallel;
-use log::debug;
-use simplelog::WriteLogger;
+
 use std::{
 use std::{
     fs::File,
     fs::File,
     io::{stdin, stdout, Read, Write},
     io::{stdin, stdout, Read, Write},
 };
 };
-use termion::{event::Key, input::TermRead, raw::IntoRawMode};
+
+use log::debug;
+use simplelog::WriteLogger;
 use url::Url;
 use url::Url;
 
 
+use termion::{event::Key, input::TermRead, raw::IntoRawMode};
+
 use darkfi::{
 use darkfi::{
     net,
     net,
     net::Settings,
     net::Settings,
     util::cli::{get_log_config, get_log_level},
     util::cli::{get_log_config, get_log_level},
-    Error, Result,
 };
 };
 
 
 use crate::{
 use crate::{
     dchatmsg::{Dchatmsg, DchatmsgsBuffer},
     dchatmsg::{Dchatmsg, DchatmsgsBuffer},
+    error::{Error, MissingSpecifier, Result},
     protocol_dchat::ProtocolDchat,
     protocol_dchat::ProtocolDchat,
 };
 };
 
 
 pub mod dchatmsg;
 pub mod dchatmsg;
+pub mod error;
 pub mod protocol_dchat;
 pub mod protocol_dchat;
 
 
 struct Dchat {
 struct Dchat {
@@ -32,12 +36,12 @@ struct Dchat {
     display: DisplayMode,
     display: DisplayMode,
 }
 }
 
 
-#[derive(Debug)]
 enum DisplayMode {
 enum DisplayMode {
     Normal,
     Normal,
     Editing,
     Editing,
     Inbox,
     Inbox,
     MessageSent,
     MessageSent,
+    SendFailed(Error),
 }
 }
 
 
 impl Dchat {
 impl Dchat {
@@ -59,7 +63,7 @@ impl Dchat {
         loop {
         loop {
             self.render().await?;
             self.render().await?;
             for k in stdin.by_ref().keys() {
             for k in stdin.by_ref().keys() {
-                match self.display {
+                match &self.display {
                     DisplayMode::Normal => match k.unwrap() {
                     DisplayMode::Normal => match k.unwrap() {
                         Key::Char('q') => return Ok(()),
                         Key::Char('q') => return Ok(()),
                         Key::Char('i') => {
                         Key::Char('i') => {
@@ -73,29 +77,28 @@ impl Dchat {
                         }
                         }
                         _ => {}
                         _ => {}
                     },
                     },
-                    DisplayMode::Editing => {
-                        match k.unwrap() {
-                            Key::Char('q') => return Ok(()),
-                            Key::Char('\n') => {
-                                match self.send().await {
-                                    Ok(_) => {
-                                        self.display = DisplayMode::MessageSent;
-                                    }
-                                    // TODO
-                                    Err(_) => {}
+                    DisplayMode::Editing => match k.unwrap() {
+                        Key::Char('q') => return Ok(()),
+                        Key::Char('\n') => {
+                            match self.send().await {
+                                Ok(_) => {
+                                    self.display = DisplayMode::MessageSent;
+                                }
+                                Err(e) => {
+                                    self.display = DisplayMode::SendFailed(e);
                                 }
                                 }
-                                break
-                            }
-                            Key::Char(c) => {
-                                self.input.push(c);
-                            }
-                            Key::Esc => {
-                                self.display = DisplayMode::Normal;
-                                break
                             }
                             }
-                            _ => {}
+                            break
                         }
                         }
-                    }
+                        Key::Char(c) => {
+                            self.input.push(c);
+                        }
+                        Key::Esc => {
+                            self.display = DisplayMode::Normal;
+                            break
+                        }
+                        _ => {}
+                    },
                     DisplayMode::MessageSent => match k.unwrap() {
                     DisplayMode::MessageSent => match k.unwrap() {
                         Key::Char('q') => return Ok(()),
                         Key::Char('q') => return Ok(()),
                         Key::Esc => {
                         Key::Esc => {
@@ -108,6 +111,14 @@ impl Dchat {
                         Key::Char('q') => return Ok(()),
                         Key::Char('q') => return Ok(()),
                         _ => {}
                         _ => {}
                     },
                     },
+                    DisplayMode::SendFailed(_) => match k.unwrap() {
+                        Key::Char('q') => return Ok(()),
+                        Key::Esc => {
+                            self.display = DisplayMode::Normal;
+                            break
+                        }
+                        _ => {}
+                    },
                 }
                 }
             }
             }
             stdout.flush()?;
             stdout.flush()?;
@@ -119,7 +130,7 @@ impl Dchat {
         let stdout = stdout();
         let stdout = stdout();
         let mut stdout = stdout.lock().into_raw_mode().unwrap();
         let mut stdout = stdout.lock().into_raw_mode().unwrap();
 
 
-        match self.display {
+        match &self.display {
             DisplayMode::Normal => {
             DisplayMode::Normal => {
                 write!(
                 write!(
                     stdout,
                     stdout,
@@ -183,6 +194,19 @@ impl Dchat {
                 )?;
                 )?;
                 stdout.flush()?;
                 stdout.flush()?;
             }
             }
+            DisplayMode::SendFailed(e) => {
+                write!(
+                    stdout,
+                    "{}{}{}send message failed! reason: {} {} esc: return to main menu {}",
+                    termion::clear::All,
+                    termion::style::Bold,
+                    termion::cursor::Goto(1, 2),
+                    e,
+                    termion::cursor::Goto(1, 3),
+                    termion::cursor::Goto(1, 4),
+                )?;
+                stdout.flush()?;
+            }
         }
         }
 
 
         Ok(())
         Ok(())
@@ -284,12 +308,12 @@ async fn main() -> Result<()> {
             "b" => bob(),
             "b" => bob(),
             _ => {
             _ => {
                 println!("you must specify either a or b");
                 println!("you must specify either a or b");
-                Err(Error::ConfigInvalid)
+                Err(MissingSpecifier.into())
             }
             }
         },
         },
         None => {
         None => {
             println!("you must specify either a or b");
             println!("you must specify either a or b");
-            Err(Error::ConfigInvalid)
+            Err(MissingSpecifier.into())
         }
         }
     };
     };