gadget_client_core/
lib.rs1pub mod error;
2use error::*;
3
4use gadget_std::hash::Hash;
5
6pub type OperatorSet<K, V> = std::collections::BTreeMap<K, V>;
7
8#[async_trait::async_trait]
9#[auto_impl::auto_impl(&, Arc)]
10pub trait GadgetServicesClient: Send + Sync + 'static {
11 type PublicApplicationIdentity: Eq + PartialEq + Hash + Ord + PartialOrd + Send + Sync + 'static;
14 type PublicAccountIdentity: Send + Sync + 'static;
17 type Id: Send + Sync + 'static;
19 type Error: core::error::Error + From<Error> + Send + Sync + 'static;
20
21 async fn get_operators(
23 &self,
24 ) -> Result<
25 OperatorSet<Self::PublicAccountIdentity, Self::PublicApplicationIdentity>,
26 Self::Error,
27 >;
28 async fn operator_id(&self) -> Result<Self::PublicApplicationIdentity, Self::Error>;
30 async fn blueprint_id(&self) -> Result<Self::Id, Self::Error>;
32
33 async fn get_operators_and_operator_id(
35 &self,
36 ) -> Result<(OperatorSet<usize, Self::PublicApplicationIdentity>, usize), Self::Error> {
37 let operators = self
38 .get_operators()
39 .await
40 .map_err(|e| Error::GetOperatorsAndOperatorId(e.to_string()))?;
41 let my_id = self
42 .operator_id()
43 .await
44 .map_err(|e| Error::GetOperatorsAndOperatorId(e.to_string()))?;
45 let mut ret = OperatorSet::new();
46 let mut ret_id = None;
47 for (id, op) in operators.into_values().enumerate() {
48 if my_id == op {
49 ret_id = Some(id);
50 }
51
52 ret.insert(id, op);
53 }
54
55 let ret_id = ret_id.ok_or_else(|| {
56 Error::GetOperatorsAndOperatorId("Operator index not found".to_string())
57 })?;
58 Ok((ret, ret_id))
59 }
60
61 async fn get_operator_index(&self) -> Result<usize, Self::Error> {
63 let (_, index) = self
64 .get_operators_and_operator_id()
65 .await
66 .map_err(|err| Error::GetOperatorIndex(err.to_string()))?;
67 Ok(index)
68 }
69}