Skip to main content

tembra_core/
model.rs

1use crate::Error;
2use std::collections::{BTreeMap, BTreeSet};
3
4/// Host-local identities are never reused. Generated IDs fit exactly in a JavaScript Number;
5/// zero is reserved for an absent identity.
6pub type Id = u64;
7/// Largest generated identity, shared by every adapter without JSON precision loss.
8pub const MAX_ID: Id = (1 << 53) - 1;
9pub type Version = u32;
10
11/// Stable identity. Tuple boundaries are preserved; keys are not delimiter-joined.
12#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
13pub struct Address {
14    pub service: String,
15    pub key: Vec<String>,
16    pub principal: String,
17}
18
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20pub enum Lifetime {
21    Referenced,
22    Host,
23}
24
25impl Lifetime {
26    /// Return the stable wire name for this variant.
27    pub const fn as_str(self) -> &'static str {
28        match self {
29            Self::Referenced => "referenced",
30            Self::Host => "host",
31        }
32    }
33}
34
35#[derive(Clone, Debug, Eq, PartialEq)]
36pub struct Service {
37    pub name: String,
38    pub methods: BTreeSet<String>,
39    pub lifetime: Lifetime,
40    /// Keeps an unreferenced object and its tasks live until this idle duration expires.
41    pub linger_ms: u32,
42}
43
44impl Service {
45    /// Create a declaration with explicit identity and default options.
46    pub fn new(
47        name: impl Into<String>,
48        methods: impl IntoIterator<Item = impl Into<String>>,
49    ) -> Self {
50        Self {
51            name: name.into(),
52            methods: methods.into_iter().map(Into::into).collect(),
53            lifetime: Lifetime::Referenced,
54            linger_ms: 0,
55        }
56    }
57}
58
59#[derive(Clone, Debug, Eq, PartialEq)]
60pub struct Requirement {
61    pub orb: String,
62    pub api_version: Version,
63    pub methods: BTreeMap<String, BTreeSet<String>>,
64    pub hooks: BTreeSet<String>,
65}
66
67#[derive(Clone, Debug, Eq, PartialEq)]
68pub struct Orb {
69    pub name: String,
70    pub api_version: Version,
71    pub services: Vec<Service>,
72    pub requires: Vec<Requirement>,
73    pub hooks: BTreeSet<String>,
74}
75
76impl Orb {
77    /// Create a declaration with explicit identity and default options.
78    pub fn new(name: impl Into<String>, api_version: Version) -> Self {
79        Self {
80            name: name.into(),
81            api_version,
82            services: Vec::new(),
83            requires: Vec::new(),
84            hooks: BTreeSet::new(),
85        }
86    }
87}
88
89#[derive(Clone, Debug, Eq, PartialEq)]
90pub struct InstalledOrb {
91    pub id: Id,
92    pub owner: Id,
93    pub code_version: Version,
94    pub declaration: Orb,
95}
96
97#[derive(Clone, Copy, Debug, Eq, PartialEq)]
98pub enum Status {
99    Loading,
100    Live,
101    Released,
102    Gone,
103}
104
105impl Status {
106    /// Return the stable wire name for this variant.
107    pub const fn as_str(self) -> &'static str {
108        match self {
109            Self::Loading => "loading",
110            Self::Live => "live",
111            Self::Released => "released",
112            Self::Gone => "gone",
113        }
114    }
115}
116
117#[derive(Clone, Debug, Eq, PartialEq)]
118pub struct Acquisition {
119    pub reference: Id,
120    pub object: Id,
121    pub orb: Id,
122    pub code_version: Version,
123    pub activate: bool,
124}
125
126/// An admitted physical invocation. Finishing its observer does not finish this invocation.
127#[derive(Clone, Debug, Eq, PartialEq)]
128pub struct Invocation {
129    pub context: Id,
130    pub id: Id,
131    pub root: Id,
132    pub object: Id,
133    pub orb: Id,
134    pub code_version: Version,
135    pub method: String,
136}
137
138#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
139pub enum Owner {
140    Host,
141    Orb(Id),
142    Object(Id),
143}
144
145#[derive(Clone, Debug, Eq, PartialEq)]
146pub struct Resource {
147    pub id: Id,
148    pub owner: Owner,
149    pub name: String,
150    pub version: Version,
151    pub code_version: Version,
152    pub exclusive: bool,
153    pub dependencies: Vec<Id>,
154    pub value: Option<Id>,
155    pub closing: bool,
156}
157
158/// Actions are executed by the adapter after a kernel operation completes.
159#[derive(Clone, Debug, Eq, PartialEq)]
160pub enum Action {
161    Execute {
162        program: Id,
163        effects: Vec<crate::Effect>,
164    },
165    CancelEffects {
166        program: Id,
167        effects: Vec<crate::Effect>,
168    },
169    ProgramPrepared(Id),
170    ProgramComplete {
171        program: Id,
172        error: Option<Error>,
173    },
174    DisposeObject(Id),
175    CloseResource(Id),
176    DropValue(Id),
177    CancelRoot(Id),
178    CancelTask(Id),
179    RetireOrb(Id),
180    StateChanged {
181        object: Id,
182        revision: u64,
183    },
184    #[cfg(feature = "subscriptions")]
185    SubscriptionFailed(Id),
186    #[cfg(feature = "subscriptions")]
187    DetachSubscription(Id),
188    #[cfg(feature = "subscriptions")]
189    Deliver {
190        subscription: Id,
191        payload: Id,
192        operation: Id,
193    },
194}
195
196#[derive(Clone, Debug, Default, Eq, PartialEq)]
197pub struct Diagnostics {
198    pub objects: usize,
199    pub references: usize,
200    pub roots: usize,
201    pub calls: usize,
202    pub resources: usize,
203    pub tasks: usize,
204    pub orbs: usize,
205    pub closing: bool,
206}
207
208#[derive(Clone, Debug)]
209pub(crate) struct Object {
210    pub address: Address,
211    pub orb: Id,
212    pub phase: Status,
213    pub references: usize,
214    pub calls: usize,
215    pub tasks: usize,
216    pub pinned: bool,
217    pub disposing: bool,
218    pub preparing: bool,
219    pub idle_at: Option<u64>,
220    pub linger_ms: u32,
221    pub gate: Option<BTreeSet<Id>>,
222    pub min_version: Version,
223    pub state: Option<crate::Snapshot>,
224    pub dependencies: BTreeSet<Id>,
225}
226
227#[derive(Clone, Debug)]
228pub(crate) struct Reference {
229    pub object: Id,
230    pub released: bool,
231    pub expected_api: Version,
232    pub methods: BTreeSet<String>,
233    #[cfg(feature = "views")]
234    pub allowed: Option<BTreeSet<String>>,
235}
236
237#[derive(Clone, Debug)]
238pub(crate) struct Root {
239    pub contexts: BTreeSet<Id>,
240    pub version: Version,
241    pub principal: String,
242    pub active: bool,
243    pub cancelled: bool,
244    pub calls: usize,
245    pub touched: BTreeSet<Id>,
246    pub orbs: BTreeSet<Id>,
247    pub cleanup: bool,
248}
249
250#[derive(Clone, Debug)]
251pub(crate) struct Task {
252    pub owner: Owner,
253    pub cancelled: bool,
254    pub version: Version,
255}
256
257#[derive(Clone, Debug, Eq, PartialEq)]
258pub struct Reload {
259    pub id: Id,
260    pub code_version: Version,
261    pub orbs: Vec<InstalledOrb>,
262    pub objects: Vec<Id>,
263    /// Candidate providers for affected service identities, shared with language adapters.
264    pub providers: BTreeMap<String, Id>,
265}
266
267#[derive(Clone, Debug, Eq, PartialEq)]
268pub struct Migration {
269    pub object: Id,
270    pub expected_revision: u64,
271    pub value: Id,
272    pub state_version: Version,
273}