tilo-resource-macros 0.5.2

Procedural macros for tilo-resource.
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
//! `tilo_resource::bindings!`: a provider's actions, as a caller in Rust calls them, from the
//! compiled surface the provider publishes.
//!
//! The provider declares its actions once, in its code (`#[derive(Effect)]`), and its test
//! writes the surface that declaration publishes. A caller names that file and the provider's
//! id, and gets, at build time, what it needs to call each action and read its answer:
//!
//! - `PROVIDER`, and per resource kind a module (`environment` for `environment`) with its
//!   `RESOURCE` and, per action, the action's id (`BRANCH_MACHINE`) and its target
//!   (`branch_machine()`);
//! - per action that takes parameters, its arguments as a struct (`BranchMachine`), its fields
//!   the parameters, an optional one an `Option`; one that takes a secret does not print;
//! - per resource kind whose surface reads as fields, that reading as a record named after the
//!   kind (`Models` for `models`);
//! - the records and enums the parameters and answers name, declared once at the top. A list,
//!   a map, a flag or an option a record leaves out reads as empty, as its provider writes it.
//!
//! A caller uses the few actions it calls, so nothing generated is dead code when unused.
//!
//! Nothing it writes is committed: the surface is the one declaration, and a changed surface
//! changes the bindings with the next build.

use proc_macro2::{Span, TokenStream as Tokens};
use quote::{format_ident, quote};
use serde_json::Value;
use syn::parse::{Parse, ParseStream};
use syn::{Ident, LitStr, Token};

/// `provider = "…", surface = "…"`.
pub struct Input {
    provider: LitStr,
    surface: LitStr,
}

impl Parse for Input {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut provider = None;
        let mut surface = None;
        while !input.is_empty() {
            let key: Ident = input.parse()?;
            input.parse::<Token![=]>()?;
            let value: LitStr = input.parse()?;
            match key.to_string().as_str() {
                "provider" => provider = Some(value),
                "surface" => surface = Some(value),
                _ => {
                    return Err(syn::Error::new(
                        key.span(),
                        "expected `provider` or `surface`",
                    ));
                }
            }
            if !input.is_empty() {
                input.parse::<Token![,]>()?;
            }
        }
        let missing = |what: &str| syn::Error::new(Span::call_site(), format!("missing `{what}`"));
        Ok(Self {
            provider: provider.ok_or_else(|| missing("provider"))?,
            surface: surface.ok_or_else(|| missing("surface"))?,
        })
    }
}

/// The bindings `input` names.
pub fn expand(input: &Input) -> syn::Result<Tokens> {
    let error = |message: String| syn::Error::new(input.surface.span(), message);
    let dir = std::env::var_os("CARGO_MANIFEST_DIR")
        .map(std::path::PathBuf::from)
        .ok_or_else(|| error("no CARGO_MANIFEST_DIR to find the surface from".into()))?;
    let path = dir.join(input.surface.value());
    let json = std::fs::read_to_string(&path)
        .map_err(|why| error(format!("{}: {why}", path.display())))?;
    let surface: Value =
        serde_json::from_str(&json).map_err(|why| error(format!("{}: {why}", path.display())))?;
    if tilo_canonical_json::to_string(&surface) != json {
        return Err(error(format!(
            "{}: not the canonical compiled form of a surface; its provider's test writes it",
            path.display()
        )));
    }
    let tracked = path.display().to_string();
    let provider = &input.provider;
    let mut named = Vec::new();
    let mut declared = std::collections::BTreeSet::new();
    let mut modules = Vec::new();
    for resource in list(&surface, "resources") {
        for ty in list(resource, "types") {
            let name = text(ty, "name");
            if declared.insert(name.to_owned()) {
                named.push(named_type(ty).map_err(error)?);
            }
        }
        if let Some(reading) = reading(resource).map_err(error)? {
            named.push(reading);
        }
        modules.push(resource_module(resource).map_err(error)?);
    }
    let unused = unused();
    let items = named
        .into_iter()
        .chain(modules)
        .map(|item| quote!(#unused #item));
    Ok(quote! {
        const _: &[u8] = ::core::include_bytes!(#tracked);
        /// The provider these actions are asked of.
        #unused
        pub const PROVIDER: &str = #provider;
        #(#items)*
    })
}

fn list<'a>(value: &'a Value, key: &str) -> &'a [Value] {
    value
        .get(key)
        .and_then(Value::as_array)
        .map_or(&[], Vec::as_slice)
}

fn text<'a>(value: &'a Value, key: &str) -> &'a str {
    value.get(key).and_then(Value::as_str).unwrap_or_default()
}

/// `kebab-case`, `snake_case` or `dotted.name` as `CamelCase`.
fn camel(name: &str) -> String {
    name.split(|c: char| !c.is_ascii_alphanumeric())
        .filter(|part| !part.is_empty())
        .map(|part| {
            let mut chars = part.chars();
            chars.next().map_or_else(String::new, |first| {
                first.to_ascii_uppercase().to_string() + chars.as_str()
            })
        })
        .collect()
}

/// A wire name as a Rust identifier, raw where it is a keyword: `signIns` is `sign_ins`.
fn snake(name: &str) -> Ident {
    let mut snake = String::with_capacity(name.len().saturating_mul(2));
    let mut lower_before = false;
    for c in name.chars() {
        if c.is_ascii_uppercase() && lower_before {
            snake.push('_');
        }
        lower_before = c.is_ascii_lowercase() || c.is_ascii_digit();
        snake.push(if c.is_ascii_alphanumeric() {
            c.to_ascii_lowercase()
        } else {
            '_'
        });
    }
    syn::parse_str::<Ident>(&snake).unwrap_or_else(|_| format_ident!("r#{}", snake))
}

/// A field type of the surface as the Rust type its JSON reads into.
fn rust_type(ty: &Value) -> Result<Tokens, String> {
    // A surface leaves out a type the schema says all of: any value.
    if ty.is_null() {
        return Ok(quote!(::serde_json::Value));
    }
    if let Some(name) = ty.as_str() {
        return Ok(match name {
            "text" | "secret" | "rich_text" | "identifier" | "actor" | "date" | "time" => {
                quote!(::std::string::String)
            }
            "int" | "duration" => quote!(i64),
            "float" => quote!(f64),
            "bool" => quote!(bool),
            "any" | "json" | "reactions" => quote!(::serde_json::Value),
            other => return Err(format!("a field type the bindings do not know: {other}")),
        });
    }
    let Some((kind, inner)) = ty.as_object().and_then(|object| object.iter().next()) else {
        return Err(format!("a field type the bindings do not know: {ty}"));
    };
    Ok(match kind.as_str() {
        "resource" => quote!(::std::string::String),
        "named" => {
            let name = format_ident!("{}", inner.as_str().unwrap_or_default());
            quote!(#name)
        }
        "option" => {
            let inner = rust_type(inner)?;
            quote!(::core::option::Option<#inner>)
        }
        "list" | "set" | "collection" => {
            let inner = rust_type(inner)?;
            quote!(::std::vec::Vec<#inner>)
        }
        "map" => {
            let inner = rust_type(inner)?;
            quote!(::std::collections::BTreeMap<::std::string::String, #inner>)
        }
        "union" | "outbox" => quote!(::serde_json::Value),
        other => return Err(format!("a field type the bindings do not know: {other}")),
    })
}

/// One field: its wire name, its type, and whether it may be left out. In an enum's case it
/// is written without `pub`, which a variant's fields may not carry.
fn field(
    name: &str,
    ty: &Value,
    optional: bool,
    doc: &str,
    public: bool,
) -> Result<Tokens, String> {
    let ident = snake(name);
    let mut rust = rust_type(ty)?;
    let is_option = ty.get("option").is_some();
    let rename = (ident.to_string().trim_start_matches("r#") != name)
        .then(|| quote!(#[serde(rename = #name)]));
    let attrs = if is_option {
        quote!(#[serde(default, skip_serializing_if = "::core::option::Option::is_none")])
    } else if optional {
        rust = quote!(::core::option::Option<#rust>);
        quote!(#[serde(default, skip_serializing_if = "::core::option::Option::is_none")])
    } else if empty_when_left_out(ty) {
        quote!(#[serde(default)])
    } else {
        Tokens::new()
    };
    let doc = if doc.is_empty() { name } else { doc };
    let public = public.then(|| quote!(pub));
    Ok(quote! {
        #[doc = #doc]
        #rename
        #attrs
        #public #ident: #rust,
    })
}

/// Whether a value of the field type `ty` that its record leaves out reads as empty: a list, a
/// map or a flag, which a provider leaves out when it is empty or off.
fn empty_when_left_out(ty: &Value) -> bool {
    ty.as_str() == Some("bool")
        || ["list", "set", "collection", "map"]
            .iter()
            .any(|kind| ty.get(kind).is_some())
}

const DERIVES: &str = "Clone, Debug, PartialEq, ::serde::Serialize, ::serde::Deserialize";

fn derives() -> Tokens {
    let derives: Tokens = DERIVES.parse().unwrap_or_default();
    quote!(#[derive(#derives)])
}

/// What lets a caller leave unused what it does not call.
fn unused() -> Tokens {
    quote!(#[allow(
        dead_code,
        reason = "a caller calls the few actions it needs of all those a surface declares"
    )])
}

/// The record a resource kind reads as, named after the kind, when its surface lists fields.
fn reading(resource: &Value) -> Result<Option<Tokens>, String> {
    let fields = list(resource, "fields");
    if fields.is_empty() {
        return Ok(None);
    }
    let kind = text(resource, "kind");
    let ident = format_ident!("{}", camel(kind));
    let fields = fields
        .iter()
        .map(|one| {
            field(
                text(one, "path"),
                &one["type"],
                false,
                text(one, "title"),
                true,
            )
        })
        .collect::<Result<Vec<_>, _>>()?;
    let doc = format!("What `{kind}` reads as.");
    let derives = derives();
    Ok(Some(quote! {
        #[doc = #doc]
        #derives
        pub struct #ident { #(#fields)* }
    }))
}

/// A record or enum the surface declares by name.
fn named_type(ty: &Value) -> Result<Tokens, String> {
    let name = text(ty, "name");
    let ident = format_ident!("{}", name);
    let doc = match text(ty, "doc") {
        "" => name,
        doc => doc,
    };
    let derives = derives();
    let cases = list(ty, "cases");
    if cases.is_empty() {
        let fields = list(ty, "fields")
            .iter()
            .map(|one| {
                field(
                    text(one, "name"),
                    &one["type"],
                    false,
                    text(one, "title"),
                    true,
                )
            })
            .collect::<Result<Vec<_>, _>>()?;
        return Ok(quote! {
            #[doc = #doc]
            #derives
            pub struct #ident { #(#fields)* }
        });
    }
    let variants = cases
        .iter()
        .map(|case| {
            let wire = text(case, "name");
            let variant = format_ident!("{}", camel(wire));
            let label = match text(case, "label") {
                "" => wire,
                label => label,
            };
            let fields = list(case, "fields")
                .iter()
                .map(|one| {
                    field(
                        text(one, "name"),
                        &one["type"],
                        false,
                        text(one, "title"),
                        false,
                    )
                })
                .collect::<Result<Vec<_>, _>>()?;
            let fields = (!fields.is_empty()).then(|| quote!({ #(#fields)* }));
            Ok(quote! {
                #[doc = #label]
                #[serde(rename = #wire)]
                #variant #fields,
            })
        })
        .collect::<Result<Vec<_>, String>>()?;
    let tag = ty
        .get("tag")
        .and_then(Value::as_str)
        .map(|tag| quote!(#[serde(tag = #tag)]));
    Ok(quote! {
        #[doc = #doc]
        #derives
        #tag
        pub enum #ident { #(#variants)* }
    })
}

/// A resource kind's module: its name, and each action's id, target and arguments.
fn resource_module(resource: &Value) -> Result<Tokens, String> {
    let kind = text(resource, "kind");
    let module = snake(kind);
    let mut items = Vec::new();
    for verb in list(resource, "verbs") {
        let id = text(verb, "id");
        let constant = format_ident!("{}", id.to_uppercase().replace(['-', '.'], "_"));
        let target = snake(id);
        let label = match text(verb, "label") {
            "" => id,
            label => label,
        };
        items.push(quote! {
            #[doc = #label]
            pub const #constant: &str = #id;
            #[doc = #label]
            #[must_use]
            pub fn #target() -> ::tilo_actions::remote::RemoteActionTarget {
                ::tilo_actions::remote::RemoteActionTarget {
                    provider: super::PROVIDER.to_owned(),
                    address: ::tilo_actions::api::ActionAddress::new(RESOURCE, #constant),
                }
            }
        });
        let params = list(verb, "params");
        if params.is_empty() {
            continue;
        }
        let name = format_ident!("{}", camel(id));
        let fields = params
            .iter()
            .map(|param| {
                let required = param.get("required").and_then(Value::as_bool) == Some(true);
                field(
                    text(param, "name"),
                    &param["type"],
                    !required,
                    text(&param["schema"], "description"),
                    true,
                )
            })
            .collect::<Result<Vec<_>, _>>()?;
        let doc = format!("What `{id}` takes.");
        // A secret it takes is handed over once: arguments holding one do not print.
        let derives = if params.iter().any(|param| param["type"] == "secret") {
            quote!(#[derive(Clone, PartialEq, ::serde::Serialize, ::serde::Deserialize)])
        } else {
            derives()
        };
        items.push(quote! {
            #[doc = #doc]
            #derives
            pub struct #name { #(#fields)* }
        });
    }
    let doc = format!("The actions of `{kind}`.");
    Ok(quote! {
        #[doc = #doc]
        pub mod #module {
            #[allow(unused_imports)]
            use super::*;
            /// The resource these actions are of.
            pub const RESOURCE: &str = #kind;
            #(#items)*
        }
    })
}