@@ -2,14 +2,14 @@ use std::collections::{HashMap, HashSet};
2
2
use std:: io;
3
3
use std:: io:: BufRead ;
4
4
use std:: sync:: { Arc , Mutex } ;
5
- use std:: { fmt , thread} ;
5
+ use std:: thread;
6
6
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 > > ,
9
9
}
10
10
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 > ) {
13
13
self . commands . insert ( name. to_string ( ) , cmd) ;
14
14
}
15
15
@@ -53,10 +53,10 @@ impl<T: Send + 'static> Client<T> {
53
53
}
54
54
55
55
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) => {
60
60
println ! ( "{}" , o_o) ;
61
61
break ;
62
62
}
@@ -65,43 +65,43 @@ impl<T: Send + 'static> Client<T> {
65
65
}
66
66
}
67
67
68
- impl < T : Sync + Send > Default for Client < T > {
68
+ impl < T : Send , E : RunError > Default for Client < T , E > {
69
69
fn default ( ) -> Self {
70
- Client {
70
+ Client :: < T , E > {
71
71
commands : HashMap :: new ( ) ,
72
72
}
73
73
}
74
74
}
75
75
76
- type RunFn < T > = fn ( Arc < Mutex < T > > , Flags ) -> Result < ( ) , RunError > ;
76
+ type RunFn < T , E > = fn ( Arc < Mutex < T > > , Flags ) -> Result < ( ) , E > ;
77
77
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 > ,
80
80
pub flags : HashMap < String , Flag > ,
81
81
pub parallel : bool ,
82
82
}
83
83
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 > {
86
86
Command {
87
87
run_fn : func,
88
88
flags : Default :: default ( ) ,
89
89
parallel : false ,
90
90
}
91
91
}
92
92
93
- pub fn run ( & self , context : & Arc < Mutex < T > > , flags : Flags ) -> RunError {
93
+ pub fn run ( & self , context : & Arc < Mutex < T > > , flags : Flags ) -> RunErrorType {
94
94
let context = Arc :: clone ( context) ;
95
95
let func = self . run_fn ;
96
96
97
97
if self . parallel {
98
98
thread:: spawn ( move || func ( context, flags) ) ;
99
- return RunError :: None ;
99
+ return RunErrorType :: None ;
100
100
}
101
101
102
102
match ( self . run_fn ) ( context, flags) {
103
- Ok ( _) => RunError :: None ,
104
- Err ( err) => err,
103
+ Ok ( _) => RunErrorType :: None ,
104
+ Err ( err) => err. error ( ) ,
105
105
}
106
106
}
107
107
@@ -110,25 +110,18 @@ impl<T: Send + 'static> Command<T> {
110
110
}
111
111
}
112
112
113
- pub enum RunError {
113
+ pub trait RunError : Send {
114
+ fn error ( & self ) -> RunErrorType ;
115
+ }
116
+
117
+ #[ derive( Clone ) ]
118
+ pub enum RunErrorType {
114
119
None ,
115
120
Quit ,
116
121
Error ( String ) ,
117
122
Fatal ( String ) ,
118
123
}
119
124
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
-
132
125
#[ derive( Clone , Copy ) ]
133
126
pub enum Flag {
134
127
Boolean ,
0 commit comments