myko_rs/
report.rs

1use serde::{ser::Error, Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::client::MykoClient;
5
6pub trait MykoReport<T> {
7    fn watch(&self, client: &MykoClient) -> impl tokio_stream::Stream<Item = T>;
8}
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct ReportResponse {
13    pub response: Value,
14
15    pub tx: String,
16}
17
18impl ReportResponse {
19    pub fn to_string(&self) -> Result<String, serde_json::Error> {
20        serde_json::to_string(self)
21    }
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase")]
26pub struct WrappedReport {
27    pub report: Value,
28    pub report_id: String,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(rename_all = "camelCase")]
33pub struct ReportError {
34    pub tx: String,
35    pub message: String,
36}
37
38pub trait ReportId {
39    fn report_id(&self) -> String;
40}
41
42pub fn wrap_report<Q: ReportId + Serialize + Clone>(
43    tx: String,
44    report: &Q,
45) -> Result<WrappedReport, serde_json::Error> {
46    let mut json = serde_json::to_value(report.clone())?;
47
48    let obj_mut = json.as_object_mut();
49
50    if obj_mut.is_none() {
51        return Err(serde_json::Error::custom("Could not convert to object"));
52    }
53
54    let obj = obj_mut.unwrap();
55
56    obj.insert("tx".to_string(), tx.into());
57
58    Ok(WrappedReport {
59        report: json,
60        report_id: report.report_id(),
61    })
62}