pub mod error;
use error::*;
use gadget_std::hash::Hash;
pub type OperatorSet<K, V> = std::collections::BTreeMap<K, V>;
#[async_trait::async_trait]
#[auto_impl::auto_impl(&, Arc)]
pub trait GadgetServicesClient: Send + Sync + 'static {
type PublicApplicationIdentity: Eq + PartialEq + Hash + Ord + PartialOrd + Send + Sync + 'static;
type PublicAccountIdentity: Send + Sync + 'static;
type Id: Send + Sync + 'static;
type Error: core::error::Error + From<Error> + Send + Sync + 'static;
async fn get_operators(
&self,
) -> Result<
OperatorSet<Self::PublicAccountIdentity, Self::PublicApplicationIdentity>,
Self::Error,
>;
async fn operator_id(&self) -> Result<Self::PublicApplicationIdentity, Self::Error>;
async fn blueprint_id(&self) -> Result<Self::Id, Self::Error>;
async fn get_operators_and_operator_id(
&self,
) -> Result<(OperatorSet<usize, Self::PublicApplicationIdentity>, usize), Self::Error> {
let operators = self
.get_operators()
.await
.map_err(|e| Error::GetOperatorsAndOperatorId(e.to_string()))?;
let my_id = self
.operator_id()
.await
.map_err(|e| Error::GetOperatorsAndOperatorId(e.to_string()))?;
let mut ret = OperatorSet::new();
let mut ret_id = None;
for (id, op) in operators.into_values().enumerate() {
if my_id == op {
ret_id = Some(id);
}
ret.insert(id, op);
}
let ret_id = ret_id.ok_or_else(|| {
Error::GetOperatorsAndOperatorId("Operator index not found".to_string())
})?;
Ok((ret, ret_id))
}
async fn get_operator_index(&self) -> Result<usize, Self::Error> {
let (_, index) = self
.get_operators_and_operator_id()
.await
.map_err(|err| Error::GetOperatorIndex(err.to_string()))?;
Ok(index)
}
}