1
1
use serde:: { Deserialize , Serialize } ;
2
- use std:: { collections:: HashMap , fmt } ;
2
+ use std:: collections:: HashMap ;
3
3
4
4
pub type Pid = u32 ;
5
5
pub type JobId = u128 ;
6
6
pub type Path = String ;
7
7
pub type ResponseError = String ;
8
- pub type Result < T > = std:: result:: Result < T , ResponseError > ;
9
-
10
8
11
9
#[ derive( Debug , Serialize , Deserialize ) ]
12
10
pub struct Job {
13
- pub reqs : Vec < Request >
11
+ pub reqs : Vec < Request > ,
14
12
}
15
13
16
14
impl Job {
@@ -25,20 +23,19 @@ impl Job {
25
23
26
24
#[ derive( Debug , Serialize , Deserialize ) ]
27
25
pub struct JobReport {
28
- pub resps : Vec < Response >
26
+ pub resps : Vec < ResponseResult > ,
29
27
}
30
28
31
29
impl JobReport {
32
30
pub fn is_ok ( & self ) -> bool {
33
31
if let Some ( resp) = self . resps . last ( ) {
34
- resp. is_ok ( )
32
+ resp. 0 . is_ok ( )
35
33
} else {
36
34
false
37
35
}
38
36
}
39
37
}
40
38
41
-
42
39
#[ derive( Debug , Serialize , Deserialize ) ]
43
40
pub enum CoordinatorMessage {
44
41
Request ( JobId , Job ) ,
@@ -50,53 +47,54 @@ pub enum WorkerMessage {
50
47
Response ( JobId , JobReport ) ,
51
48
StdoutPacket ( String ) ,
52
49
StderrPacket ( String ) ,
50
+ StreamFailure ,
53
51
}
54
52
55
53
#[ derive( Debug , Serialize , Deserialize ) ]
56
54
pub struct WriteFileRequest {
57
55
pub path : Path ,
58
- pub content : Vec < u8 >
56
+ pub content : Vec < u8 > ,
59
57
}
60
58
61
59
#[ derive( Debug , Serialize , Deserialize ) ]
62
- pub struct WriteFileResponse ( pub Result < ( ) > ) ;
60
+ pub struct WriteFileResponse ( pub ( ) ) ;
63
61
64
62
#[ derive( Debug , Serialize , Deserialize ) ]
65
63
pub struct ReadFileRequest {
66
- pub path : Path
64
+ pub path : Path ,
67
65
}
68
66
69
67
#[ derive( Debug , Serialize , Deserialize ) ]
70
- pub struct ReadFileResponse ( pub Result < Vec < u8 > > ) ;
68
+ pub struct ReadFileResponse ( pub Vec < u8 > ) ;
71
69
72
70
#[ derive( Debug , Serialize , Deserialize ) ]
73
71
pub struct ExecuteCommandRequest {
74
- pub cmd : String ,
75
- pub args : Vec < String > ,
76
- pub envs : HashMap < String , String > ,
77
- pub cwd : Option < String > , // None means in project direcotry.
72
+ pub cmd : String ,
73
+ pub args : Vec < String > ,
74
+ pub envs : HashMap < String , String > ,
75
+ pub cwd : Option < String > , // None means in project direcotry.
78
76
}
79
77
80
78
#[ derive( Debug , Serialize , Deserialize ) ]
81
- pub struct ExecuteCommandResponse ( pub Result < ExecuteOutput > ) ;
79
+ pub struct ExecuteCommandResponse ( pub ExecuteOutput ) ;
82
80
83
81
#[ derive( Debug , Serialize , Deserialize ) ]
84
82
pub struct StreamCommandRequest {
85
- pub cmd : String ,
86
- pub args : Vec < String > ,
87
- pub envs : HashMap < String , String > ,
88
- pub cwd : Option < String > , // None means in project direcotry.
83
+ pub cmd : String ,
84
+ pub args : Vec < String > ,
85
+ pub envs : HashMap < String , String > ,
86
+ pub cwd : Option < String > , // None means in project direcotry.
89
87
}
90
88
91
89
#[ derive( Debug , Serialize , Deserialize ) ]
92
- pub struct StreamCommandResponse ( pub Result < ( ) > ) ;
90
+ pub struct StreamCommandResponse ( pub ( ) ) ;
93
91
// Relative path is resolved in project direcotry.
94
92
#[ derive( Debug , Serialize , Deserialize ) ]
95
93
pub enum Request {
96
94
WriteFile ( WriteFileRequest ) ,
97
95
ReadFile ( ReadFileRequest ) ,
98
96
ExecuteCommand ( ExecuteCommandRequest ) ,
99
- StreamCommand ( StreamCommandRequest )
97
+ StreamCommand ( StreamCommandRequest ) ,
100
98
}
101
99
102
100
#[ derive( Debug , Serialize , Deserialize , Clone ) ]
@@ -106,18 +104,6 @@ pub struct ExecuteOutput {
106
104
pub stdout : Vec < u8 > ,
107
105
}
108
106
109
- impl fmt:: Display for ExecuteOutput {
110
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
111
- write ! (
112
- f,
113
- "ExecuteOutput(\n status: {:?}\n stderr: {}\n stdout: {}\n )" ,
114
- self . status,
115
- std:: str :: from_utf8( & self . stderr) . unwrap( ) ,
116
- std:: str :: from_utf8( & self . stdout) . unwrap( )
117
- )
118
- }
119
- }
120
-
121
107
#[ derive( Debug , Serialize , Deserialize ) ]
122
108
pub enum Response {
123
109
WriteFile ( WriteFileResponse ) ,
@@ -126,39 +112,11 @@ pub enum Response {
126
112
StreamCommand ( StreamCommandResponse ) ,
127
113
}
128
114
129
- impl fmt:: Display for Response {
130
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
131
- match self {
132
- Response :: ReadFile ( res) => {
133
- if let Ok ( content) = & res. 0 {
134
- write ! (
135
- f,
136
- "ReadResult.content:\n {}" ,
137
- std:: str :: from_utf8( & content) . unwrap( )
138
- )
139
- } else {
140
- write ! ( f, "{:#?}" , res)
141
- }
142
- }
143
- Response :: ExecuteCommand ( res) => {
144
- if let Ok ( output) = & res. 0 {
145
- write ! ( f, "ReadResult.output:\n {}" , output)
146
- } else {
147
- write ! ( f, "{:#?}" , res)
148
- }
149
- }
150
- _ => write ! ( f, "{:#?}" , self ) ,
151
- }
152
- }
153
- }
115
+ #[ derive( Debug , Serialize , Deserialize ) ]
116
+ pub struct ResponseResult ( pub Result < Response , ResponseError > ) ;
154
117
155
- impl Response {
156
- pub fn is_ok ( & self ) -> bool {
157
- match self {
158
- Response :: WriteFile ( res) => res. 0 . is_ok ( ) ,
159
- Response :: ReadFile ( res) => res. 0 . is_ok ( ) ,
160
- Response :: ExecuteCommand ( res) => res. 0 . is_ok ( ) ,
161
- Response :: StreamCommand ( res) => res. 0 . is_ok ( ) ,
162
- }
118
+ impl From < Result < Response , anyhow:: Error > > for ResponseResult {
119
+ fn from ( value : Result < Response , anyhow:: Error > ) -> Self {
120
+ ResponseResult ( value. map_err ( |e| format ! ( "{:#}" , e) ) )
163
121
}
164
122
}
0 commit comments