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
use std::{collections::BTreeMap, sync::Arc};

use crate::{
    procedure_object,
    types::{XoObject, XoObjectMap},
    RpcError,
};

use jsonrpsee_types::{traits::Client, v2::params::ParamsSer, JsonValue};
use jsonrpsee_ws_client::WsClient;

use crate::procedure_args;

pub struct XoProcedures {
    pub(crate) inner: Arc<WsClient>,
}

impl XoProcedures {
    /// Get all objects from server
    /// * `R` is a type that can hold that entire result set with all different types
    /// * `filter` is an optional filter
    /// * `limit` is an optional max limit on number of results
    /// xo-cli: xo.getAllObjects [filter=<object>] [limit=<number>] [ndjson=<boolean>]
    pub async fn get_all_objects<R: serde::de::DeserializeOwned>(
        &self,
        filter: impl Into<Option<serde_json::Map<String, JsonValue>>>,
        limit: impl Into<Option<usize>>,
    ) -> Result<R, RpcError> {
        let args = match (filter.into(), limit.into()) {
            (Some(filter), Some(limit)) => {
                procedure_args! { "filter" => filter, "limit" => limit }
            }
            (Some(filter), None) => procedure_args! { "filter" => filter },
            (None, Some(limit)) => procedure_args! { "limit" => limit },
            (None, None) => procedure_args! {},
        };

        self.inner
            .request("xo.getAllObjects", Some(ParamsSer::Map(args)))
            .await
    }

    /// Get all objects of specified type from server
    /// * `R` is a type that can represent that collection of objects
    /// * `filter` is an optional filter
    /// * `limit` is an optional max limit on number of results
    pub async fn get_objects<R: XoObjectMap>(
        &self,
        filter: impl Into<Option<serde_json::Map<String, JsonValue>>>,
        limit: impl Into<Option<usize>>,
    ) -> Result<R, RpcError> {
        let mut filter = filter.into().unwrap_or_default();
        filter.insert("type".to_string(), R::Object::OBJECT_TYPE.into());

        self.get_all_objects(filter, limit).await
    }

    /// Get single object of specified type from server
    /// * `R` is a type that can represent that type of object
    /// * `id` is the id of the object
    pub async fn get_object<R: XoObject>(
        &self,
        id: R::IdType,
    ) -> Result<Option<R>, GetSingleObjectError>
    where
        R::IdType: Ord,
    {
        let filter = procedure_object!(
            "id" => id.clone(),
            "type" => R::OBJECT_TYPE.to_string()
        );

        // TODO: Can we get rid of the BTreeMap here?
        let mut result: BTreeMap<R::IdType, R> = self
            .get_all_objects(filter, Some(2))
            .await
            .map_err(GetSingleObjectError::Rpc)?;

        match result.remove(&id) {
            None => Ok(None),
            Some(vm) if result.is_empty() => Ok(Some(vm)),
            _ => Err(GetSingleObjectError::MultipleMatches),
        }
    }
}

#[derive(Debug)]
pub enum GetSingleObjectError {
    MultipleMatches,
    Rpc(RpcError),
}