use crate::Reason;
mod contexts;
mod lifecycle;
mod orbs;
mod references;
mod resources;
mod tasks;
#[cfg(test)]
mod tests;
use crate::{
Code, Error, Result,
index::{Index, VersionCounts},
model::*,
program::WaitKey,
state::Snapshot,
};
use std::{
cell::RefCell,
collections::{BTreeMap, BTreeSet, VecDeque},
rc::Rc,
};
#[derive(Clone)]
pub struct Host {
pub(crate) inner: Rc<RefCell<Inner>>,
}
pub(crate) use storage::Inner;
use storage::Pending;
mod ownership;
mod storage;
impl Default for Host {
fn default() -> Self {
Self::new()
}
}
pub(crate) fn gone(kind: Reason) -> Error {
Error::new(Code::Gone, kind, format!("{kind} is no longer available"))
}
pub(crate) fn conflict(reason: Reason) -> Error {
Error::new(Code::Conflict, reason, reason.as_str().replace('_', " "))
}
fn exhausted() -> Error {
Error::new(
Code::Exhausted,
Reason::Identities,
"identity or revision space exhausted",
)
}
fn reserve(next: &mut Id) -> Result<Id> {
let id = *next;
if id > MAX_ID {
return Err(exhausted());
}
*next += 1;
Ok(id)
}
fn validate_orb(orb: &Orb) -> Result<()> {
if orb.name.is_empty() || orb.api_version == 0 {
return Err(Error::invalid(
Reason::Orb,
"orb name and api version are required",
));
}
if !cfg!(feature = "hooks")
&& (!orb.hooks.is_empty() || orb.requires.iter().any(|r| !r.hooks.is_empty()))
{
return Err(Error::new(
Code::Unsupported,
Reason::Hooks,
"hooks were excluded from this build",
));
}
let mut services = BTreeSet::new();
for service in &orb.services {
if service.name.is_empty()
|| !services.insert(&service.name)
|| service.methods.iter().any(|m| {
m.is_empty()
|| matches!(
m.as_str(),
"then" | "release" | "state" | "ready" | "status" | "opaque"
)
})
{
return Err(Error::invalid(
Reason::Service,
"invalid or duplicate service/member name",
));
}
if service.lifetime == Lifetime::Host && service.linger_ms != 0 {
return Err(Error::invalid(
Reason::Linger,
"host-lifetime objects cannot linger",
));
}
}
Ok(())
}