use crate::provider::{Handles, Memory};
use crate::api::extension::{ContractError, ProviderId};
use std::{collections::BTreeMap, sync::Arc, time::Duration};
#[derive(Default)]
pub struct HandlesAuthority {
providers: BTreeMap<ProviderId, Arc<dyn Handles>>,
}
#[derive(Default)]
pub struct ProviderAuthority {
pub handles: Option<Arc<dyn Handles>>,
pub memory: Option<Arc<dyn Memory>>,
}
#[derive(Default)]
pub struct Authorities {
providers: BTreeMap<ProviderId, ProviderAuthority>,
}
impl Authorities {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn grant(
&mut self,
provider: ProviderId,
authority: ProviderAuthority,
) -> Result<(), ContractError> {
if self.providers.insert(provider, authority).is_some() {
return Err(ContractError::DuplicateProvider);
}
Ok(())
}
#[must_use]
pub fn provider(&self, provider: &ProviderId) -> Option<&ProviderAuthority> {
self.providers.get(provider)
}
pub fn iter(&self) -> impl Iterator<Item = (&ProviderId, &ProviderAuthority)> {
self.providers.iter()
}
}
impl From<HandlesAuthority> for Authorities {
fn from(authority: HandlesAuthority) -> Self {
Self {
providers: authority
.providers
.into_iter()
.map(|(provider, handles)| {
(
provider,
ProviderAuthority {
handles: Some(handles),
memory: None,
},
)
})
.collect(),
}
}
}
impl HandlesAuthority {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn grant(
&mut self,
provider: ProviderId,
handles: Arc<dyn Handles>,
) -> Result<(), ContractError> {
if self.providers.insert(provider, handles).is_some() {
return Err(ContractError::DuplicateProvider);
}
Ok(())
}
#[must_use]
pub fn handles(&self, provider: &ProviderId) -> Option<&Arc<dyn Handles>> {
self.providers.get(provider)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ProcessId(pub u64);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ResourceId(pub u64);
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LinuxError {
pub errno: i32,
pub context: String,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResourceError {
pub category: ResourceErrorCategory,
pub context: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ResourceErrorCategory {
Unsupported,
Invalid,
Quota,
Host,
Provider,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExtensionError {
pub category: ExtensionErrorCategory,
pub context: String,
pub retry_after: Option<Duration>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExtensionErrorCategory {
Unsupported,
Invalid,
Protocol,
Timeout,
Cancelled,
Quota,
Provider,
}