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
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate thiserror;
#[macro_use]
extern crate anyhow;
#[doc(hidden)]
pub mod bus_message;
mod error;
mod id;
mod request;
pub use error::*;
pub use id::*;
pub use request::*;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::io::Cursor;
pub type Result<T> = anyhow::Result<T>;
pub trait Topic: Serialize + DeserializeOwned + Send + Sync + 'static {
fn name() -> &'static str;
fn encode(&self) -> Result<Vec<u8>> {
let data = rmp_serde::to_vec(self)?;
Ok(data)
}
fn decode(data: &[u8]) -> Result<Self> {
let msg = rmp_serde::from_read(Cursor::new(data))?;
Ok(msg)
}
}
#[async_trait::async_trait]
pub trait Context {
async fn call<T, R>(&self, service_name: &str, request: Request<T>) -> Result<Response<R>>
where
T: Serialize + Send + 'static,
R: DeserializeOwned + Send + 'static;
async fn notify<T: Serialize + Send + 'static>(&self, service_name: &str, request: Request<T>);
async fn notify_to<T: Serialize + Send + 'static>(&self, to: ServiceId, request: Request<T>);
async fn publish_with_topic<T: Topic>(&self, topic: &str, msg: T);
async fn publish<T: Topic>(&self, msg: T) {
self.publish_with_topic(T::name(), msg).await
}
}