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
//! A PDF ASSEMBLED IN RUST — a `.pdf.rs`: pages taken from PDFs by ref, blank pages, comments, stamps, signatures,
//! redactions and form fields on them, form values, bookmarks, page labels and attachments. The same elements, and the
//! same documents, as the TypeScript SDK's `pdf.ts` and the Python SDK's `pdf.py`:
//!
//! ```
//! use commandagi::design::pdf::*;
//!
//! fn document() -> El {
//!     pdf([
//!         page([]).src("Contract.pdf").n(1),
//!         page([
//!             highlight().rects([[72, 700, 300, 712]]).author("Ada").text("Check this"),
//!             note([reply().text("Because.").author("Bob")]).at([500, 700]).text("Why?"),
//!             stamp().rect([400, 40, 560, 90]).name("Approved"),
//!         ])
//!         .src("Contract.pdf")
//!         .n(3)
//!         .rotate(90),
//!         page([]).size("a4"),
//!         fill().name("Name").value("Ada Lovelace"),
//!         bookmark([bookmark([]).title("Payment").page(2).top(500)]).title("Terms").page(2),
//!         footer().center("Page {page} of {pages}").pages("2-"),
//!         bates().prefix("ACME-").digits(6).position("bottom-right"),
//!     ])
//!     .title("Signed contract")
//! }
//! # commandagi::design::declare(document()).unwrap();
//! ```
//!
//! Pages and page numbers are 1-based; coordinates are PDF points from the page's lower-left corner; a colour is
//! `[r, g, b]`, each 0..1; a `src` is a ref relative to the file's folder. A header or footer is text in its `left`,
//! `center` and `right` slots with `{page}`, `{pages}`, `{date}` and `{file}` in it, on every page or `pages`; Bates
//! numbers run from `start`. Nothing adds a default. A run gives back
//! `{format, document, sources}` beside an empty graph.

use super::documents::{declare_document, DocTree, TagRule, Vocabulary};
use super::element::{attributes, elements};
use super::{Declared, Family};
pub use super::{fragment, json, El, Json, Value};

elements! {
    /// A PDF: its pages, form values, bookmarks, labels and attachments.
    pdf: children;
    /// A page of another PDF (`.src("Contract.pdf").n(3)`) or a blank page (`.size("a4")`), holding its marks.
    page: children;
    /// Marks the text under `.rects([...])`.
    highlight: leaf;
    underline: leaf;
    strikeout: leaf;
    squiggly: leaf;
    /// A sticky note at `.at([x, y])`, holding its replies.
    note: children;
    /// A reply in a note's thread: `.text(…)`; `.state("Completed")` resolves the thread.
    reply: leaf;
    /// A text box: `.rect([...]).text(…)`.
    textbox: leaf;
    /// Freehand strokes: `.strokes([[x, y, x, y], …])`.
    ink: leaf;
    rectangle: leaf;
    ellipse: leaf;
    /// A line or an arrow: `.from([x, y]).to([x, y]).arrow("end")`.
    line: leaf;
    polygon: leaf;
    polyline: leaf;
    /// A stamp: `.rect([...]).name("Approved")`, or `.image("seal.png")`.
    stamp: leaf;
    /// A picture of a signature: `.typed(…)`, `.image(…)` or `.strokes(…)` (0..1 in its rect).
    signature: leaf;
    /// What is under `.rect([...])` is removed when the PDF is built.
    redact: leaf;
    /// A form field: `.kind("text").name("Name").rect([...])`.
    field: leaf;
    /// A form field's value: `.name("Name").value("Ada")`.
    fill: leaf;
    /// A bookmark to `.page(n)`, holding its children.
    bookmark: children;
    /// Page labels from `.from(n)`: `.style("r")`.
    label: leaf;
    /// An attachment: `.src("data.csv")`.
    attach: leaf;
    /// Text at the top of every page (or `.pages("2-")`): `.left(…)`, `.center(…)`, `.right(…)`.
    header: leaf;
    /// Text at the foot of every page: `.center("Page {page} of {pages}")`.
    footer: leaf;
    /// A Bates number on every page: `.prefix("ACME-").start(1).digits(6).position("bottom-right")`.
    bates: leaf;
}

attributes! {
    /// The attributes of a PDF's elements, chained on them.
    pub trait PdfAttrs {
        title; author; subject; keywords; creator;
        id; src; n; rotate; size; width; height;
        text; color; opacity; date; rects; at; icon; open; rect; text_color; border; fill; align;
        strokes; from; to; arrow; points; name; label; image; typed; style; overlay;
        kind; value; options; checked; multiline; max_length; required; read_only; tooltip; default_; editable; state;
        page; top; bold; italic; url; prefix; start; description; mime_type;
        left; center; right; family; margin; inset; pages; suffix; digits; position;
    }
}

pub(crate) const FAMILY: Family = Family { module: "pdf", roots: &["pdf"], declare };

const PAGE_SIZES: &[&str] = &["a3", "a4", "a5", "letter", "legal", "tabloid"];
const PDF_FIELDS: &[&str] = &["title", "author", "subject", "keywords", "creator"];
const PAGE_FIELDS: &[&str] = &["id", "src", "n", "rotate", "size", "width", "height"];
const BOOKMARK_FIELDS: &[&str] = &["id", "title", "page", "top", "open", "bold", "italic", "color", "url"];
const LABEL_FIELDS: &[&str] = &["from", "style", "prefix", "start"];
const ATTACH_FIELDS: &[&str] = &["src", "name", "description", "mimeType"];
const BAND_FIELDS: &[&str] = &["left", "center", "right", "size", "color", "family", "margin", "inset", "pages", "start", "date"];
const BATES_FIELDS: &[&str] = &["prefix", "suffix", "start", "digits", "position", "size", "color", "family", "margin", "inset"];
const BATES_POSITIONS: &[&str] = &["top-left", "top-center", "top-right", "bottom-left", "bottom-center", "bottom-right"];
/// A review state a reply sets on its thread: "Completed" is resolved, "None" reopens it.
pub const REVIEW_STATES: &[&str] = &["Accepted", "Rejected", "Cancelled", "Completed", "None"];
const TEXT_MARK: &[&str] = &["id", "author", "text", "color", "opacity", "date", "rects"];

const PDF: Vocabulary = Vocabulary {
    format: "pdf",
    noun: "a PDF",
    root: "pdf",
    tags: &[
        TagRule::new("pdf", &[]).attrs(PDF_FIELDS),
        TagRule::new("page", &["pdf"]).key("id").attrs(PAGE_FIELDS),
        TagRule::new("highlight", &["page"]).key("id").required(&["rects"]).attrs(TEXT_MARK),
        TagRule::new("underline", &["page"]).key("id").required(&["rects"]).attrs(TEXT_MARK),
        TagRule::new("strikeout", &["page"]).key("id").required(&["rects"]).attrs(TEXT_MARK),
        TagRule::new("squiggly", &["page"]).key("id").required(&["rects"]).attrs(TEXT_MARK),
        TagRule::new("note", &["page"]).key("id").required(&["at"]).attrs(&["id", "author", "text", "color", "opacity", "date", "at", "icon", "open"]),
        TagRule::new("textbox", &["page"]).key("id").required(&["rect", "text"]).attrs(&["id", "author", "text", "color", "opacity", "date", "rect", "size", "textColor", "border", "fill", "align"]),
        TagRule::new("ink", &["page"]).key("id").required(&["strokes"]).attrs(&["id", "author", "text", "color", "opacity", "date", "strokes", "width"]),
        TagRule::new("rectangle", &["page"]).key("id").required(&["rect"]).attrs(&["id", "author", "text", "color", "opacity", "date", "rect", "width", "fill"]),
        TagRule::new("ellipse", &["page"]).key("id").required(&["rect"]).attrs(&["id", "author", "text", "color", "opacity", "date", "rect", "width", "fill"]),
        TagRule::new("line", &["page"]).key("id").required(&["from", "to"]).attrs(&["id", "author", "text", "color", "opacity", "date", "from", "to", "width", "arrow"]),
        TagRule::new("polygon", &["page"]).key("id").required(&["points"]).attrs(&["id", "author", "text", "color", "opacity", "date", "points", "width", "fill"]),
        TagRule::new("polyline", &["page"]).key("id").required(&["points"]).attrs(&["id", "author", "text", "color", "opacity", "date", "points", "width"]),
        TagRule::new("stamp", &["page"]).key("id").required(&["rect"]).attrs(&["id", "author", "text", "color", "opacity", "date", "rect", "name", "label", "image"]),
        TagRule::new("signature", &["page"]).key("id").required(&["rect"]).attrs(&["id", "author", "rect", "typed", "style", "image", "strokes", "color", "width"]),
        TagRule::new("redact", &["page"]).key("id").required(&["rect"]).attrs(&["id", "rect", "fill", "overlay"]),
        TagRule::new("field", &["page"]).key("id").required(&["kind", "name"]).attrs(&["id", "kind", "name", "rect", "rects", "value", "options", "checked", "multiline", "maxLength", "required", "readOnly", "tooltip", "default", "editable", "size"]),
        TagRule::new("reply", &["note"]).required(&["text"]).attrs(&["text", "author", "date", "state"]),
        TagRule::new("fill", &["pdf"]).key("name").required(&["name", "value"]).attrs(&["name", "value"]),
        TagRule::new("bookmark", &["pdf", "bookmark"]).key("id").required(&["title"]).attrs(BOOKMARK_FIELDS),
        TagRule::new("label", &["pdf"]).required(&["from"]).attrs(LABEL_FIELDS),
        TagRule::new("attach", &["pdf"]).required(&["src"]).attrs(ATTACH_FIELDS),
        TagRule::new("header", &["pdf"]).attrs(BAND_FIELDS),
        TagRule::new("footer", &["pdf"]).attrs(BAND_FIELDS),
        TagRule::new("bates", &["pdf"]).attrs(BATES_FIELDS),
    ],
    from_tree: pdf_of,
};

fn declare(root: El) -> Result<Declared, String> {
    declare_document(&root, &PDF)
}

fn num(v: Option<&Json>) -> Option<f64> {
    v.and_then(|v| v.as_f64()).filter(|n| n.is_finite())
}
fn is_int(v: Option<&Json>) -> bool {
    num(v).is_some_and(|n| n.fract() == 0.0)
}
fn nums(v: &Json, len: Option<usize>) -> bool {
    matches!(v, Json::Arr(a) if len.is_none_or(|l| a.len() == l) && a.iter().all(|x| x.as_f64().is_some_and(f64::is_finite)))
}
fn rect(v: Option<&Json>) -> bool {
    v.is_some_and(|v| nums(v, Some(4)))
}
fn color(v: &Json) -> bool {
    nums(v, Some(3)) && matches!(v, Json::Arr(a) if a.iter().all(|x| x.as_f64().is_some_and(|n| (0.0..=1.0).contains(&n))))
}

fn own(t: &DocTree, keys: &[&str]) -> Json {
    let mut out = Json::obj();
    for k in keys {
        if let Some(v) = t.attr_of(k) {
            out.set(k, v.clone());
        }
    }
    out
}

fn check_mark(c: &DocTree) -> Result<(), String> {
    let w = format!("{}()", c.tag);
    if c.attr_of("rect").is_some() && !rect(c.attr_of("rect")) {
        return Err(format!("{w}: rect is [x0, y0, x1, y1] in points"));
    }
    if let Some(r) = c.attr_of("rects") {
        if !matches!(r, Json::Arr(a) if !a.is_empty() && a.iter().all(|x| nums(x, Some(4)))) {
            return Err(format!("{w}: rects is a list of [x0, y0, x1, y1]"));
        }
    }
    for k in ["at", "from", "to"] {
        if c.attr_of(k).is_some_and(|v| !nums(v, Some(2))) {
            return Err(format!("{w}: {k} is [x, y] in points"));
        }
    }
    for k in ["color", "textColor"] {
        if c.attr_of(k).is_some_and(|v| !color(v)) {
            return Err(format!("{w}: {k} is [r, g, b], each 0 to 1"));
        }
    }
    for k in ["fill", "border"] {
        if c.attr_of(k).is_some_and(|v| *v != Json::Null && !color(v)) {
            return Err(format!("{w}: {k} is [r, g, b], each 0 to 1, or null"));
        }
    }
    if c.attr_of("opacity").is_some() && !num(c.attr_of("opacity")).is_some_and(|n| (0.0..=1.0).contains(&n)) {
        return Err(format!("{w}: opacity is 0 to 1"));
    }
    if let Some(s) = c.attr_of("strokes") {
        if !matches!(s, Json::Arr(a) if a.iter().all(|x| nums(x, None) && matches!(x, Json::Arr(p) if p.len() >= 2 && p.len() % 2 == 0))) {
            return Err(format!("{w}: strokes is a list of [x, y, x, y …]"));
        }
    }
    if let Some(p) = c.attr_of("points") {
        if !(nums(p, None) && matches!(p, Json::Arr(a) if a.len() >= 4 && a.len() % 2 == 0)) {
            return Err(format!("{w}: points is [x, y, x, y …]"));
        }
    }
    if c.attr_of("arrow").is_some_and(|a| !["none", "start", "end", "both"].contains(&a.as_str().unwrap_or(""))) {
        return Err(format!(r#"{w}: arrow is "none", "start", "end" or "both""#));
    }
    if c.tag == "field" {
        let kind = c.attr_of("kind").and_then(Json::as_str).unwrap_or("");
        if !["text", "checkbox", "radio", "dropdown", "list", "signature"].contains(&kind) {
            return Err(r#"field(): kind is "text", "checkbox", "radio", "dropdown", "list" or "signature""#.into());
        }
        if kind == "radio" {
            let len = |k: &str| match c.attr_of(k) {
                Some(Json::Arr(a)) => Some(a.len()),
                _ => None,
            };
            if len("options").is_none() || len("options") != len("rects") {
                return Err(r#"field(): kind "radio" has options and rects, one rect per option"#.into());
            }
        } else if !rect(c.attr_of("rect")) {
            return Err("field(): rect is [x0, y0, x1, y1] in points".into());
        }
    }
    if c.tag == "signature" && ["typed", "image", "strokes"].iter().filter(|k| c.attr_of(k).is_some()).count() != 1 {
        return Err("signature(): is one of typed, image or strokes".into());
    }
    if c.tag == "stamp" && !["name", "image", "label"].iter().any(|k| c.attr_of(k).is_some()) {
        return Err("stamp(): has a name (Approved, Draft …), a label or an image".into());
    }
    for k in ["text", "author", "name", "label", "image", "typed", "overlay", "tooltip"] {
        if c.attr_of(k).is_some_and(|v| !matches!(v, Json::Str(_))) {
            return Err(format!("{w}: {k} is text"));
        }
    }
    for k in ["required", "readOnly", "multiline", "checked", "editable"] {
        if c.attr_of(k).is_some_and(|v| !matches!(v, Json::Bool(_))) {
            return Err(format!("{w}: {k} is true or false"));
        }
    }
    if c.attr_of("default").is_some_and(|v| !matches!(v, Json::Str(_)) && !matches!(v, Json::Arr(a) if a.iter().all(|x| matches!(x, Json::Str(_))))) {
        return Err(format!("{w}: default is text, or a list of texts"));
    }
    for r in c.children.iter().filter(|r| r.tag == "reply") {
        if r.attr_of("state").is_some_and(|s| !REVIEW_STATES.contains(&s.as_str().unwrap_or(""))) {
            return Err(r#"reply(): state is "Accepted", "Rejected", "Cancelled", "Completed" or "None""#.into());
        }
    }
    Ok(())
}

fn check_page(p: &DocTree) -> Result<(), String> {
    if let Some(src) = p.attr_of("src") {
        if !matches!(src, Json::Str(s) if !s.is_empty()) {
            return Err("page(): src is the ref of a PDF".into());
        }
        if !(is_int(p.attr_of("n")) && num(p.attr_of("n")).unwrap_or(0.0) >= 1.0) {
            return Err("page(): n is the page's number in src, from 1".into());
        }
        if ["size", "width", "height"].iter().any(|k| p.attr_of(k).is_some()) {
            return Err("page(): is a page of src, or a blank page of a size: not both".into());
        }
    } else {
        if p.attr_of("n").is_some() {
            return Err("page(): n is the page number in src".into());
        }
        match p.attr_of("size") {
            Some(s) if !PAGE_SIZES.contains(&s.as_str().unwrap_or("")) => return Err(format!("page(): size is {}", PAGE_SIZES.join(", "))),
            None if !(num(p.attr_of("width")).is_some_and(|w| w > 0.0) && num(p.attr_of("height")).is_some_and(|h| h > 0.0)) => {
                return Err("page(): is a page of a PDF (src and n) or a blank page (size, or width and height in points)".into())
            }
            _ => {}
        }
    }
    if p.attr_of("rotate").is_some() && !(is_int(p.attr_of("rotate")) && num(p.attr_of("rotate")).unwrap_or(1.0) % 90.0 == 0.0) {
        return Err("page(): rotate is a multiple of 90".into());
    }
    p.children.iter().try_for_each(check_mark)
}

fn check_band(t: &DocTree) -> Result<(), String> {
    let w = format!("{}()", t.tag);
    if t.tag != "bates" && !["left", "center", "right"].iter().any(|k| matches!(t.attr_of(k), Some(Json::Str(s)) if !s.trim().is_empty())) {
        return Err(format!("{w}: has text in left, center or right"));
    }
    for k in ["left", "center", "right", "pages", "date", "prefix", "suffix"] {
        if t.attr_of(k).is_some_and(|v| !matches!(v, Json::Str(_))) {
            return Err(format!("{w}: {k} is text"));
        }
    }
    for k in ["size", "margin", "inset"] {
        if t.attr_of(k).is_some() && !num(t.attr_of(k)).is_some_and(|n| n >= 0.0) {
            return Err(format!("{w}: {k} is points, 0 or more"));
        }
    }
    if t.attr_of("color").is_some_and(|v| !color(v)) {
        return Err(format!("{w}: color is [r, g, b], each 0 to 1"));
    }
    if t.attr_of("family").is_some_and(|f| !["sans", "serif", "mono"].contains(&f.as_str().unwrap_or(""))) {
        return Err(format!(r#"{w}: family is "sans", "serif" or "mono""#));
    }
    if t.attr_of("start").is_some() && !(is_int(t.attr_of("start")) && num(t.attr_of("start")).unwrap_or(-1.0) >= 0.0) {
        return Err(format!("{w}: start is a whole number"));
    }
    if t.attr_of("digits").is_some() && !(is_int(t.attr_of("digits")) && num(t.attr_of("digits")).is_some_and(|d| (1.0..=15.0).contains(&d))) {
        return Err("bates(): digits is 1 to 15".into());
    }
    if t.attr_of("position").is_some_and(|p| !BATES_POSITIONS.contains(&p.as_str().unwrap_or(""))) {
        return Err(format!("bates(): position is {}", BATES_POSITIONS.join(", ")));
    }
    Ok(())
}

fn bookmark_of(t: &DocTree) -> Result<Json, String> {
    if !matches!(t.attr_of("title"), Some(Json::Str(_))) {
        return Err("bookmark(): title is text".into());
    }
    if t.attr_of("page").is_some() && !(is_int(t.attr_of("page")) && num(t.attr_of("page")).unwrap_or(0.0) >= 1.0) {
        return Err("bookmark(): page is a page number, from 1".into());
    }
    let mut b = own(t, BOOKMARK_FIELDS);
    let kids = t.children.iter().filter(|c| c.tag == "bookmark").map(bookmark_of).collect::<Result<Vec<_>, _>>()?;
    if !kids.is_empty() {
        b.set("children", kids);
    }
    Ok(b)
}

fn pdf_of(t: &DocTree) -> Result<Json, String> {
    for k in PDF_FIELDS {
        if t.attr_of(k).is_some_and(|v| !matches!(v, Json::Str(_))) {
            return Err(format!("pdf(): {k} is text"));
        }
    }
    let (mut pages, mut fills, mut bookmarks, mut labels, mut attachments) = (vec![], vec![], vec![], vec![], vec![]);
    let mut bands: Vec<(&str, Json)> = vec![];
    for c in &t.children {
        match c.tag {
            "page" => {
                check_page(c)?;
                let mut page = own(c, PAGE_FIELDS);
                let marks: Vec<Json> = c
                    .children
                    .iter()
                    .map(|m| {
                        let mut mark = Json::obj().with("type", m.tag);
                        for (k, v) in &m.attrs {
                            mark.set(k, v.clone());
                        }
                        let replies: Vec<Json> = m.children.iter().filter(|r| r.tag == "reply").map(DocTree::record).collect();
                        if !replies.is_empty() {
                            mark.set("replies", replies);
                        }
                        mark
                    })
                    .collect();
                if !marks.is_empty() {
                    page.set("marks", marks);
                }
                pages.push(page);
            }
            "fill" => {
                let ok = match c.attr_of("value") {
                    Some(Json::Str(_) | Json::Bool(_)) => true,
                    Some(Json::Arr(a)) => a.iter().all(|x| matches!(x, Json::Str(_))),
                    _ => false,
                };
                if !ok {
                    return Err("fill(): value is text, true or false, or a list of texts".into());
                }
                fills.push(own(c, &["name", "value"]));
            }
            "bookmark" => bookmarks.push(bookmark_of(c)?),
            "label" => {
                if !(is_int(c.attr_of("from")) && num(c.attr_of("from")).unwrap_or(0.0) >= 1.0) {
                    return Err("label(): from is a page number, from 1".into());
                }
                if c.attr_of("style").is_some_and(|s| !["D", "r", "R", "a", "A"].contains(&s.as_str().unwrap_or(""))) {
                    return Err(r#"label(): style is "D" (1 2 3), "r" (i ii), "R" (I II), "a" (a b) or "A" (A B)"#.into());
                }
                labels.push(own(c, LABEL_FIELDS));
            }
            "attach" => attachments.push(own(c, ATTACH_FIELDS)),
            "header" | "footer" | "bates" => {
                if bands.iter().any(|(t, _)| *t == c.tag) {
                    return Err(format!("pdf(): a PDF has one {}()", c.tag));
                }
                check_band(c)?;
                bands.push((c.tag, own(c, if c.tag == "bates" { BATES_FIELDS } else { BAND_FIELDS })));
            }
            _ => {}
        }
    }
    let mut out = own(t, PDF_FIELDS).with("pages", pages);
    for (k, v) in [("fill", fills), ("bookmarks", bookmarks), ("labels", labels), ("attachments", attachments)] {
        if !v.is_empty() {
            out.set(k, v);
        }
    }
    for tag in ["header", "footer", "bates"] {
        if let Some((_, b)) = bands.iter().find(|(t, _)| *t == tag) {
            out.set(tag, b.clone());
        }
    }
    Ok(out)
}