myrmic-common 0.7.0

Wire types shared across the Myrmic WASM host/guest boundary (db, cells, BLE, web), plus the host-only code generator behind myrmic-sdk-macros and the cell build tooling.
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//! Code generation for `import!`-ed bridge specs.
//!
//! Two entry points, one per bridge kind:
//!
//! * [`http_bridge`] — the request/response payload types come from the
//!   bridge's embedded **JSON Schema** and are generated with [`typify`]. Each
//!   endpoint becomes one fire-and-forget client method.
//! * [`mqtt_bridge`] — MQTT specs carry no schema; their payload types are
//!   still derived from the `${kind:name}` template placeholders, via the
//!   [`crate::codegen::cell_api`] model.
//!
//! Both emit a `<Name>Client` whose methods dispatch commands with
//! `myrmic_sdk::send`, plus the payload/event types those methods reference.
//!
//! [`typify`]: typify_impl

use std::collections::{BTreeMap, HashMap};

use heck::ToUpperCamelCase;
use proc_macro2::TokenStream as Ts;
use quote::{format_ident, quote};

use crate::codegen::bridge_api::{UserHttpBridgeApi, UserHttpEndpoint, UserMqttBridge};
use crate::codegen::cell_api::{ApiCommand, ApiEvent, ApiField, ApiType, CellApi};
use crate::codegen::status::status_variant_name;
use crate::codegen::template::{RawSeg, Seg, Segments};

mod schema;

/// Generates the client + payload/reply types for an HTTP bridge.
///
/// The bridge's `types` JSON Schema is run through `typify` to produce the Rust
/// payload types. Each endpoint becomes one client method whose positional
/// arguments are the endpoint's request placeholders (`${kind:name}` in the
/// path/query/headers, then the body), followed by a typed `Callback` naming
/// the handler the HTTP reply is delivered to. `db:` placeholders resolve
/// runtime-side and take no argument.
///
/// Each endpoint also gets a generated `<Endpoint>Reply` enum built from its
/// response template — one variant per listed status code plus `Unknown(u16)`:
/// it is the payload the callback handler decodes, and the `T` in `Callback<T>`.
///
/// The wire payload is a JSON object keyed by placeholder name (the runtime
/// decodes it by name, so field order is irrelevant) plus a reserved
/// `__callback` key carrying the callback command name; a private
/// `__<Endpoint>Payload` struct encodes exactly that.
pub fn http_bridge(root: &Ts, api: UserHttpBridgeApi) -> Result<Ts, String> {
    let UserHttpBridgeApi {
        name,
        base_url: _, // resolved on the runtime side, not the client
        types,
        endpoints,
    } = api;

    // Turn the JSON Schema into Rust types (and a name -> ident lookup so the
    // placeholders can refer to them).
    let (type_defs, type_idents) = match types {
        Some(schema) => schema::generate(root, schema)?,
        None => (Ts::new(), BTreeMap::new()),
    };

    let mut defs = Vec::new();
    let mut methods = Vec::new();
    for endpoint in &endpoints {
        let (reply_def, payload_def, method) = http_endpoint(root, endpoint, &type_idents)?;
        defs.push(reply_def);
        defs.push(payload_def);
        methods.push(method);
    }

    let client = client_struct(&name, &methods);

    Ok(quote! {
        #type_defs
        #(#defs)*
        #client
    })
}

/// Generates one endpoint's `<Endpoint>Reply` type, its private wire-payload
/// struct, and the client method that ties them together.
fn http_endpoint(
    root: &Ts,
    endpoint: &UserHttpEndpoint,
    type_idents: &BTreeMap<String, Ts>,
) -> Result<(Ts, Ts, Ts), String> {
    let camel = endpoint.id.to_upper_camel_case();
    let reply_ident = format_ident!("{camel}Reply");
    let payload_ident = format_ident!("__{camel}Payload");

    // A generated Reply must not clash with a schema type of the same name.
    if type_idents.contains_key(&format!("{camel}Reply")) {
        return Err(format!(
            "endpoint `{}` generates `{camel}Reply`, which collides with a `types` definition of the same name",
            endpoint.id
        ));
    }

    let params = request_params(endpoint)?;

    // `__callback` is the reserved wire key carrying the caller's callback command
    // name; a request placeholder of that name would collide with it.
    if params.iter().any(|(_, n)| n == "__callback") {
        return Err(format!(
            "endpoint `{}` has a request placeholder named `__callback`, which is reserved",
            endpoint.id
        ));
    }

    let mut sig = Vec::new();
    let mut field_decls = Vec::new();
    let mut inits = Vec::new();
    for (kind, name) in &params {
        let (s, f, i) = request_param_tokens(root, kind, name, type_idents);
        sig.push(s);
        field_decls.push(f);
        inits.push(i);
    }
    if let Some(body) = &endpoint.request.body
        && let Some((kind, name)) = body.0.split()
    {
        if params.iter().any(|(_, n)| n == "body") {
            return Err(format!(
                "endpoint `{}` has both a `body` and a request parameter named `body`",
                endpoint.id
            ));
        }
        let (s, f, i) = body_param_tokens(root, kind, name, type_idents);
        sig.push(s);
        field_decls.push(f);
        inits.push(i);
    }

    let reply_def = reply_enum(root, &reply_ident, endpoint, type_idents)?;

    let serde_crate = format!("{root}::codegen::exports::serde");

    // Private: only this module's methods build it. JSON-encoding the struct (the
    // `Message` default) is the wire object the runtime's `decode_vals` reads back
    // by field name; the reserved `__callback` key carries the caller's callback
    // command name, which the runtime strips before matching request placeholders.
    let payload_def = quote! {
        #[derive(
            #root::codegen::exports::serde::Serialize,
            #root::codegen::exports::serde::Deserialize,
            #root::Message,
        )]
        #[serde(crate = #serde_crate)]
        struct #payload_ident {
            #(#field_decls,)*
            __callback: #root::String,
        }
    };

    let method_ident = format_ident!("{}", endpoint.id);
    let command_name = &endpoint.id;
    let method = quote! {
        pub fn #method_ident(&self, #(#sig,)* cb: #root::Callback<#reply_ident>) -> #root::Result<()> {
            let sri = #root::Sri::from_target(self.target)
                .map_err(|_| "invalid bridge target")?;
            let __cb: #root::Command = cb.into();
            let __payload = #payload_ident {
                #(#inits,)*
                __callback: __cb.as_ref().into(),
            };
            #root::send(sri, #command_name, &__payload)
        }
    };

    Ok((reply_def, payload_def, method))
}

/// The request placeholders as `(kind, name)` in the runtime's decode order
/// (path, then query, then headers), rejecting a name used more than once
/// (the wire expects one value per placeholder occurrence). `db:`/raw segments
/// carry no value and are skipped.
fn request_params(endpoint: &UserHttpEndpoint) -> Result<Vec<(String, String)>, String> {
    let req = &endpoint.request;
    let mut params: Vec<(String, String)> = Vec::new();
    let mut push = |kind: &str, name: &str| -> Result<(), String> {
        if params.iter().any(|(_, n)| n == name) {
            return Err(format!(
                "request placeholder `{name}` appears more than once in endpoint `{}`; each parameter must be unique",
                endpoint.id
            ));
        }
        params.push((kind.to_string(), name.to_string()));
        Ok(())
    };
    for seg in req.path.0.iter() {
        if let Some((kind, name)) = seg.split() {
            push(kind, name)?;
        }
    }
    for segs in req.query.values() {
        for seg in segs.0.iter() {
            if let Some((kind, name)) = seg.split() {
                push(kind, name)?;
            }
        }
    }
    for segs in req.headers.values() {
        for seg in segs.0.iter() {
            if let Some((kind, name)) = seg.split() {
                push(kind, name)?;
            }
        }
    }
    Ok(params)
}

/// A request placeholder's `(signature arg, payload field, payload init)`
/// tokens. `json` args are handed by reference and stored as a nested JSON value
/// (`to_value`) so the runtime reads them back as an object; scalars are taken by
/// value, `string` by `&str`.
fn request_param_tokens(
    root: &Ts,
    kind: &str,
    name: &str,
    type_idents: &BTreeMap<String, Ts>,
) -> (Ts, Ts, Ts) {
    let ident = format_ident!("{name}");
    match kind {
        "bool" => scalar(&ident, &quote! { bool }),
        "u8" => scalar(&ident, &quote! { u8 }),
        "u16" => scalar(&ident, &quote! { u16 }),
        "u32" => scalar(&ident, &quote! { u32 }),
        "u64" => scalar(&ident, &quote! { u64 }),
        "i8" => scalar(&ident, &quote! { i8 }),
        "i16" => scalar(&ident, &quote! { i16 }),
        "i32" => scalar(&ident, &quote! { i32 }),
        "i64" => scalar(&ident, &quote! { i64 }),
        "f32" => scalar(&ident, &quote! { f32 }),
        "f64" => scalar(&ident, &quote! { f64 }),
        "json" => {
            let jt = json_ref_type(root, name, type_idents);
            (
                quote! { #ident: &#jt },
                quote! { #ident: #root::JsonValue },
                quote! { #ident: #root::codegen::exports::serde_json::to_value(#ident)
                .map_err(|_| "unable to serialise json argument")? },
            )
        }
        // "string" and anything unknown fall back to the SDK string type.
        _ => (
            quote! { #ident: &str },
            quote! { #ident: #root::String },
            quote! { #ident: #ident.into() },
        ),
    }
}

/// The body placeholder's `(signature arg, payload field, payload init)`, always
/// named `body`. `json` is stored as a nested JSON value, `bytes` as a byte vector.
fn body_param_tokens(
    root: &Ts,
    kind: &str,
    name: &str,
    type_idents: &BTreeMap<String, Ts>,
) -> (Ts, Ts, Ts) {
    match kind {
        "json" => {
            let jt = json_ref_type(root, name, type_idents);
            (
                quote! { body: &#jt },
                quote! { body: #root::JsonValue },
                quote! { body: #root::codegen::exports::serde_json::to_value(body)
                .map_err(|_| "unable to serialise json body")? },
            )
        }
        "bytes" => (
            quote! { body: &[u8] },
            quote! { body: #root::Vec<u8> },
            quote! { body: body.to_vec() },
        ),
        _ => (
            quote! { body: &str },
            quote! { body: #root::String },
            quote! { body: body.into() },
        ),
    }
}

/// A scalar arg is taken and stored by value; its payload init is a field
/// shorthand.
fn scalar(ident: &syn::Ident, ty: &Ts) -> (Ts, Ts, Ts) {
    (
        quote! { #ident: #ty },
        quote! { #ident: #ty },
        quote! { #ident },
    )
}

/// The `<Endpoint>Reply` enum, built from the response template: one variant per
/// listed status code (named by its canonical reason — `200` -> `Ok`, `404` ->
/// `NotFound`), plus `Unknown(u16)` for any status the bridge sees that the spec
/// doesn't list.
///
/// A status entry that is only a body becomes a tuple variant carrying that body;
/// one with headers becomes a struct variant (a `String` field per header, plus a
/// `body` field when a body is templated); one with neither becomes a unit variant.
///
/// JSON codec (the `Message` default): the runtime builds the reply from the
/// response template without a schema, emitting the externally-tagged serde form
/// of the matching variant; a typed `body` then deserialises from the nested value.
fn reply_enum(
    root: &Ts,
    reply_ident: &syn::Ident,
    endpoint: &UserHttpEndpoint,
    type_idents: &BTreeMap<String, Ts>,
) -> Result<Ts, String> {
    let mut variants = Vec::new();
    let mut seen: Vec<String> = Vec::new();

    for (code, variant) in &endpoint.response {
        let name = status_variant_name(*code)
            .map_err(|err| format!("endpoint `{}`: {err}", endpoint.id))?;
        if seen.iter().any(|s| s == &name) {
            return Err(format!(
                "endpoint `{}` lists two statuses that map to variant `{name}`",
                endpoint.id
            ));
        }
        seen.push(name.clone());
        let ident = format_ident!("{name}");
        let variant = &variant.0; // unwrap the `ParseInto` shorthand wrapper

        let mut header_fields = Vec::new();
        let mut header_names: Vec<String> = Vec::new();
        for hdr in variant.headers.values() {
            if let Some((_, hname)) = hdr.0.split() {
                if header_names.iter().any(|n| n == hname) {
                    return Err(format!(
                        "endpoint `{}` status `{code}` uses response placeholder `{hname}` more than once",
                        endpoint.id
                    ));
                }
                header_names.push(hname.to_string());
                let field = format_ident!("{hname}");
                header_fields.push(quote! { #field: #root::String });
            }
        }

        let body_ty =
            variant
                .body
                .as_ref()
                .and_then(|b| b.0.split())
                .map(|(kind, bname)| match kind {
                    "json" => json_ref_type(root, bname, type_idents),
                    "bytes" => quote! { #root::Vec<u8> },
                    _ => quote! { #root::String },
                });

        if !header_fields.is_empty() {
            if let Some(body_ty) = body_ty {
                header_fields.push(quote! { body: #body_ty });
            }
            variants.push(quote! { #ident { #(#header_fields),* } });
        } else if let Some(body_ty) = body_ty {
            variants.push(quote! { #ident(#body_ty) });
        } else {
            variants.push(quote! { #ident });
        }
    }

    variants.push(quote! { Unknown(u16) });

    let serde_crate = format!("{root}::codegen::exports::serde");
    Ok(quote! {
        #[derive(
            Debug,
            Clone,
            #root::codegen::exports::serde::Serialize,
            #root::codegen::exports::serde::Deserialize,
            #root::Message,
        )]
        #[serde(crate = #serde_crate)]
        pub enum #reply_ident {
            #(#variants,)*
        }
    })
}

/// A `json` placeholder's Rust type: the schema definition it names, or an
/// untyped `myrmic_sdk::JsonValue` when no such definition exists.
fn json_ref_type(root: &Ts, name: &str, type_idents: &BTreeMap<String, Ts>) -> Ts {
    type_idents
        .get(name)
        .cloned()
        .unwrap_or_else(|| quote! { #root::JsonValue })
}

/// Generates the client + payload/event types for an MQTT bridge.
///
/// MQTT egresses become fire-and-forget `<id>` client methods; ingresses
/// become event payload types (implementing `myrmic_sdk::CellEvent`).
pub fn mqtt_bridge(root: &Ts, api: UserMqttBridge) -> Result<Ts, String> {
    let cell_api = convert_mqtt(api)?;
    Ok(cell_api_tokens(root, &cell_api))
}

// ---------------------------------------------------------------------------
// Shared client / method generation
// ---------------------------------------------------------------------------

/// Emits a single fire-and-forget client method that sends the command named
/// `name` to the bridge's target SRI, optionally carrying a typed payload.
/// `by_value` selects the payload calling convention: `false` takes `&Ty`
/// (HTTP), `true` takes `Ty` by value (MQTT).
fn command_method(root: &Ts, name: &str, arg_ty: Option<&Ts>, by_value: bool) -> Ts {
    let method = format_ident!("{}", name);
    let command_name = name;

    let resolve_sri = quote! {
        let sri = #root::Sri::from_target(self.target)
            .map_err(|_| "invalid bridge target")?;
    };

    match arg_ty {
        Some(ty) if by_value => quote! {
            pub fn #method(&self, value: #ty) -> #root::Result<()> {
                #resolve_sri
                #root::send(sri, #command_name, &value)
            }
        },
        Some(ty) => quote! {
            pub fn #method(&self, value: &#ty) -> #root::Result<()> {
                #resolve_sri
                #root::send(sri, #command_name, value)
            }
        },
        None => quote! {
            pub fn #method(&self) -> #root::Result<()> {
                #resolve_sri
                #root::send(sri, #command_name, &#root::Void)
            }
        },
    }
}

/// Wraps the generated methods in a `<Name>Client` that stores the bridge's
/// target (an SRI or resolvable SRN string).
fn client_struct(bridge_name: &str, methods: &[Ts]) -> Ts {
    let client = format_ident!("{}Client", bridge_name.to_upper_camel_case());
    quote! {
        pub struct #client {
            target: &'static str,
        }

        impl #client {
            /// Binds the client to a bridge cell, named by SRI or SRN string.
            pub const fn new(target: &'static str) -> Self {
                Self { target }
            }

            #(#methods)*
        }
    }
}

// ---------------------------------------------------------------------------
// MQTT: template-placeholder -> cell_api -> tokens
// ---------------------------------------------------------------------------

/// Generates the struct/event types and client for a `CellApi` (MQTT path).
fn cell_api_tokens(root: &Ts, api: &CellApi) -> Ts {
    let mut type_defs = Vec::new();
    if let Some(types) = &api.types {
        for (name, ty) in types {
            type_defs.push(struct_def(root, name, ty, /* event */ None));
        }
    }

    let mut event_defs = Vec::new();
    for (name, event) in &api.events {
        event_defs.push(struct_def(root, name, event.as_ref(), Some(name)));
    }

    let mut methods = Vec::new();
    for (name, cmd) in &api.commands {
        methods.extend(command_methods(root, name, cmd));
    }

    let client = client_struct(&api.cell, &methods);

    quote! {
        #(#type_defs)*
        #(#event_defs)*
        #client
    }
}

/// A struct type from an [`ApiType`]. When `event_name` is `Some`, also emits a
/// `myrmic_sdk::CellEvent` impl so the type can be used as an event payload.
///
/// serde is reached through `myrmic_sdk::codegen::exports` (both the derive path and
/// the `#[serde(crate = …)]` container attr) so the cell needs no direct serde
/// dependency — mirroring what the typify path does for schema types.
fn struct_def(root: &Ts, name: &str, ty: &ApiType, event_name: Option<&str>) -> Ts {
    let ident = format_ident!("{}", name);
    let serde_crate = format!("{root}::codegen::exports::serde");

    let fields = ty.fields.iter().map(|f| {
        let field_ident = format_ident!("{}", f.name);
        let field_ty: syn::Type =
            syn::parse_str(&f.field_type).unwrap_or_else(|_| syn::parse_quote!(#root::String));
        quote! { pub #field_ident: #field_ty }
    });

    let event_impl = event_name.map(|ev| {
        quote! {
            impl #root::CellEvent for #ident {
                fn event_name() -> &'static str { #ev }
            }
        }
    });

    quote! {
        #[derive(
            Debug,
            Clone,
            #root::codegen::exports::serde::Serialize,
            #root::codegen::exports::serde::Deserialize,
            #root::Message,
        )]
        #[serde(crate = #serde_crate)]
        pub struct #ident {
            #(#fields,)*
        }
        #event_impl
    }
}

/// Generates the client method for one command. Commands are fire-and-forget,
/// so there is a single dispatch shape, named after the command itself.
fn command_methods(root: &Ts, name: &str, cmd: &ApiCommand) -> Vec<Ts> {
    let arg_ty = cmd
        .args
        .as_deref()
        .filter(|s| *s != "None")
        .map(|s| format_ident!("{}", s));

    // MQTT call sites pass the payload by value.
    let arg = arg_ty.as_ref().map(|ty| quote! { #ty });
    let method = command_method(root, name, arg.as_ref(), /* by_value */ true);

    vec![method]
}

/// Converts a `UserMqttBridge` into a `CellApi`: egresses -> commands (+ arg
/// types), ingresses -> events. Ported from the original `import!` macro.
fn convert_mqtt(api: UserMqttBridge) -> Result<CellApi, String> {
    let UserMqttBridge {
        name,
        broker_url: _,
        ingress,
        egress,
    } = api;

    let name = name.to_upper_camel_case();

    let mut types: HashMap<String, ApiType> = HashMap::new();
    let mut events: HashMap<String, ApiEvent> = HashMap::new();
    let mut commands: HashMap<String, ApiCommand> = HashMap::new();

    for egress in egress {
        let cmd_name = egress.id.to_upper_camel_case();

        let mut fields = FieldCollector::default();
        fields.scan_segments(&egress.topic.0)?;
        fields.push(&egress.payload.0)?;

        types.insert(
            cmd_name.clone(),
            ApiType {
                description: None,
                fields: fields.into_fields(),
            },
        );

        commands.insert(
            egress.id,
            ApiCommand {
                description: None,
                args: Some(cmd_name),
            },
        );
    }

    for ingress in ingress {
        let event_name = ingress.id.to_upper_camel_case();

        let mut fields = FieldCollector::default();
        fields.push(&ingress.payload.0)?;

        events.insert(
            event_name,
            ApiEvent(ApiType {
                description: None,
                fields: fields.into_fields(),
            }),
        );
    }

    Ok(CellApi {
        cell: name,
        types: if types.is_empty() { None } else { Some(types) },
        commands,
        events,
    })
}

/// Collects distinct `${kind:name}` placeholders into typed fields.
#[derive(Default)]
struct FieldCollector {
    fields: Vec<ApiField>,
    seen: HashMap<String, String>,
}

impl FieldCollector {
    fn scan_segments<S: RawSeg>(&mut self, segments: &Segments<S>) -> Result<(), String> {
        for seg in segments.iter() {
            self.push(seg)?;
        }
        Ok(())
    }

    fn push<S: Seg>(&mut self, seg: &S) -> Result<(), String> {
        let Some((ty, name)) = seg.split() else {
            return Ok(());
        };
        if ty == "db" {
            return Ok(());
        }

        if let Some(prev) = self.seen.insert(name.to_string(), ty.to_string())
            && prev != ty
        {
            return Err(format!("placeholder `{name}` used with conflicting types"));
        }

        self.fields.push(ApiField {
            name: name.to_string(),
            serde_with: None,
            field_type: map_placeholder_type(ty).to_string(),
            description: None,
        });

        Ok(())
    }

    fn into_fields(self) -> Vec<ApiField> {
        self.fields
    }
}

fn map_placeholder_type(ty: &str) -> &'static str {
    match ty {
        "bool" => "bool",
        "u8" => "u8",
        "u16" => "u16",
        "u32" => "u32",
        "u64" => "u64",
        "i8" => "i8",
        "i16" => "i16",
        "i32" => "i32",
        "i64" => "i64",
        "f32" => "f32",
        "f64" => "f64",
        "bytes" => "::myrmic_sdk::Bytes",
        "json" => "::myrmic_sdk::JsonValue",
        // "string" and anything unknown fall back to the SDK string type.
        _ => "::myrmic_sdk::String",
    }
}

#[cfg(test)]
mod tests;