commandagi 0.6.10

The CommandAGI SDK for Rust: the platform client (agent threads, live sessions on a simulated world) and CommandAGI documents (a schematic, a 3D part …) declared as code. No dependencies.
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! THE ONE ELEMENT TYPE — every document family is a tree of [`El`]: a tag (the TypeScript SDK's JSX tag), attributes
//! by their TypeScript names in the order they were written, children (elements, and texts for a tag that holds text),
//! and the call site that made it. The family modules declare their constructors and their attribute traits with the
//! two macros of this file (`elements!`, `attributes!`); `super` (design/mod.rs) states the conventions.

use super::json::Json;
use super::source::{here, Site};

/// One element: what one constructor call wrote, with the methods chained on it.
///
/// Its inherent methods are few and never an attribute's name, so a family's attribute trait is never shadowed.
#[derive(Clone, Debug)]
pub struct El {
    /// The TypeScript SDK's tag (`resistor`, `brush-stroke`, `Company`).
    pub tag: &'static str,
    /// The attributes by TypeScript name (`schX`), in the order they were written.
    pub attrs: Vec<(String, Json)>,
    /// How many of the first attributes the constructor took by position (the schematic's `name`, `from`, `to`).
    pub positional: usize,
    /// Elements and texts, in order. A fragment is flattened into the element that holds it.
    pub children: Vec<Child>,
    /// The constructor's call site; none for a fragment.
    pub site: Option<Site>,
    /// Values refused when they were written (invalid JSON text): the declaration refuses the first one by name.
    pub refused: Vec<String>,
}

/// A child of an element: an element, or a text of a tag that holds text.
#[derive(Clone, Debug)]
pub enum Child {
    El(El),
    Text(String),
}

impl El {
    /// An element of `tag`, at its caller's site (every constructor is `#[track_caller]`, so this is the file's call).
    #[track_caller]
    pub fn new(tag: &'static str) -> El {
        El { tag, attrs: Vec::new(), positional: 0, children: Vec::new(), site: Some(here()), refused: Vec::new() }
    }

    /// Set an attribute by its name, whatever the name: `.attr("cutoff", 4)`. A vocabulary declares most of its
    /// attributes as methods (`.x(40)`), but a field it does not list (a port of a node type of your own) has no method;
    /// this one always compiles and sets the field by its TypeScript name. [`with_attr`](El::with_attr) is the same.
    pub fn attr(self, name: impl Into<String>, value: impl Into<Value>) -> El {
        self.with_attr(name, value)
    }

    /// Set attribute `name` (a TypeScript name). Written twice, the last value wins in the first one's place.
    pub fn with_attr(mut self, name: impl Into<String>, value: impl Into<Value>) -> El {
        let name = name.into();
        let value = match value.into().0 {
            Ok(v) => v,
            Err(why) => {
                self.refused.push(format!("{} is JSON text ({why})", rust_name(&name)));
                Json::Null
            }
        };
        match self.attrs.iter_mut().find(|(k, _)| *k == name) {
            Some(entry) => entry.1 = value,
            None => self.attrs.push((name, value)),
        }
        self
    }

    /// Append children; a fragment's children take its place.
    pub fn with_children(mut self, children: impl IntoIterator<Item = Child>) -> El {
        fn push(out: &mut Vec<Child>, c: Child) {
            match c {
                Child::El(el) if el.tag == FRAGMENT => el.children.into_iter().for_each(|c| push(out, c)),
                Child::Text(t) if t.is_empty() => {}
                c => out.push(c),
            }
        }
        for c in children {
            push(&mut self.children, c);
        }
        self
    }

    /// The value of attribute `name` (a TypeScript name).
    pub fn attr_of(&self, name: &str) -> Option<&Json> {
        self.attrs.iter().find(|(k, _)| k == name).map(|(_, v)| v)
    }

    /// The child elements, in order (texts left out).
    pub fn child_elements(&self) -> impl Iterator<Item = &El> {
        self.children.iter().filter_map(|c| match c {
            Child::El(el) => Some(el),
            Child::Text(_) => None,
        })
    }

    /// The call as a person reads it in a message: `resistor("R1")`, `unit("U1", 2)`, `layer()`.
    pub fn call_text(&self) -> String {
        let args: Vec<String> = self.attrs.iter().take(self.positional).map(|(_, v)| v.text()).collect();
        format!("{}({})", fn_name(self.tag), args.join(", "))
    }

    /// What TypeScript puts in a `source` for this element: `{"site": [line, column]}`, or none.
    pub fn source_json(&self) -> Option<Json> {
        self.site.map(|s| Json::obj().with("site", vec![Json::from(s.line), Json::from(s.column)]))
    }

    /// The first value refused anywhere in the tree, by the call that wrote it.
    pub fn refusal(&self) -> Result<(), String> {
        if let Some(why) = self.refused.first() {
            return Err(format!("{}: {why}", self.call_text()));
        }
        self.child_elements().try_for_each(El::refusal)
    }
}

/// The tag of a fragment: several elements as one item of a children list, React's fragment.
pub const FRAGMENT: &str = "fragment";

/// Several elements as one item of a children list: `fragment((1..=4).map(|i| resistor(format!("R{i}"))))`.
/// The elements a closure or a loop makes are the code's to edit; the list around them stays the editor's.
pub fn fragment(children: impl IntoIterator<Item = El>) -> El {
    El { tag: FRAGMENT, attrs: Vec::new(), positional: 0, children: Vec::new(), site: None, refused: Vec::new() }
        .with_children(children.into_iter().map(Child::El))
}

// ── Values ─────────────────────────────────────────────────────────────────────────────────────────────────────────

/// An attribute's value: a number, a string, a bool, a homogeneous array of these (nested), or JSON text ([`json`]).
#[derive(Clone, Debug)]
pub struct Value(pub(crate) Result<Json, String>);

/// JSON text as a value: `.excitation(json(r#"{"acMagnitude": 1}"#))`. Text that is not JSON is refused, by the
/// attribute's name, when the document is declared.
pub fn json(text: &str) -> Value {
    Value(Json::parse(text))
}

macro_rules! value_from_number {
    ($($t:ty),*) => {$(
        impl From<$t> for Value {
            fn from(n: $t) -> Value {
                Value(Ok(Json::Num(n as f64)))
            }
        }
    )*};
}
value_from_number!(i8, i16, i32, i64, u8, u16, u32, u64, usize, isize, f64);

impl From<f32> for Value {
    /// The shortest decimal that reads back as the same `f32` (`0.1f32` is 0.1, not 0.10000000149011612).
    fn from(n: f32) -> Value {
        Value(Ok(Json::Num(n.to_string().parse::<f64>().unwrap_or(n as f64))))
    }
}
impl From<&str> for Value {
    fn from(s: &str) -> Value {
        Value(Ok(Json::from(s)))
    }
}
impl From<String> for Value {
    fn from(s: String) -> Value {
        Value(Ok(Json::from(s)))
    }
}
impl From<&String> for Value {
    fn from(s: &String) -> Value {
        Value(Ok(Json::from(s.as_str())))
    }
}
impl From<bool> for Value {
    fn from(b: bool) -> Value {
        Value(Ok(Json::Bool(b)))
    }
}
impl From<Json> for Value {
    fn from(j: Json) -> Value {
        Value(Ok(j))
    }
}
impl<T: Into<Value>, const N: usize> From<[T; N]> for Value {
    fn from(items: [T; N]) -> Value {
        array(items)
    }
}
impl<T: Into<Value>> From<Vec<T>> for Value {
    fn from(items: Vec<T>) -> Value {
        array(items)
    }
}
impl<T: Into<Value> + Clone> From<&[T]> for Value {
    fn from(items: &[T]) -> Value {
        array(items.iter().cloned())
    }
}

fn array<T: Into<Value>>(items: impl IntoIterator<Item = T>) -> Value {
    Value(items.into_iter().map(|v| v.into().0).collect::<Result<Vec<_>, _>>().map(Json::Arr))
}

// ── Children and text parameters ───────────────────────────────────────────────────────────────────────────────────

/// What a text-holding tag takes: `p("The run passed.")`, `p(("The run ", b("passed"), "."))`, `p(b("x"))`,
/// `p([b("x"), i("y")])`, `p(vec![…])`; empty: `p("")` or `p([])`.
pub trait Text {
    fn into_children(self) -> Vec<Child>;
}

/// One item of a text tuple: a text or an element.
pub trait TextPart {
    fn into_child(self) -> Child;
}

impl TextPart for &str {
    fn into_child(self) -> Child {
        Child::Text(self.to_string())
    }
}
impl TextPart for String {
    fn into_child(self) -> Child {
        Child::Text(self)
    }
}
impl TextPart for &String {
    fn into_child(self) -> Child {
        Child::Text(self.clone())
    }
}
impl TextPart for El {
    fn into_child(self) -> Child {
        Child::El(self)
    }
}

macro_rules! text_from_part {
    ($($t:ty),*) => {$(
        impl Text for $t {
            fn into_children(self) -> Vec<Child> {
                vec![self.into_child()]
            }
        }
    )*};
}
text_from_part!(&str, String, &String, El);
impl<const N: usize> Text for [El; N] {
    fn into_children(self) -> Vec<Child> {
        self.into_iter().map(Child::El).collect()
    }
}
impl Text for Vec<El> {
    fn into_children(self) -> Vec<Child> {
        self.into_iter().map(Child::El).collect()
    }
}

macro_rules! text_tuples {
    ($(($($p:ident),+)),*) => {$(
        impl<$($p: TextPart),+> Text for ($($p,)+) {
            #[allow(non_snake_case)]
            fn into_children(self) -> Vec<Child> {
                let ($($p,)+) = self;
                vec![$($p.into_child()),+]
            }
        }
    )*};
}
text_tuples!(
    (A),
    (A, B),
    (A, B, C),
    (A, B, C, D),
    (A, B, C, D, E),
    (A, B, C, D, E, F),
    (A, B, C, D, E, F, G),
    (A, B, C, D, E, F, G, H),
    (A, B, C, D, E, F, G, H, I),
    (A, B, C, D, E, F, G, H, I, J),
    (A, B, C, D, E, F, G, H, I, J, K),
    (A, B, C, D, E, F, G, H, I, J, K, L),
    (A, B, C, D, E, F, G, H, I, J, K, L, M),
    (A, B, C, D, E, F, G, H, I, J, K, L, M, N),
    (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O),
    (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P)
);

// ── Names ──────────────────────────────────────────────────────────────────────────────────────────────────────────

const KEYWORDS: &[&str] = &[
    "as", "async", "await", "box", "break", "const", "continue", "crate", "do", "dyn", "else", "enum", "extern", "false", "final", "fn", "for",
    "gen", "if", "impl", "in", "let", "loop", "macro", "match", "mod", "move", "mut", "override", "priv", "pub", "ref", "return", "self",
    "static", "struct", "super", "trait", "true", "try", "type", "typeof", "unsafe", "unsized", "use", "virtual", "where", "while", "yield",
];

/// The TypeScript name of a Rust method or parameter: `sch_x` → `schX`, `type_` → `type` (each `_x` is an uppercase X).
pub fn ts_name(rust: &str) -> String {
    let mut out = String::new();
    let mut up = false;
    for c in rust.trim_end_matches('_').chars() {
        if c == '_' {
            up = true;
        } else if up {
            out.extend(c.to_uppercase());
            up = false;
        } else {
            out.push(c);
        }
    }
    out
}

fn snake(ts: &str, first_upper_joins: bool) -> String {
    let mut out = String::new();
    for (i, c) in ts.chars().enumerate() {
        if c == '-' {
            out.push('_');
        } else if c.is_uppercase() {
            if i > 0 || !first_upper_joins {
                out.push('_');
            }
            out.extend(c.to_lowercase());
        } else {
            out.push(c);
        }
    }
    if KEYWORDS.contains(&out.as_str()) {
        out.push('_');
    }
    out
}

/// The Rust method of a TypeScript attribute name: `schX` → `sch_x`, `type` → `type_` (what a message names).
pub fn rust_name(ts: &str) -> String {
    snake(ts, false)
}

/// The constructor of a TypeScript tag: `brush-stroke` → `brush_stroke`, `CapTable` → `cap_table`, `move` → `move_`.
pub fn fn_name(tag: &str) -> String {
    snake(tag, true)
}

/// A constructor's tag: the one written in `elements!`, else the function's name without a keyword's `_`.
pub fn tag_of(function: &'static str, written: Option<&'static str>) -> &'static str {
    written.unwrap_or_else(|| function.strip_suffix('_').unwrap_or(function))
}

// ── The two macros a family module declares itself with ───────────────────────────────────────────────────────────

/// A family's constructors, one per tag:
///
/// ```text
/// elements! {
///     /// An ideal resistor.
///     resistor(name): leaf;                  // fn resistor(name: impl Into<Value>) -> El
///     group(name): children;                 // fn group(name, children: impl IntoIterator<Item = El>) -> El
///     p: text;                               // fn p(text: impl Text) -> El
///     brush_stroke = "brush-stroke": leaf;   // a tag that is not the function's name
/// }
/// ```
///
/// The parenthesised names are the tag's positional attributes (TypeScript names by [`ts_name`]); only the schematic
/// has them. Every constructor is `#[track_caller]`, so the element knows the file's call.
macro_rules! elements {
    ($( $(#[$doc:meta])* $f:ident $(= $tag:literal)? $(( $($arg:ident),* ))? : $holds:ident; )*) => {
        $( elements!(@one [$(#[$doc])*] $f [$($tag)?] [$($($arg)*)?] $holds); )*
    };
    (@one [$($doc:tt)*] $f:ident [$($tag:literal)?] [$($arg:ident)*] leaf) => {
        $($doc)*
        #[track_caller]
        pub fn $f($($arg: impl Into<$crate::design::Value>),*) -> $crate::design::El {
            elements!(@new $f [$($tag)?] [$($arg)*])
        }
    };
    (@one [$($doc:tt)*] $f:ident [$($tag:literal)?] [$($arg:ident)*] children) => {
        $($doc)*
        #[track_caller]
        pub fn $f($($arg: impl Into<$crate::design::Value>,)* children: impl IntoIterator<Item = $crate::design::El>) -> $crate::design::El {
            elements!(@new $f [$($tag)?] [$($arg)*]).with_children(children.into_iter().map($crate::design::Child::El))
        }
    };
    (@one [$($doc:tt)*] $f:ident [$($tag:literal)?] [$($arg:ident)*] text) => {
        $($doc)*
        #[track_caller]
        pub fn $f($($arg: impl Into<$crate::design::Value>,)* text: impl $crate::design::Text) -> $crate::design::El {
            elements!(@new $f [$($tag)?] [$($arg)*]).with_children(text.into_children())
        }
    };
    (@new $f:ident [$($tag:literal)?] [$($arg:ident)*]) => {{
        #[allow(unused_mut)]
        let mut el = $crate::design::El::new($crate::design::element::tag_of(stringify!($f), None $(.or(Some($tag)))?));
        $( el = el.with_attr($crate::design::element::ts_name(stringify!($arg)), $arg); el.positional += 1; )*
        el
    }};
}
pub(crate) use elements;

/// A family's attribute methods, as one trait implemented for [`El`]:
///
/// ```text
/// attributes! {
///     /// The schematic's attributes.
///     pub trait SchematicAttrs {
///         /// Where the symbol sits, across: millimetres.
///         sch_x;                          // .sch_x(v) sets "schX"
///         type_;                          // .type_(v) sets "type"
///         stroke_dash = "stroke-dash";    // a name that is not the method's camelCase
///     }
/// }
/// ```
///
/// Each method takes any [`Value`] (numbers, strings, bools, arrays of them, `json(…)`); the family checks the value
/// when it declares, and refuses by name what it cannot read.
macro_rules! attributes {
    ($(#[$tdoc:meta])* pub trait $trait:ident { $( $(#[$doc:meta])* $m:ident $(= $ts:literal)?; )* }) => {
        $(#[$tdoc])*
        pub trait $trait: Sized {
            $( $(#[$doc])* fn $m(self, value: impl Into<$crate::design::Value>) -> Self; )*
        }
        impl $trait for $crate::design::El {
            $(
                fn $m(self, value: impl Into<$crate::design::Value>) -> $crate::design::El {
                    let name: Option<&str> = None $(.or(Some($ts)))?;
                    self.with_attr(name.map(str::to_string).unwrap_or_else(|| $crate::design::element::ts_name(stringify!($m))), value)
                }
            )*
        }
    };
}
pub(crate) use attributes;

#[cfg(test)]
mod tests {
    use super::*;

    mod family {
        elements! {
            /// A leaf with two positional attributes.
            wire_(from, to): leaf;
            /// A container.
            layer: children;
            /// A text tag.
            p: text;
            /// A tag that is not the function's name.
            brush_stroke = "brush-stroke": leaf;
            /// A tag with a capital.
            cap_table = "CapTable": children;
        }
        attributes! {
            /// Attributes.
            pub trait FamilyAttrs {
                /// A camelCase name.
                frozen_rows;
                /// A keyword.
                type_;
                /// A name written beside it.
                stroke_dash = "stroke-dash";
            }
        }
    }
    use family::*;

    #[test]
    fn the_macros_declare_constructors_and_attribute_traits() {
        let w = wire_("a", 2).type_("x").frozen_rows(1).stroke_dash([2, 1]);
        assert_eq!(w.tag, "wire");
        assert_eq!(w.positional, 2);
        assert_eq!(w.call_text(), r#"wire("a", 2)"#);
        let names: Vec<&str> = w.attrs.iter().map(|(k, _)| k.as_str()).collect();
        assert_eq!(names, ["from", "to", "type", "frozenRows", "stroke-dash"]);
        assert_eq!(brush_stroke().tag, "brush-stroke");
        assert_eq!(cap_table([layer([])]).tag, "CapTable");
        assert_eq!(p(("a", brush_stroke())).children.len(), 2);
        assert!(p("").children.is_empty() && p([]).children.is_empty() && layer([]).children.is_empty());
        let line = line!() + 1;
        let el = layer([]);
        assert_eq!(el.site, Some(Site { line, column: 18 }), "the site is the call in this file, through the macro");
    }

    #[test]
    fn attr_sets_a_name_no_trait_lists() {
        // A node type of the file's own: its ports are fields no vocabulary trait names.
        let el = layer([]).attr("cutoff", 4).attr("model_number", "AR4");
        let names: Vec<&str> = el.attrs.iter().map(|(k, _)| k.as_str()).collect();
        assert_eq!(names, ["cutoff", "model_number"]);
        assert_eq!(el.attr_of("cutoff").map(Json::text), Some("4".to_string()));
        assert_eq!(el.attr_of("model_number").map(Json::text), Some("\"AR4\"".to_string()));
        // Written twice, the last value wins in the first one's place.
        let once = el.attr("cutoff", 5);
        assert_eq!(once.attrs.len(), 2);
        assert_eq!(once.attr_of("cutoff").map(Json::text), Some("5".to_string()));
    }
}