Skip to content

Commit 1d7af11

Browse files
committed
chore: allow implementing a trait for cmd run error
1 parent ae29f9e commit 1d7af11

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

uai/src/lib.rs

+26-33
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::collections::{HashMap, HashSet};
22
use std::io;
33
use std::io::BufRead;
44
use std::sync::{Arc, Mutex};
5-
use std::{fmt, thread};
5+
use std::thread;
66

7-
pub struct Client<T: Send> {
8-
commands: HashMap<String, Command<T>>,
7+
pub struct Client<T: Send, E: RunError> {
8+
commands: HashMap<String, Command<T, E>>,
99
}
1010

11-
impl<T: Send + 'static> Client<T> {
12-
pub fn add_command(&mut self, name: &str, cmd: Command<T>) {
11+
impl<T: Send + 'static, E: RunError + 'static> Client<T, E> {
12+
pub fn add_command(&mut self, name: &str, cmd: Command<T, E>) {
1313
self.commands.insert(name.to_string(), cmd);
1414
}
1515

@@ -53,10 +53,10 @@ impl<T: Send + 'static> Client<T> {
5353
}
5454

5555
match cmd.run(&context, flags) {
56-
RunError::None => (),
57-
RunError::Quit => break,
58-
RunError::Error(o_o) => println!("{}", o_o),
59-
RunError::Fatal(o_o) => {
56+
RunErrorType::None => (),
57+
RunErrorType::Quit => break,
58+
RunErrorType::Error(o_o) => println!("{}", o_o),
59+
RunErrorType::Fatal(o_o) => {
6060
println!("{}", o_o);
6161
break;
6262
}
@@ -65,43 +65,43 @@ impl<T: Send + 'static> Client<T> {
6565
}
6666
}
6767

68-
impl<T: Sync + Send> Default for Client<T> {
68+
impl<T: Send, E: RunError> Default for Client<T, E> {
6969
fn default() -> Self {
70-
Client {
70+
Client::<T, E> {
7171
commands: HashMap::new(),
7272
}
7373
}
7474
}
7575

76-
type RunFn<T> = fn(Arc<Mutex<T>>, Flags) -> Result<(), RunError>;
76+
type RunFn<T, E> = fn(Arc<Mutex<T>>, Flags) -> Result<(), E>;
7777

78-
pub struct Command<T> {
79-
pub run_fn: RunFn<T>,
78+
pub struct Command<T, E: RunError> {
79+
pub run_fn: RunFn<T, E>,
8080
pub flags: HashMap<String, Flag>,
8181
pub parallel: bool,
8282
}
8383

84-
impl<T: Send + 'static> Command<T> {
85-
pub fn new(func: RunFn<T>) -> Command<T> {
84+
impl<T: Send + 'static, E: RunError + 'static> Command<T, E> {
85+
pub fn new(func: RunFn<T, E>) -> Command<T, E> {
8686
Command {
8787
run_fn: func,
8888
flags: Default::default(),
8989
parallel: false,
9090
}
9191
}
9292

93-
pub fn run(&self, context: &Arc<Mutex<T>>, flags: Flags) -> RunError {
93+
pub fn run(&self, context: &Arc<Mutex<T>>, flags: Flags) -> RunErrorType {
9494
let context = Arc::clone(context);
9595
let func = self.run_fn;
9696

9797
if self.parallel {
9898
thread::spawn(move || func(context, flags));
99-
return RunError::None;
99+
return RunErrorType::None;
100100
}
101101

102102
match (self.run_fn)(context, flags) {
103-
Ok(_) => RunError::None,
104-
Err(err) => err,
103+
Ok(_) => RunErrorType::None,
104+
Err(err) => err.error(),
105105
}
106106
}
107107

@@ -110,25 +110,18 @@ impl<T: Send + 'static> Command<T> {
110110
}
111111
}
112112

113-
pub enum RunError {
113+
pub trait RunError: Send {
114+
fn error(&self) -> RunErrorType;
115+
}
116+
117+
#[derive(Clone)]
118+
pub enum RunErrorType {
114119
None,
115120
Quit,
116121
Error(String),
117122
Fatal(String),
118123
}
119124

120-
impl From<RunError> for Result<(), RunError> {
121-
fn from(value: RunError) -> Self {
122-
Err(value)
123-
}
124-
}
125-
126-
impl From<&dyn fmt::Debug> for RunError {
127-
fn from(value: &dyn fmt::Debug) -> Self {
128-
Self::Error(format!("{:?}", value))
129-
}
130-
}
131-
132125
#[derive(Clone, Copy)]
133126
pub enum Flag {
134127
Boolean,

0 commit comments

Comments
 (0)