Skip to main content

tembra_core/host/
mod.rs

1use crate::Reason;
2mod contexts;
3mod lifecycle;
4mod orbs;
5mod references;
6mod resources;
7mod tasks;
8#[cfg(test)]
9mod tests;
10
11use crate::{
12    Code, Error, Result,
13    index::{Index, VersionCounts},
14    model::*,
15    program::WaitKey,
16    state::Snapshot,
17};
18use std::{
19    cell::RefCell,
20    collections::{BTreeMap, BTreeSet, VecDeque},
21    rc::Rc,
22};
23
24#[derive(Clone)]
25pub struct Host {
26    pub(crate) inner: Rc<RefCell<Inner>>,
27}
28
29pub(crate) use storage::Inner;
30use storage::Pending;
31
32mod ownership;
33mod storage;
34
35impl Default for Host {
36    /// Create a fresh platform-independent kernel.
37    fn default() -> Self {
38        Self::new()
39    }
40}
41
42/// Describe a record that no longer exists.
43pub(crate) fn gone(kind: Reason) -> Error {
44    Error::new(Code::Gone, kind, format!("{kind} is no longer available"))
45}
46
47/// Describe an operation rejected by the current lifecycle state.
48pub(crate) fn conflict(reason: Reason) -> Error {
49    Error::new(Code::Conflict, reason, reason.as_str().replace('_', " "))
50}
51
52/// Report identity or revision exhaustion without wrapping counters.
53fn exhausted() -> Error {
54    Error::new(
55        Code::Exhausted,
56        Reason::Identities,
57        "identity or revision space exhausted",
58    )
59}
60
61/// Issue the next identity within the shared safe-integer limit.
62fn reserve(next: &mut Id) -> Result<Id> {
63    let id = *next;
64    if id > MAX_ID {
65        return Err(exhausted());
66    }
67    *next += 1;
68    Ok(id)
69}
70
71/// Reject invalid declarations before constructing a replacement graph.
72fn validate_orb(orb: &Orb) -> Result<()> {
73    if orb.name.is_empty() || orb.api_version == 0 {
74        return Err(Error::invalid(
75            Reason::Orb,
76            "orb name and api version are required",
77        ));
78    }
79    if !cfg!(feature = "hooks")
80        && (!orb.hooks.is_empty() || orb.requires.iter().any(|r| !r.hooks.is_empty()))
81    {
82        return Err(Error::new(
83            Code::Unsupported,
84            Reason::Hooks,
85            "hooks were excluded from this build",
86        ));
87    }
88    let mut services = BTreeSet::new();
89    for service in &orb.services {
90        if service.name.is_empty()
91            || !services.insert(&service.name)
92            || service.methods.iter().any(|m| {
93                m.is_empty()
94                    || matches!(
95                        m.as_str(),
96                        "then" | "release" | "state" | "ready" | "status" | "opaque"
97                    )
98            })
99        {
100            return Err(Error::invalid(
101                Reason::Service,
102                "invalid or duplicate service/member name",
103            ));
104        }
105        if service.lifetime == Lifetime::Host && service.linger_ms != 0 {
106            return Err(Error::invalid(
107                Reason::Linger,
108                "host-lifetime objects cannot linger",
109            ));
110        }
111    }
112    Ok(())
113}