-
Notifications
You must be signed in to change notification settings - Fork 13
/
org_example_more.rs
298 lines (298 loc) · 10.7 KB
/
org_example_more.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#![doc = "This file was automatically generated by the varlink rust generator"]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use serde_derive::{Deserialize, Serialize};
use std::io::BufRead;
use std::sync::{Arc, RwLock};
use varlink::{self, CallTrait};
#[allow(dead_code)]
#[derive(Clone, PartialEq, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum ErrorKind {
Varlink_Error,
VarlinkReply_Error,
TestMoreError(Option<TestMoreError_Args>),
}
impl ::std::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match self {
ErrorKind::Varlink_Error => write!(f, "Varlink Error"),
ErrorKind::VarlinkReply_Error => write!(f, "Varlink error reply"),
ErrorKind::TestMoreError(v) => write!(f, "org.example.more.TestMoreError: {:#?}", v),
}
}
}
pub struct Error(
pub ErrorKind,
pub Option<Box<dyn std::error::Error + 'static + Send + Sync>>,
pub Option<&'static str>,
);
impl Error {
#[allow(dead_code)]
pub fn kind(&self) -> &ErrorKind {
&self.0
}
}
impl From<ErrorKind> for Error {
fn from(e: ErrorKind) -> Self {
Error(e, None, None)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.1
.as_ref()
.map(|e| e.as_ref() as &(dyn std::error::Error + 'static))
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use std::error::Error as StdError;
if let Some(ref o) = self.2 {
std::fmt::Display::fmt(o, f)?;
}
std::fmt::Debug::fmt(&self.0, f)?;
if let Some(e) = self.source() {
std::fmt::Display::fmt("\nCaused by:\n", f)?;
std::fmt::Debug::fmt(&e, f)?;
}
Ok(())
}
}
#[allow(dead_code)]
pub type Result<T> = std::result::Result<T, Error>;
impl From<varlink::Error> for Error {
fn from(e: varlink::Error) -> Self {
match e.kind() {
varlink::ErrorKind::VarlinkErrorReply(r) => Error(
ErrorKind::from(r),
Some(Box::from(e)),
Some(concat!(file!(), ":", line!(), ": ")),
),
_ => Error(
ErrorKind::Varlink_Error,
Some(Box::from(e)),
Some(concat!(file!(), ":", line!(), ": ")),
),
}
}
}
#[allow(dead_code)]
impl Error {
pub fn source_varlink_kind(&self) -> Option<&varlink::ErrorKind> {
use std::error::Error as StdError;
let mut s: &dyn StdError = self;
while let Some(c) = s.source() {
let k = self
.source()
.and_then(|e| e.downcast_ref::<varlink::Error>())
.map(|e| e.kind());
if k.is_some() {
return k;
}
s = c;
}
None
}
}
impl From<&varlink::Reply> for ErrorKind {
#[allow(unused_variables)]
fn from(e: &varlink::Reply) -> Self {
match e {
varlink::Reply {
error: Some(ref t), ..
} if t == "org.example.more.TestMoreError" => match e {
varlink::Reply {
parameters: Some(p),
..
} => match serde_json::from_value(p.clone()) {
Ok(v) => ErrorKind::TestMoreError(v),
Err(_) => ErrorKind::TestMoreError(None),
},
_ => ErrorKind::TestMoreError(None),
},
_ => ErrorKind::VarlinkReply_Error,
}
}
}
pub trait VarlinkCallError: varlink::CallTrait {
fn reply_test_more_error(&mut self, r#reason: String) -> varlink::Result<()> {
self.reply_struct(varlink::Reply::error(
"org.example.more.TestMoreError",
Some(
serde_json::to_value(TestMoreError_Args { r#reason })
.map_err(varlink::map_context!())?,
),
))
}
}
impl<'a> VarlinkCallError for varlink::Call<'a> {}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct r#State {
pub r#start: Option<bool>,
pub r#progress: Option<i64>,
pub r#end: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct TestMoreError_Args {
pub r#reason: String,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Ping_Reply {
pub r#pong: String,
}
impl varlink::VarlinkReply for Ping_Reply {}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Ping_Args {
pub r#ping: String,
}
pub trait Call_Ping: VarlinkCallError {
fn reply(&mut self, r#pong: String) -> varlink::Result<()> {
self.reply_struct(Ping_Reply { r#pong }.into())
}
}
impl<'a> Call_Ping for varlink::Call<'a> {}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct StopServing_Reply {}
impl varlink::VarlinkReply for StopServing_Reply {}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct StopServing_Args {}
pub trait Call_StopServing: VarlinkCallError {
fn reply(&mut self) -> varlink::Result<()> {
self.reply_struct(varlink::Reply::parameters(None))
}
}
impl<'a> Call_StopServing for varlink::Call<'a> {}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct TestMore_Reply {
pub r#state: State,
}
impl varlink::VarlinkReply for TestMore_Reply {}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct TestMore_Args {
pub r#n: i64,
}
pub trait Call_TestMore: VarlinkCallError {
fn reply(&mut self, r#state: State) -> varlink::Result<()> {
self.reply_struct(TestMore_Reply { r#state }.into())
}
}
impl<'a> Call_TestMore for varlink::Call<'a> {}
pub trait VarlinkInterface {
fn ping(&self, call: &mut dyn Call_Ping, r#ping: String) -> varlink::Result<()>;
fn stop_serving(&self, call: &mut dyn Call_StopServing) -> varlink::Result<()>;
fn test_more(&self, call: &mut dyn Call_TestMore, r#n: i64) -> varlink::Result<()>;
fn call_upgraded(
&self,
_call: &mut varlink::Call,
_bufreader: &mut dyn BufRead,
) -> varlink::Result<Vec<u8>> {
Ok(Vec::new())
}
}
pub trait VarlinkClientInterface {
fn ping(&mut self, r#ping: String) -> varlink::MethodCall<Ping_Args, Ping_Reply, Error>;
fn stop_serving(&mut self) -> varlink::MethodCall<StopServing_Args, StopServing_Reply, Error>;
fn test_more(&mut self, r#n: i64) -> varlink::MethodCall<TestMore_Args, TestMore_Reply, Error>;
}
#[allow(dead_code)]
pub struct VarlinkClient {
connection: Arc<RwLock<varlink::Connection>>,
}
impl VarlinkClient {
#[allow(dead_code)]
pub fn new(connection: Arc<RwLock<varlink::Connection>>) -> Self {
VarlinkClient { connection }
}
}
impl VarlinkClientInterface for VarlinkClient {
fn ping(&mut self, r#ping: String) -> varlink::MethodCall<Ping_Args, Ping_Reply, Error> {
varlink::MethodCall::<Ping_Args, Ping_Reply, Error>::new(
self.connection.clone(),
"org.example.more.Ping",
Ping_Args { r#ping },
)
}
fn stop_serving(&mut self) -> varlink::MethodCall<StopServing_Args, StopServing_Reply, Error> {
varlink::MethodCall::<StopServing_Args, StopServing_Reply, Error>::new(
self.connection.clone(),
"org.example.more.StopServing",
StopServing_Args {},
)
}
fn test_more(&mut self, r#n: i64) -> varlink::MethodCall<TestMore_Args, TestMore_Reply, Error> {
varlink::MethodCall::<TestMore_Args, TestMore_Reply, Error>::new(
self.connection.clone(),
"org.example.more.TestMore",
TestMore_Args { r#n },
)
}
}
#[allow(dead_code)]
pub struct VarlinkInterfaceProxy {
inner: Box<dyn VarlinkInterface + Send + Sync>,
}
#[allow(dead_code)]
pub fn new(inner: Box<dyn VarlinkInterface + Send + Sync>) -> VarlinkInterfaceProxy {
VarlinkInterfaceProxy { inner }
}
impl varlink::Interface for VarlinkInterfaceProxy {
fn get_description(&self) -> &'static str {
"# Example Varlink service\ninterface org.example.more\n\n# Enum, returning either start, progress or end\n# progress: [0-100]\ntype State (\n start: ?bool,\n progress: ?int,\n end: ?bool\n)\n\n# Returns the same string\nmethod Ping(ping: string) -> (pong: string)\n\n# Dummy progress method\n# n: number of progress steps\nmethod TestMore(n: int) -> (state: State)\n\n# Stop serving\nmethod StopServing() -> ()\n\n# Something failed in TestMore\nerror TestMoreError (reason: string)\n"
}
fn get_name(&self) -> &'static str {
"org.example.more"
}
fn call_upgraded(
&self,
call: &mut varlink::Call,
bufreader: &mut dyn BufRead,
) -> varlink::Result<Vec<u8>> {
self.inner.call_upgraded(call, bufreader)
}
fn call(&self, call: &mut varlink::Call) -> varlink::Result<()> {
let req = call.request.unwrap();
match req.method.as_ref() {
"org.example.more.Ping" => {
if let Some(args) = req.parameters.clone() {
let args: Ping_Args = match serde_json::from_value(args) {
Ok(v) => v,
Err(e) => {
let es = format!("{}", e);
let _ = call.reply_invalid_parameter(es.clone());
return Err(varlink::context!(varlink::ErrorKind::SerdeJsonDe(es)));
}
};
self.inner.ping(call as &mut dyn Call_Ping, args.r#ping)
} else {
call.reply_invalid_parameter("parameters".into())
}
}
"org.example.more.StopServing" => {
self.inner.stop_serving(call as &mut dyn Call_StopServing)
}
"org.example.more.TestMore" => {
if let Some(args) = req.parameters.clone() {
let args: TestMore_Args = match serde_json::from_value(args) {
Ok(v) => v,
Err(e) => {
let es = format!("{}", e);
let _ = call.reply_invalid_parameter(es.clone());
return Err(varlink::context!(varlink::ErrorKind::SerdeJsonDe(es)));
}
};
self.inner
.test_more(call as &mut dyn Call_TestMore, args.r#n)
} else {
call.reply_invalid_parameter("parameters".into())
}
}
m => call.reply_method_not_found(String::from(m)),
}
}
}