use crate::Error;
use std::collections::{BTreeMap, BTreeSet};
pub type Id = u64;
pub const MAX_ID: Id = (1 << 53) - 1;
pub type Version = u32;
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Address {
pub service: String,
pub key: Vec<String>,
pub principal: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Lifetime {
Referenced,
Host,
}
impl Lifetime {
pub const fn as_str(self) -> &'static str {
match self {
Self::Referenced => "referenced",
Self::Host => "host",
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Service {
pub name: String,
pub methods: BTreeSet<String>,
pub lifetime: Lifetime,
pub linger_ms: u32,
}
impl Service {
pub fn new(
name: impl Into<String>,
methods: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
Self {
name: name.into(),
methods: methods.into_iter().map(Into::into).collect(),
lifetime: Lifetime::Referenced,
linger_ms: 0,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Requirement {
pub orb: String,
pub api_version: Version,
pub methods: BTreeMap<String, BTreeSet<String>>,
pub hooks: BTreeSet<String>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Orb {
pub name: String,
pub api_version: Version,
pub services: Vec<Service>,
pub requires: Vec<Requirement>,
pub hooks: BTreeSet<String>,
}
impl Orb {
pub fn new(name: impl Into<String>, api_version: Version) -> Self {
Self {
name: name.into(),
api_version,
services: Vec::new(),
requires: Vec::new(),
hooks: BTreeSet::new(),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InstalledOrb {
pub id: Id,
pub owner: Id,
pub code_version: Version,
pub declaration: Orb,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Status {
Loading,
Live,
Released,
Gone,
}
impl Status {
pub const fn as_str(self) -> &'static str {
match self {
Self::Loading => "loading",
Self::Live => "live",
Self::Released => "released",
Self::Gone => "gone",
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Acquisition {
pub reference: Id,
pub object: Id,
pub orb: Id,
pub code_version: Version,
pub activate: bool,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Invocation {
pub context: Id,
pub id: Id,
pub root: Id,
pub object: Id,
pub orb: Id,
pub code_version: Version,
pub method: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum Owner {
Host,
Orb(Id),
Object(Id),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Resource {
pub id: Id,
pub owner: Owner,
pub name: String,
pub version: Version,
pub code_version: Version,
pub exclusive: bool,
pub dependencies: Vec<Id>,
pub value: Option<Id>,
pub closing: bool,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Action {
Execute {
program: Id,
effects: Vec<crate::Effect>,
},
CancelEffects {
program: Id,
effects: Vec<crate::Effect>,
},
ProgramPrepared(Id),
ProgramComplete {
program: Id,
error: Option<Error>,
},
DisposeObject(Id),
CloseResource(Id),
DropValue(Id),
CancelRoot(Id),
CancelTask(Id),
RetireOrb(Id),
StateChanged {
object: Id,
revision: u64,
},
#[cfg(feature = "subscriptions")]
SubscriptionFailed(Id),
#[cfg(feature = "subscriptions")]
DetachSubscription(Id),
#[cfg(feature = "subscriptions")]
Deliver {
subscription: Id,
payload: Id,
operation: Id,
},
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Diagnostics {
pub objects: usize,
pub references: usize,
pub roots: usize,
pub calls: usize,
pub resources: usize,
pub tasks: usize,
pub orbs: usize,
pub closing: bool,
}
#[derive(Clone, Debug)]
pub(crate) struct Object {
pub address: Address,
pub orb: Id,
pub phase: Status,
pub references: usize,
pub calls: usize,
pub tasks: usize,
pub pinned: bool,
pub disposing: bool,
pub preparing: bool,
pub idle_at: Option<u64>,
pub linger_ms: u32,
pub gate: Option<BTreeSet<Id>>,
pub min_version: Version,
pub state: Option<crate::Snapshot>,
pub dependencies: BTreeSet<Id>,
}
#[derive(Clone, Debug)]
pub(crate) struct Reference {
pub object: Id,
pub released: bool,
pub expected_api: Version,
pub methods: BTreeSet<String>,
#[cfg(feature = "views")]
pub allowed: Option<BTreeSet<String>>,
}
#[derive(Clone, Debug)]
pub(crate) struct Root {
pub contexts: BTreeSet<Id>,
pub version: Version,
pub principal: String,
pub active: bool,
pub cancelled: bool,
pub calls: usize,
pub touched: BTreeSet<Id>,
pub orbs: BTreeSet<Id>,
pub cleanup: bool,
}
#[derive(Clone, Debug)]
pub(crate) struct Task {
pub owner: Owner,
pub cancelled: bool,
pub version: Version,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Reload {
pub id: Id,
pub code_version: Version,
pub orbs: Vec<InstalledOrb>,
pub objects: Vec<Id>,
pub providers: BTreeMap<String, Id>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Migration {
pub object: Id,
pub expected_revision: u64,
pub value: Id,
pub state_version: Version,
}