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
//! Write a `com.fedfork.exec.composition` in Rust. `codegen` turns each module's module.json (vendored in
//! atlibs.lock.json, or a module directory) into types: a module, its entries, the contracts it hooks and
//! implements, and its config as a typed input. A route to a module that does not implement the contract,
//! or an input field of the wrong type, does not compile. `check` runs the hosts' load checks
//! (`atlibs compose check`) and points a failure at your line.
use std::cell::RefCell;
use std::marker::PhantomData;
use std::panic::Location;
use std::path::Path;
use serde::{Serialize, Serializer};
use serde_json::{Map, Value, json};
pub mod codegen;
pub use serde_json;
/// A module binding (generated).
pub trait Module {
const KEY: &'static str;
const PIN: Option<&'static str>;
}
/// A contract: a lexicon procedure one module hooks and another implements (generated, one per NSID).
pub trait Contract: Copy {
const NSID: &'static str;
}
#[diagnostic::on_unimplemented(message = "module `{Self}` has no hook `{C}`", label = "not one of the caller's module.json hooks")]
pub trait Hooks<C: Contract>: Module {
const REASON: &'static str;
}
#[diagnostic::on_unimplemented(
message = "module `{Self}` does not implement `{C}`",
label = "its module.json implements does not list this contract",
note = "route it to an instance whose module implements it"
)]
pub trait Implements<C: Contract>: Module {
const ENTRY: &'static str;
}
/// One of module `M`'s entries (generated: `VmMcp::vm_create`).
pub struct Entry<M>(&'static str, PhantomData<M>);
impl<M> Entry<M> {
pub const fn new(name: &'static str) -> Self {
Entry(name, PhantomData)
}
}
impl<M> Clone for Entry<M> {
fn clone(&self) -> Self {
*self
}
}
impl<M> Copy for Entry<M> {}
/// A parameter type: its module.json field type.
pub trait ParamType: Serialize {
const TYPE: &'static str;
}
impl ParamType for String {
const TYPE: &'static str = "string";
}
impl ParamType for i64 {
const TYPE: &'static str = "integer";
}
impl ParamType for f64 {
const TYPE: &'static str = "number";
}
impl ParamType for bool {
const TYPE: &'static str = "boolean";
}
impl<T: Serialize> ParamType for Vec<T> {
const TYPE: &'static str = "array";
}
impl ParamType for Map<String, Value> {
const TYPE: &'static str = "object";
}
/// A parameter's declaration; `Composition::param` adds it and gives its placeholder.
pub struct Param<T> {
key: String,
label: Option<String>,
required: bool,
description: Option<String>,
secret: bool,
range: (Option<i64>, Option<i64>),
default: Option<T>,
choices: Option<Vec<T>>,
}
impl<T: ParamType> Param<T> {
pub fn new(key: &str) -> Self {
Param { key: key.into(), label: None, required: false, description: None, secret: false, range: (None, None), default: None, choices: None }
}
pub fn label(mut self, s: &str) -> Self {
self.label = Some(s.into());
self
}
pub fn description(mut self, s: &str) -> Self {
self.description = Some(s.into());
self
}
pub fn required(mut self) -> Self {
self.required = true;
self
}
pub fn default(mut self, v: T) -> Self {
self.default = Some(v);
self
}
pub fn choices(mut self, v: Vec<T>) -> Self {
self.choices = Some(v);
self
}
}
impl Param<String> {
/// From the host's secrets, never a record: no default.
pub fn secret(mut self) -> Self {
self.secret = true;
self
}
}
impl Param<i64> {
pub fn range(mut self, min: i64, max: i64) -> Self {
self.range = (Some(min), Some(max));
self
}
}
/// `{"$param": key}`: a parameter's value in an instance's input.
#[derive(Clone)]
pub struct Placeholder<T>(String, PhantomData<T>);
impl<T> Serialize for Placeholder<T> {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
json!({ "$param": self.0 }).serialize(s)
}
}
/// `{"$host": name}`: a value only the host gives (any field's type).
pub struct Host(String);
pub fn host(name: &str) -> Host {
Host(name.into())
}
/// A module's typed input (generated: `<Module>Input`, one setter per config field).
pub trait Input<M> {
fn fields(self) -> Map<String, Value>;
}
/// A config field's value: a literal of its type, a parameter of its type, or a host value.
pub struct Arg<K>(pub Value, PhantomData<K>);
fn arg<K>(v: Value) -> Arg<K> {
Arg(v, PhantomData)
}
/// The field kinds (module.json field types).
pub mod kind {
pub struct Str;
pub struct Int;
pub struct Num;
pub struct Bool;
pub struct Obj;
pub struct Arr;
}
impl<K> From<Host> for Arg<K> {
fn from(h: Host) -> Self {
arg(json!({ "$host": h.0 }))
}
}
macro_rules! args {
($kind:ty: $($t:ty),*; $p:ty) => {
$(impl From<$t> for Arg<$kind> { fn from(v: $t) -> Self { arg(json!(v)) } })*
impl From<Placeholder<$p>> for Arg<$kind> { fn from(p: Placeholder<$p>) -> Self { arg(json!(p)) } }
};
}
args!(kind::Str: &str, String; String);
args!(kind::Int: i64, i32, u32; i64);
args!(kind::Num: f64, i64; f64);
args!(kind::Bool: bool; bool);
args!(kind::Obj: Map<String, Value>; Map<String, Value>);
impl<T: Serialize> From<Vec<T>> for Arg<kind::Arr> {
fn from(v: Vec<T>) -> Self {
arg(json!(v))
}
}
impl<T> From<Placeholder<Vec<T>>> for Arg<kind::Arr> {
fn from(p: Placeholder<Vec<T>>) -> Self {
arg(json!(p))
}
}
/// An instance of module `M` in a composition.
pub struct Inst<'c, M> {
c: &'c Composition,
i: usize,
_m: PhantomData<M>,
}
impl<M> Inst<'_, M> {
fn set(self, k: &str, v: Value) -> Self {
self.c.m.borrow_mut().instances[self.i].insert(k.into(), v);
self
}
fn name(&self) -> Value {
self.c.m.borrow().instances[self.i]["name"].clone()
}
pub fn session(self, s: &str) -> Self {
self.set("session", json!(s))
}
/// Its input: the module's generated `<Module>Input` (its config fields, typed).
pub fn input(self, v: impl Input<M>) -> Self {
self.set("input", Value::Object(v.fields()))
}
/// Not pinned to the vendored module: it gets its hooks' defaults.
pub fn unpinned(self) -> Self {
self.c.m.borrow_mut().instances[self.i].shift_remove("pin");
self
}
}
/// A handler the composition's caller supplies.
pub struct HandlerRef(String);
/// An edge, to add policies to.
pub struct EdgeRef<'c> {
c: &'c Composition,
i: usize,
}
impl EdgeRef<'_> {
pub fn policy<M>(self, inst: &Inst<M>, entry: Entry<M>) -> Self {
let p = json!({ "instance": inst.name(), "entry": entry.0 });
let mut m = self.c.m.borrow_mut();
let e = &mut m.edges[self.i];
e.entry("policies").or_insert_with(|| json!([])).as_array_mut().expect("a list").push(p);
drop(m);
self
}
}
#[derive(Default)]
struct Manifest {
parameters: Vec<Value>,
instances: Vec<Map<String, Value>>,
handlers: Vec<Value>,
edges: Vec<Map<String, Value>>,
exports: Vec<Value>,
limits: Option<Value>,
sites: Vec<(String, &'static Location<'static>)>,
}
pub struct Composition {
name: String,
description: Option<String>,
m: RefCell<Manifest>,
}
impl Composition {
pub fn new(name: &str) -> Self {
Composition { name: name.into(), description: None, m: RefCell::default() }
}
pub fn description(mut self, s: &str) -> Self {
self.description = Some(s.into());
self
}
pub fn limits(self, depth: u32, calls: u32) -> Self {
self.m.borrow_mut().limits = Some(json!({ "depth": depth, "calls": calls }));
self
}
pub fn param<T: ParamType>(&self, p: Param<T>) -> Placeholder<T> {
let mut o = Map::new();
o.insert("key".into(), json!(p.key));
p.label.map(|v| o.insert("label".into(), json!(v)));
o.insert("type".into(), json!(T::TYPE));
p.required.then(|| o.insert("required".into(), json!(true)));
p.description.map(|v| o.insert("description".into(), json!(v)));
p.secret.then(|| o.insert("secret".into(), json!(true)));
p.range.0.map(|v| o.insert("minimum".into(), json!(v)));
p.range.1.map(|v| o.insert("maximum".into(), json!(v)));
let mut values = Map::new();
p.default.map(|v| values.insert("default".into(), json!(v)));
p.choices.map(|v| values.insert("enum".into(), json!(v)));
if !values.is_empty() {
o.insert("values".into(), Value::Object(values));
}
self.m.borrow_mut().parameters.push(Value::Object(o));
Placeholder(p.key, PhantomData)
}
/// An instance of `M`, pinned as vendored.
#[track_caller]
pub fn instance<M: Module>(&self, name: &str) -> Inst<'_, M> {
let mut o = Map::new();
o.insert("name".into(), json!(name));
o.insert("module".into(), json!(M::KEY));
M::PIN.map(|p| o.insert("pin".into(), json!(p)));
let mut m = self.m.borrow_mut();
m.instances.push(o);
m.sites.push((format!("instance {name}"), Location::caller()));
Inst { c: self, i: m.instances.len() - 1, _m: PhantomData }
}
pub fn handler(&self, name: &str, description: &str) -> HandlerRef {
self.m.borrow_mut().handlers.push(json!({ "name": name, "description": description }));
HandlerRef(name.into())
}
#[track_caller]
fn edge(&self, from: Value, nsid: &str, to: Map<String, Value>, reason: &str) -> EdgeRef<'_> {
let mut o = Map::new();
o.insert("from".into(), from.clone());
o.insert("nsid".into(), json!(nsid));
o.extend(to);
o.insert("reason".into(), json!(reason));
let mut m = self.m.borrow_mut();
m.edges.push(o);
m.sites.push((format!("edge {} {nsid}", from.as_str().unwrap_or_default()), Location::caller()));
EdgeRef { c: self, i: m.edges.len() - 1 }
}
/// `from` calling contract `C` goes to `to` (at the entry its module implements). Empty `reason`: the hook's.
#[track_caller]
pub fn route<C: Contract, F: Hooks<C>, T: Implements<C>>(&self, from: &Inst<F>, _: C, to: &Inst<T>, reason: &str) -> EdgeRef<'_> {
self.edge(from.name(), C::NSID, Map::from_iter([("to".to_string(), to.name())]), if reason.is_empty() { F::REASON } else { reason })
}
/// `from` calling contract `C` goes to a handler the caller supplies.
#[track_caller]
pub fn route_handler<C: Contract, F: Hooks<C>>(&self, from: &Inst<F>, _: C, to: &HandlerRef, reason: &str) -> EdgeRef<'_> {
self.edge(from.name(), C::NSID, Map::from_iter([("handler".to_string(), json!(to.0))]), if reason.is_empty() { F::REASON } else { reason })
}
/// The entry serving contract `C`: the instance's implementing one.
pub fn export<C: Contract, T: Implements<C>>(&self, _: C, inst: &Inst<T>) {
self.export_entry(C::NSID, inst, Entry::<T>::new(T::ENTRY));
}
/// An entry serving `nsid` that its module does not declare in implements.
pub fn export_entry<T>(&self, nsid: &str, inst: &Inst<T>, entry: Entry<T>) {
self.m.borrow_mut().exports.push(json!({ "nsid": nsid, "instance": inst.name(), "entry": entry.0 }));
}
pub fn manifest(&self) -> Value {
let m = self.m.borrow();
let mut o = Map::new();
o.insert("$type".into(), json!("com.fedfork.exec.composition"));
o.insert("name".into(), json!(self.name));
self.description.as_ref().map(|d| o.insert("description".into(), json!(d)));
let mut list = |k: &str, v: Vec<Value>| (!v.is_empty()).then(|| o.insert(k.into(), Value::Array(v)));
list("parameters", m.parameters.clone());
list("instances", m.instances.iter().cloned().map(Value::Object).collect());
list("handlers", m.handlers.clone());
list("edges", m.edges.iter().cloned().map(Value::Object).collect());
list("exports", m.exports.clone());
m.limits.clone().map(|l| o.insert("limits".into(), l));
Value::Object(o)
}
/// The file a host loads: tab-indented, as `atlibs` and the JS builder write it.
pub fn to_json(&self) -> String {
let mut out = Vec::new();
let mut ser = serde_json::Serializer::with_formatter(&mut out, serde_json::ser::PrettyFormatter::with_indent(b"\t"));
self.manifest().serialize(&mut ser).expect("json");
format!("{}\n", String::from_utf8(out).expect("utf8"))
}
/// The hosts' load checks, no wasm: `atlibs compose check <file>` in `dir` (where atlibs.lock.json vendors
/// the modules). A failure names the line of the instance or route it is about.
pub fn check(&self, dir: &Path) -> Result<(), String> {
let file = dir.join(".atlibs-compose-check.json");
std::fs::write(&file, self.to_json()).map_err(|e| format!("{}: {e}", file.display()))?;
let out = std::process::Command::new("atlibs").args(["compose", "check"]).arg(&file).output();
let _ = std::fs::remove_file(&file);
let out = out.map_err(|e| format!("atlibs: {e}"))?;
if out.status.success() {
return Ok(());
}
let msg = String::from_utf8_lossy(&out.stderr).trim().to_string();
let m = self.m.borrow();
let site = m.sites.iter().filter(|(what, _)| msg.contains(&format!("{what}:")) || msg.contains(&format!("{what} "))).last();
Err(match site {
Some((_, at)) => format!("{msg}\n at {at}"),
None => msg,
})
}
}