lenso-app-plan 0.1.0

Immutable application plans for the Lenso vNext runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

fn default_configuration() -> Value {
    Value::Object(serde_json::Map::new())
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct CapabilityEndpoint {
    capability_id: String,
    descriptor_version: String,
    operations: Vec<String>,
    #[serde(default)]
    operation_kinds: BTreeMap<String, InteractionKind>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    admission: Option<RequestAdmission>,
    #[serde(default)]
    operation_admissions: BTreeMap<String, RequestAdmission>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    event_capacity: Option<usize>,
}

impl CapabilityEndpoint {
    /// Declares request Operations for one Capability endpoint.
    pub fn request(
        capability_id: impl Into<String>,
        descriptor_version: impl Into<String>,
        operations: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        Self {
            capability_id: capability_id.into(),
            descriptor_version: descriptor_version.into(),
            operations: operations.into_iter().map(Into::into).collect(),
            ..Self::default()
        }
    }

    /// Declares one endpoint and marks all supplied Operations as streams.
    pub fn stream(
        capability_id: impl Into<String>,
        descriptor_version: impl Into<String>,
        operations: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        let mut endpoint = Self::request(capability_id, descriptor_version, operations);
        for operation in endpoint.operations.clone() {
            endpoint
                .operation_kinds
                .insert(operation, InteractionKind::Stream);
        }
        endpoint
    }

    /// Declares one endpoint and marks all supplied Operations as Events.
    pub fn event(
        capability_id: impl Into<String>,
        descriptor_version: impl Into<String>,
        operations: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        let mut endpoint = Self::request(capability_id, descriptor_version, operations);
        for operation in endpoint.operations.clone() {
            endpoint
                .operation_kinds
                .insert(operation, InteractionKind::Event);
        }
        endpoint
    }

    /// Sets one Operation's interaction kind.
    #[must_use]
    pub fn with_operation_kind(
        mut self,
        operation: impl Into<String>,
        kind: InteractionKind,
    ) -> Self {
        self.operation_kinds.insert(operation.into(), kind);
        self
    }

    /// Applies one bounded request policy to every request Operation.
    #[must_use]
    pub fn with_admission(mut self, admission: RequestAdmission) -> Self {
        self.admission = Some(admission);
        self
    }

    /// Applies one bounded Event mailbox capacity.
    #[must_use]
    pub fn with_event_capacity(mut self, capacity: usize) -> Self {
        self.event_capacity = Some(capacity);
        self
    }

    /// Returns the Capability identity.
    pub fn capability_id(&self) -> &str {
        &self.capability_id
    }
    /// Returns the exact Descriptor version.
    pub fn descriptor_version(&self) -> &str {
        &self.descriptor_version
    }
    /// Returns the declared Operations.
    pub fn operations(&self) -> &[String] {
        &self.operations
    }
    /// Returns the authored interaction kinds.
    pub fn operation_kinds(&self) -> &BTreeMap<String, InteractionKind> {
        &self.operation_kinds
    }
    /// Returns the endpoint-wide request policy.
    pub const fn admission(&self) -> Option<RequestAdmission> {
        self.admission
    }
    /// Returns Operation-specific request policies.
    pub fn operation_admissions(&self) -> &BTreeMap<String, RequestAdmission> {
        &self.operation_admissions
    }
    /// Returns the Event mailbox capacity.
    pub const fn event_capacity(&self) -> Option<usize> {
        self.event_capacity
    }
}

/// Transport-independent interaction kind in authoring data.
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum InteractionKind {
    /// One request produces one response.
    #[default]
    Request,
    /// One open establishes a bidirectional stream.
    Stream,
    /// One publication is delivered to subscribers.
    Event,
}

/// Bounded request queue and concurrency policy.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RequestAdmission {
    queue_capacity: usize,
    max_concurrency: usize,
}

impl RequestAdmission {
    /// Creates one request admission policy.
    pub const fn new(queue_capacity: usize, max_concurrency: usize) -> Self {
        Self {
            queue_capacity,
            max_concurrency,
        }
    }
    /// Returns the queue capacity.
    pub const fn queue_capacity(self) -> usize {
        self.queue_capacity
    }
    /// Returns the concurrency limit.
    pub const fn max_concurrency(self) -> usize {
        self.max_concurrency
    }
}

/// One Capability requirement declared by a Module Instance.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct CapabilityRequirement {
    capability_id: String,
    descriptor_version: String,
    cardinality: Cardinality,
}

impl CapabilityRequirement {
    /// Declares an exactly-one requirement.
    pub fn one(capability_id: impl Into<String>, descriptor_version: impl Into<String>) -> Self {
        Self::new(capability_id, descriptor_version, Cardinality::One)
    }
    /// Declares an optional requirement.
    pub fn optional(
        capability_id: impl Into<String>,
        descriptor_version: impl Into<String>,
    ) -> Self {
        Self::new(capability_id, descriptor_version, Cardinality::Optional)
    }
    /// Declares a deterministic many-provider requirement.
    pub fn many(capability_id: impl Into<String>, descriptor_version: impl Into<String>) -> Self {
        Self::new(capability_id, descriptor_version, Cardinality::Many)
    }
    /// Declares a requirement with an explicit cardinality.
    pub fn new(
        capability_id: impl Into<String>,
        descriptor_version: impl Into<String>,
        cardinality: Cardinality,
    ) -> Self {
        Self {
            capability_id: capability_id.into(),
            descriptor_version: descriptor_version.into(),
            cardinality,
        }
    }
    /// Returns the Capability identity.
    pub fn capability_id(&self) -> &str {
        &self.capability_id
    }
    /// Returns the exact Descriptor version.
    pub fn descriptor_version(&self) -> &str {
        &self.descriptor_version
    }
    /// Returns the requirement cardinality.
    pub const fn cardinality(&self) -> Cardinality {
        self.cardinality
    }
}

/// Binding cardinality in App Composition.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Cardinality {
    One,
    Optional,
    Many,
}

/// One consumer-to-provider binding selected before Kernel boot.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Binding {
    consumer: String,
    capability_id: String,
    descriptor_version: String,
    provider: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    admission: Option<RequestAdmission>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    event_capacity: Option<usize>,
}

impl Binding {
    /// Binds a consumer requirement to one explicit provider Instance.
    pub fn new(
        consumer: impl Into<String>,
        capability_id: impl Into<String>,
        descriptor_version: impl Into<String>,
        provider: impl Into<String>,
    ) -> Self {
        Self {
            consumer: consumer.into(),
            capability_id: capability_id.into(),
            descriptor_version: descriptor_version.into(),
            provider: provider.into(),
            admission: None,
            event_capacity: None,
        }
    }
    /// Overrides request admission for this binding.
    #[must_use]
    pub fn with_admission(mut self, admission: RequestAdmission) -> Self {
        self.admission = Some(admission);
        self
    }
    /// Overrides Event mailbox capacity for this binding.
    #[must_use]
    pub fn with_event_capacity(mut self, capacity: usize) -> Self {
        self.event_capacity = Some(capacity);
        self
    }
    /// Returns the consumer Instance key.
    pub fn consumer(&self) -> &str {
        &self.consumer
    }
    /// Returns the Capability identity.
    pub fn capability_id(&self) -> &str {
        &self.capability_id
    }
    /// Returns the Descriptor version.
    pub fn descriptor_version(&self) -> &str {
        &self.descriptor_version
    }
    /// Returns the provider Instance key.
    pub fn provider(&self) -> &str {
        &self.provider
    }
    /// Returns an explicit request policy.
    pub const fn admission(&self) -> Option<RequestAdmission> {
        self.admission
    }
    /// Returns an explicit Event capacity.
    pub const fn event_capacity(&self) -> Option<usize> {
        self.event_capacity
    }
}

/// One Module Instance in App Composition.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Module {
    key: String,
    package: String,
    #[serde(default = "default_entrypoint")]
    entrypoint: String,
    #[serde(default = "default_configuration")]
    configuration: Value,
    #[serde(default)]
    provides: Vec<CapabilityEndpoint>,
    #[serde(default)]
    requires: Vec<CapabilityRequirement>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    execution_class: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    configuration_schema: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    role: Option<ModuleRole>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    execution_lane: Option<String>,
}

/// Target-owned Web UI role selected by an authoring profile.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ModuleRole {
    WebShell,
    BrowserAdapter,
    UiContribution,
}

fn default_entrypoint() -> String {
    "default".to_owned()
}

impl Module {
    /// Selects one package under an App-local Instance key.
    pub fn new(key: impl Into<String>, package: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            package: package.into(),
            entrypoint: default_entrypoint(),
            configuration: default_configuration(),
            provides: Vec::new(),
            requires: Vec::new(),
            execution_class: None,
            configuration_schema: None,
            role: None,
            execution_lane: None,
        }
    }
    /// Selects an explicit package entrypoint.
    #[must_use]
    pub fn with_entrypoint(mut self, entrypoint: impl Into<String>) -> Self {
        self.entrypoint = entrypoint.into();
        self
    }
    /// Supplies opaque non-secret Module configuration.
    #[must_use]
    pub fn with_configuration(mut self, configuration: Value) -> Self {
        self.configuration = configuration;
        self
    }
    /// Replaces Module configuration in an existing authoring document.
    pub fn set_configuration(&mut self, configuration: Value) {
        self.configuration = configuration;
    }
    /// Associates a JSON Schema used by `check` for configuration shape.
    #[must_use]
    pub fn with_configuration_schema(mut self, path: impl Into<String>) -> Self {
        self.configuration_schema = Some(path.into());
        self
    }
    /// Declares an explicit target-owned Web UI role.
    #[must_use]
    pub const fn with_role(mut self, role: ModuleRole) -> Self {
        self.role = Some(role);
        self
    }
    /// Places this Module Instance on one declared Execution Lane.
    #[must_use]
    pub fn with_execution_lane(mut self, execution_lane: impl Into<String>) -> Self {
        self.execution_lane = Some(execution_lane.into());
        self
    }
    /// Declares one provided Capability endpoint.
    #[must_use]
    pub fn with_capability(mut self, capability: CapabilityEndpoint) -> Self {
        self.provides.push(capability);
        self
    }
    /// Declares one required Capability.
    #[must_use]
    pub fn with_requirement(mut self, requirement: CapabilityRequirement) -> Self {
        self.requires.push(requirement);
        self
    }
    /// Selects an explicit Execution Adapter class.
    pub fn set_execution_class(&mut self, execution_class: impl Into<String>) {
        self.execution_class = Some(execution_class.into());
    }
    /// Returns the Instance key.
    pub fn key(&self) -> &str {
        &self.key
    }
    /// Returns the package identity.
    pub fn package(&self) -> &str {
        &self.package
    }
    /// Returns the entrypoint.
    pub fn entrypoint(&self) -> &str {
        &self.entrypoint
    }
    /// Returns Module configuration.
    pub fn configuration(&self) -> &Value {
        &self.configuration
    }
    /// Returns provided Capability endpoints.
    pub fn provides(&self) -> &[CapabilityEndpoint] {
        &self.provides
    }
    /// Returns required Capabilities.
    pub fn requires(&self) -> &[CapabilityRequirement] {
        &self.requires
    }
    /// Returns the selected Execution Adapter class, when explicit.
    pub fn execution_class(&self) -> Option<&str> {
        self.execution_class.as_deref()
    }
    /// Returns the optional configuration schema path.
    pub fn configuration_schema(&self) -> Option<&str> {
        self.configuration_schema.as_deref()
    }
    pub const fn role(&self) -> Option<ModuleRole> {
        self.role
    }
    /// Returns the authored Execution Lane, when one was selected explicitly.
    pub fn execution_lane(&self) -> Option<&str> {
        self.execution_lane.as_deref()
    }
}