mbus_api/
lib.rs

1#![allow(missing_docs, trivial_casts, unused_variables, unused_mut, unused_imports, unused_extern_crates, non_camel_case_types)]
2
3use async_trait::async_trait;
4use futures::Stream;
5use std::error::Error;
6use std::task::{Poll, Context};
7use swagger::{ApiError, ContextWrapper};
8
9type ServiceError = Box<dyn Error + Send + Sync + 'static>;
10
11pub const BASE_PATH: &'static str = "";
12pub const API_VERSION: &'static str = "0.3.5";
13
14#[derive(Debug, PartialEq)]
15#[must_use]
16pub enum GetResponse {
17    /// OK
18    OK
19    (String)
20    ,
21    /// Bad request
22    BadRequest
23    (String)
24    ,
25    /// Not found (or M-Bus HTTPD is unauthorized to access it, or to change baud rate to that specified, etc)
26    NotFound
27    (String)
28}
29
30#[derive(Debug, PartialEq)]
31#[must_use]
32pub enum GetMultiResponse {
33    /// OK
34    OK
35    (String)
36    ,
37    /// Bad request
38    BadRequest
39    (String)
40    ,
41    /// Not found (or M-Bus HTTPD is unauthorized to access it, or to change baud rate to that specified, etc)
42    NotFound
43    (String)
44}
45
46#[derive(Debug, PartialEq)]
47#[must_use]
48pub enum HatResponse {
49    /// OK
50    OK
51    (models::Hat)
52    ,
53    /// Not found
54    NotFound
55    (String)
56}
57
58#[derive(Debug, PartialEq)]
59#[must_use]
60pub enum HatOffResponse {
61    /// OK
62    OK
63    ,
64    /// Not found
65    NotFound
66    (String)
67}
68
69#[derive(Debug, PartialEq)]
70#[must_use]
71pub enum HatOnResponse {
72    /// OK
73    OK
74    ,
75    /// Not found
76    NotFound
77    (String)
78}
79
80#[derive(Debug, PartialEq)]
81#[must_use]
82pub enum MbusApiResponse {
83    /// OK
84    OK
85    (String)
86    ,
87    /// Not found
88    NotFound
89    (String)
90}
91
92#[derive(Debug, PartialEq)]
93#[must_use]
94pub enum ScanResponse {
95    /// OK
96    OK
97    (String)
98    ,
99    /// Bad request
100    BadRequest
101    (String)
102    ,
103    /// Not found (e.g. device not found, or M-Bus HTTPD is unauthorized to access it, or to change baud rate to that specified, device not responding etc)
104    NotFound
105    (String)
106}
107
108/// API
109#[async_trait]
110pub trait Api<C: Send + Sync> {
111    fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>> {
112        Poll::Ready(Ok(()))
113    }
114
115    async fn get(
116        &self,
117        device: String,
118        baudrate: models::Baudrate,
119        address: String,
120        context: &C) -> Result<GetResponse, ApiError>;
121
122    async fn get_multi(
123        &self,
124        device: String,
125        baudrate: models::Baudrate,
126        address: String,
127        maxframes: i32,
128        context: &C) -> Result<GetMultiResponse, ApiError>;
129
130    async fn hat(
131        &self,
132        context: &C) -> Result<HatResponse, ApiError>;
133
134    async fn hat_off(
135        &self,
136        context: &C) -> Result<HatOffResponse, ApiError>;
137
138    async fn hat_on(
139        &self,
140        context: &C) -> Result<HatOnResponse, ApiError>;
141
142    async fn mbus_api(
143        &self,
144        context: &C) -> Result<MbusApiResponse, ApiError>;
145
146    async fn scan(
147        &self,
148        device: String,
149        baudrate: models::Baudrate,
150        context: &C) -> Result<ScanResponse, ApiError>;
151
152}
153
154/// API where `Context` isn't passed on every API call
155#[async_trait]
156pub trait ApiNoContext<C: Send + Sync> {
157
158    fn poll_ready(&self, _cx: &mut Context) -> Poll<Result<(), Box<dyn Error + Send + Sync + 'static>>>;
159
160    fn context(&self) -> &C;
161
162    async fn get(
163        &self,
164        device: String,
165        baudrate: models::Baudrate,
166        address: String,
167        ) -> Result<GetResponse, ApiError>;
168
169    async fn get_multi(
170        &self,
171        device: String,
172        baudrate: models::Baudrate,
173        address: String,
174        maxframes: i32,
175        ) -> Result<GetMultiResponse, ApiError>;
176
177    async fn hat(
178        &self,
179        ) -> Result<HatResponse, ApiError>;
180
181    async fn hat_off(
182        &self,
183        ) -> Result<HatOffResponse, ApiError>;
184
185    async fn hat_on(
186        &self,
187        ) -> Result<HatOnResponse, ApiError>;
188
189    async fn mbus_api(
190        &self,
191        ) -> Result<MbusApiResponse, ApiError>;
192
193    async fn scan(
194        &self,
195        device: String,
196        baudrate: models::Baudrate,
197        ) -> Result<ScanResponse, ApiError>;
198
199}
200
201/// Trait to extend an API to make it easy to bind it to a context.
202pub trait ContextWrapperExt<C: Send + Sync> where Self: Sized
203{
204    /// Binds this API to a context.
205    fn with_context(self: Self, context: C) -> ContextWrapper<Self, C>;
206}
207
208impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ContextWrapperExt<C> for T {
209    fn with_context(self: T, context: C) -> ContextWrapper<T, C> {
210         ContextWrapper::<T, C>::new(self, context)
211    }
212}
213
214#[async_trait]
215impl<T: Api<C> + Send + Sync, C: Clone + Send + Sync> ApiNoContext<C> for ContextWrapper<T, C> {
216    fn poll_ready(&self, cx: &mut Context) -> Poll<Result<(), ServiceError>> {
217        self.api().poll_ready(cx)
218    }
219
220    fn context(&self) -> &C {
221        ContextWrapper::context(self)
222    }
223
224    async fn get(
225        &self,
226        device: String,
227        baudrate: models::Baudrate,
228        address: String,
229        ) -> Result<GetResponse, ApiError>
230    {
231        let context = self.context().clone();
232        self.api().get(device, baudrate, address, &context).await
233    }
234
235    async fn get_multi(
236        &self,
237        device: String,
238        baudrate: models::Baudrate,
239        address: String,
240        maxframes: i32,
241        ) -> Result<GetMultiResponse, ApiError>
242    {
243        let context = self.context().clone();
244        self.api().get_multi(device, baudrate, address, maxframes, &context).await
245    }
246
247    async fn hat(
248        &self,
249        ) -> Result<HatResponse, ApiError>
250    {
251        let context = self.context().clone();
252        self.api().hat(&context).await
253    }
254
255    async fn hat_off(
256        &self,
257        ) -> Result<HatOffResponse, ApiError>
258    {
259        let context = self.context().clone();
260        self.api().hat_off(&context).await
261    }
262
263    async fn hat_on(
264        &self,
265        ) -> Result<HatOnResponse, ApiError>
266    {
267        let context = self.context().clone();
268        self.api().hat_on(&context).await
269    }
270
271    async fn mbus_api(
272        &self,
273        ) -> Result<MbusApiResponse, ApiError>
274    {
275        let context = self.context().clone();
276        self.api().mbus_api(&context).await
277    }
278
279    async fn scan(
280        &self,
281        device: String,
282        baudrate: models::Baudrate,
283        ) -> Result<ScanResponse, ApiError>
284    {
285        let context = self.context().clone();
286        self.api().scan(device, baudrate, &context).await
287    }
288
289}
290
291
292#[cfg(feature = "client")]
293pub mod client;
294
295// Re-export Client as a top-level name
296#[cfg(feature = "client")]
297pub use client::Client;
298
299#[cfg(feature = "server")]
300pub mod server;
301
302// Re-export router() as a top-level name
303#[cfg(feature = "server")]
304pub use self::server::Service;
305
306#[cfg(feature = "server")]
307pub mod context;
308
309pub mod models;
310
311#[cfg(any(feature = "client", feature = "server"))]
312pub(crate) mod header;