flowjs-rs-macros 0.0.0-alpha.3

Derive macro for flowjs-rs
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
//! Attribute parsing for `#[flow(...)]` and `#[serde(...)]` (when serde-compat is enabled).
//!
//! API-compatible with ts-rs: every `#[ts(...)]` attribute has a `#[flow(...)]` equivalent
//! with identical semantics. Flow-specific additions (opaque, flow_enum) extend the surface.

use syn::{Attribute, Expr, Ident, Lit, Result, Token, WherePredicate};

// ── Optional system ─────────────────────────────────────────────────────

/// Controls how `Option<T>` fields are represented in Flow.
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub enum Optional {
    /// `Option<T>` → `field?: T` (or `field?: T | null` if nullable)
    Optional { nullable: bool },
    /// Explicitly NOT optional — standard behavior.
    NotOptional,
    /// Inherit from parent (container-level `optional_fields`).
    Inherit,
}

impl Optional {
    /// Parse from `#[flow(optional)]`, `#[flow(optional = nullable)]`, `#[flow(optional = false)]`.
    pub fn parse_from_meta(meta: &syn::meta::ParseNestedMeta<'_>) -> Result<Self> {
        if meta.input.peek(Token![=]) {
            let value: Lit = meta.value()?.parse()?;
            if let Lit::Str(s) = &value {
                match s.value().as_str() {
                    "nullable" => Ok(Self::Optional { nullable: true }),
                    other => Err(meta.error(format!(
                        "unknown optional value: `{other}`. Expected \"nullable\""
                    ))),
                }
            } else if let Lit::Bool(b) = &value {
                if b.value() {
                    Ok(Self::Optional { nullable: false })
                } else {
                    Ok(Self::NotOptional)
                }
            } else {
                Err(meta.error("expected string or bool for `optional`"))
            }
        } else {
            Ok(Self::Optional { nullable: false })
        }
    }

    /// Resolve field-level optional against container-level optional_fields.
    #[allow(dead_code)]
    pub fn resolve(&self, container: &Self) -> bool {
        match self {
            Self::Optional { .. } => true,
            Self::NotOptional => false,
            Self::Inherit => matches!(container, Self::Optional { .. }),
        }
    }

    #[allow(dead_code)]
    pub fn is_nullable(&self, container: &Self) -> bool {
        match self {
            Self::Optional { nullable } => *nullable,
            Self::Inherit => match container {
                Self::Optional { nullable } => *nullable,
                _ => false,
            },
            Self::NotOptional => false,
        }
    }
}

// ── Flow enum representation ────────────────────────────────────────────

/// Flow enum representation type.
#[derive(Clone)]
pub enum FlowEnumRepr {
    Symbol,
    String,
    Number,
    Boolean,
}

// ── Container attributes ────────────────────────────────────────────────

/// Container-level attributes (`#[flow(...)]` on struct/enum).
pub struct ContainerAttr {
    pub rename: Option<String>,
    pub rename_all: Option<Inflection>,
    pub rename_all_fields: Option<Inflection>,
    pub export: bool,
    pub export_to: Option<Expr>,
    pub tag: Option<String>,
    pub content: Option<String>,
    pub untagged: bool,
    pub crate_rename: Option<syn::Path>,
    pub bound: Option<Vec<WherePredicate>>,
    /// `Some(None)` = fully opaque, `Some(Some("string"))` = bounded opaque.
    pub opaque: Option<Option<String>>,
    /// Flow enum declaration. Also set by `repr(enum)` / `repr(enum = name)`.
    pub flow_enum: Option<FlowEnumRepr>,
    /// `#[flow(type = "...")]` — raw Flow type override for the whole container.
    pub type_override: Option<String>,
    /// `#[flow(as = "...")]` — delegate to another Rust type's Flow representation.
    pub type_as: Option<syn::Type>,
    /// `#[flow(concrete(T = i32, ...))]` — replace generic params with concrete types.
    pub concrete: std::collections::HashMap<Ident, syn::Type>,
    /// `#[flow(optional_fields)]` — make all `Option<T>` fields optional.
    pub optional_fields: Optional,
}

impl ContainerAttr {
    pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
        let mut this = Self {
            rename: None,
            rename_all: None,
            rename_all_fields: None,
            export: false,
            export_to: None,
            tag: None,
            content: None,
            untagged: false,
            crate_rename: None,
            bound: None,
            opaque: None,
            flow_enum: None,
            type_override: None,
            type_as: None,
            concrete: std::collections::HashMap::new(),
            optional_fields: Optional::Inherit,
        };

        for attr in attrs {
            if attr.path().is_ident("flow") {
                attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("rename") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.rename = Some(s.value());
                        }
                    } else if meta.path.is_ident("rename_all") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.rename_all = Some(Inflection::parse(&s.value()).expect("invalid rename_all value"));
                        }
                    } else if meta.path.is_ident("rename_all_fields") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.rename_all_fields = Some(Inflection::parse(&s.value()).expect("invalid rename_all value"));
                        }
                    } else if meta.path.is_ident("export") {
                        this.export = true;
                    } else if meta.path.is_ident("export_to") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            let val = s.value();
                            this.export_to = Some(syn::parse_quote!(#val));
                        }
                    } else if meta.path.is_ident("tag") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.tag = Some(s.value());
                        }
                    } else if meta.path.is_ident("content") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.content = Some(s.value());
                        }
                    } else if meta.path.is_ident("untagged") {
                        this.untagged = true;
                    } else if meta.path.is_ident("crate") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.crate_rename = Some(s.parse()?);
                        }
                    } else if meta.path.is_ident("opaque") {
                        if meta.input.peek(Token![=]) {
                            let value: Lit = meta.value()?.parse()?;
                            if let Lit::Str(s) = value {
                                this.opaque = Some(Some(s.value()));
                            }
                        } else {
                            this.opaque = Some(None);
                        }
                    } else if meta.path.is_ident("flow_enum") {
                        if meta.input.peek(Token![=]) {
                            let value: Lit = meta.value()?.parse()?;
                            if let Lit::Str(s) = value {
                                match s.value().as_str() {
                                    "string" => this.flow_enum = Some(FlowEnumRepr::String),
                                    "symbol" => this.flow_enum = Some(FlowEnumRepr::Symbol),
                                    "number" => this.flow_enum = Some(FlowEnumRepr::Number),
                                    "boolean" => this.flow_enum = Some(FlowEnumRepr::Boolean),
                                    other => return Err(meta.error(format!(
                                        "unknown flow_enum representation: `{other}`. Expected \"string\", \"symbol\", \"number\", or \"boolean\""
                                    ))),
                                }
                            }
                        } else {
                            this.flow_enum = Some(FlowEnumRepr::Symbol);
                        }
                    } else if meta.path.is_ident("type") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.type_override = Some(s.value());
                        }
                    } else if meta.path.is_ident("as") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.type_as = Some(s.parse()?);
                        }
                    } else if meta.path.is_ident("concrete") {
                        // #[flow(concrete(T = i32, U = String))]
                        let content;
                        syn::parenthesized!(content in meta.input);
                        while !content.is_empty() {
                            let ident: Ident = content.parse()?;
                            let _: Token![=] = content.parse()?;
                            let ty: syn::Type = content.parse()?;
                            this.concrete.insert(ident, ty);
                            if content.peek(Token![,]) {
                                let _: Token![,] = content.parse()?;
                            }
                        }
                    } else if meta.path.is_ident("optional_fields") {
                        this.optional_fields = Optional::parse_from_meta(&meta)?;
                    } else if meta.path.is_ident("repr") {
                        // ts-rs compat: #[flow(repr(enum))] or #[flow(repr(enum = name))]
                        let content;
                        syn::parenthesized!(content in meta.input);
                        let ident: Ident = content.parse()?;
                        if ident != "enum" {
                            return Err(content.error("expected `enum`"));
                        }
                        if content.peek(Token![=]) {
                            let _: Token![=] = content.parse()?;
                            let value: Ident = content.parse()?;
                            if value == "name" {
                                this.flow_enum = Some(FlowEnumRepr::String);
                            } else {
                                return Err(content.error("expected `name`"));
                            }
                        } else {
                            this.flow_enum = Some(FlowEnumRepr::Number);
                        }
                    } else if meta.path.is_ident("bound") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            let where_clause: syn::WhereClause =
                                syn::parse_str(&format!("where {}", s.value()))?;
                            let preds: Vec<_> = where_clause.predicates.into_iter().collect();
                            match &mut this.bound {
                                Some(existing) => existing.extend(preds),
                                None => this.bound = Some(preds),
                            }
                        }
                    } else {
                        let path = meta
                            .path
                            .get_ident()
                            .map(|i| i.to_string())
                            .unwrap_or_else(|| "unknown".to_string());
                        return Err(meta.error(format!("unknown #[flow(...)] attribute: `{path}`")));
                    }
                    Ok(())
                })?;
            }

            #[cfg(feature = "serde-compat")]
            if attr.path().is_ident("serde") {
                attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("rename") {
                        let value: Lit = meta.value()?.parse()?;
                        if this.rename.is_none() {
                            if let Lit::Str(s) = value {
                                this.rename = Some(s.value());
                            }
                        }
                    } else if meta.path.is_ident("rename_all") {
                        let value: Lit = meta.value()?.parse()?;
                        if this.rename_all.is_none() {
                            if let Lit::Str(s) = value {
                                this.rename_all = Some(Inflection::parse(&s.value()).expect("invalid rename_all value"));
                            }
                        }
                    } else if meta.path.is_ident("rename_all_fields") {
                        let value: Lit = meta.value()?.parse()?;
                        if this.rename_all_fields.is_none() {
                            if let Lit::Str(s) = value {
                                this.rename_all_fields = Some(Inflection::parse(&s.value()).expect("invalid rename_all value"));
                            }
                        }
                    } else if meta.path.is_ident("tag") {
                        let value: Lit = meta.value()?.parse()?;
                        if this.tag.is_none() {
                            if let Lit::Str(s) = value {
                                this.tag = Some(s.value());
                            }
                        }
                    } else if meta.path.is_ident("content") {
                        let value: Lit = meta.value()?.parse()?;
                        if this.content.is_none() {
                            if let Lit::Str(s) = value {
                                this.content = Some(s.value());
                            }
                        }
                    } else if meta.path.is_ident("untagged") {
                        if !this.untagged {
                            this.untagged = true;
                        }
                    } else {
                        // Skip other serde attributes — consume = value if present
                        let _ = meta.value().and_then(|v| v.parse::<Lit>()).ok();
                    }
                    Ok(())
                })?;
            }
        }

        Ok(this)
    }

    /// Apply `rename_all` inflection to a field name.
    pub fn rename_field(&self, raw: &str) -> String {
        match &self.rename_all {
            Some(inflection) => inflection.apply(raw),
            None => raw.to_owned(),
        }
    }

    /// Apply `rename_all` inflection to a variant name.
    pub fn rename_variant(&self, raw: &str) -> String {
        match &self.rename_all {
            Some(inflection) => inflection.apply(raw),
            None => raw.to_owned(),
        }
    }

    /// Apply `rename_all_fields` inflection to a field inside an enum variant.
    /// Falls back to `rename_all` if `rename_all_fields` is not set.
    pub fn rename_variant_field(&self, raw: &str) -> String {
        match &self.rename_all_fields {
            Some(inflection) => inflection.apply(raw),
            None => self.rename_field(raw),
        }
    }
}

// ── Field attributes ────────────────────────────────────────────────────

/// Field-level attributes (`#[flow(...)]` on struct fields).
pub struct FieldAttr {
    pub rename: Option<String>,
    pub type_override: Option<String>,
    pub type_as: Option<syn::Type>,
    pub skip: bool,
    pub optional: Optional,
    pub inline: bool,
    pub flatten: bool,
    /// Add the `+` (covariant/readonly) prefix on this field.
    /// Default: false (matches ts-rs behavior — fields are mutable by default).
    pub readonly: bool,
    /// serde: `skip_serializing_if` or `skip_serializing` seen
    pub maybe_omitted: bool,
    /// serde: `default` seen
    #[allow(dead_code)]
    pub has_default: bool,
}

impl FieldAttr {
    pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
        let mut this = Self {
            rename: None,
            type_override: None,
            type_as: None,
            skip: false,
            optional: Optional::Inherit,
            inline: false,
            flatten: false,
            readonly: false,
            maybe_omitted: false,
            has_default: false,
        };

        for attr in attrs {
            if attr.path().is_ident("flow") {
                attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("rename") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.rename = Some(s.value());
                        }
                    } else if meta.path.is_ident("type") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.type_override = Some(s.value());
                        }
                    } else if meta.path.is_ident("as") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.type_as = Some(s.parse()?);
                        }
                    } else if meta.path.is_ident("skip") {
                        this.skip = true;
                    } else if meta.path.is_ident("optional") {
                        this.optional = Optional::parse_from_meta(&meta)?;
                    } else if meta.path.is_ident("inline") {
                        this.inline = true;
                    } else if meta.path.is_ident("flatten") {
                        this.flatten = true;
                    } else if meta.path.is_ident("readonly") {
                        this.readonly = true;
                    } else {
                        let path = meta
                            .path
                            .get_ident()
                            .map(|i| i.to_string())
                            .unwrap_or_else(|| "unknown".to_string());
                        return Err(
                            meta.error(format!("unknown #[flow(...)] field attribute: `{path}`"))
                        );
                    }
                    Ok(())
                })?;
            }

            #[cfg(feature = "serde-compat")]
            if attr.path().is_ident("serde") {
                attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("rename") {
                        let value: Lit = meta.value()?.parse()?;
                        if this.rename.is_none() {
                            if let Lit::Str(s) = value {
                                this.rename = Some(s.value());
                            }
                        }
                    } else if meta.path.is_ident("skip") {
                        if !this.skip {
                            this.skip = true;
                        }
                    } else if meta.path.is_ident("skip_serializing") {
                        this.maybe_omitted = true;
                    } else if meta.path.is_ident("skip_serializing_if") {
                        let _ = meta.value().and_then(|v| v.parse::<Lit>()).ok();
                        this.maybe_omitted = true;
                    } else if meta.path.is_ident("default") {
                        let _ = meta.value().and_then(|v| v.parse::<Lit>()).ok();
                        this.has_default = true;
                    } else if meta.path.is_ident("flatten") {
                        if !this.flatten {
                            this.flatten = true;
                        }
                    } else {
                        // Skip other serde attributes — consume = value if present
                        let _ = meta.value().and_then(|v| v.parse::<Lit>()).ok();
                    }
                    Ok(())
                })?;
            }
        }

        Ok(this)
    }

    /// Whether this field should be key-optional (`field?:`).
    pub fn is_optional(&self, container_optional: &Optional) -> bool {
        match &self.optional {
            Optional::Optional { .. } => true,
            Optional::NotOptional => false,
            Optional::Inherit => {
                // Inherit from container, or fall back to serde heuristic
                if matches!(container_optional, Optional::Optional { .. }) {
                    true
                } else {
                    self.is_serde_optional()
                }
            }
        }
    }

    /// Whether this field can be absent in serialized output based on serde attributes.
    pub fn is_serde_optional(&self) -> bool {
        self.maybe_omitted
    }
}

// ── Variant attributes ──────────────────────────────────────────────────

/// Variant-level attributes (`#[flow(...)]` on enum variants).
pub struct VariantAttr {
    pub rename: Option<String>,
    pub skip: bool,
    /// `#[flow(type = "...")]` — override variant type.
    pub type_override: Option<String>,
    /// `#[flow(as = "...")]` — delegate to another type.
    pub type_as: Option<syn::Type>,
    /// `#[flow(rename_all = "...")]` — per-variant field renaming.
    pub rename_all: Option<Inflection>,
    /// `#[flow(inline)]` — inline variant type definition.
    pub inline: bool,
    /// `#[flow(untagged)]` — make this specific variant untagged.
    pub untagged: bool,
    /// `#[flow(optional_fields)]` — per-variant optional field behavior.
    pub optional_fields: Optional,
}

impl VariantAttr {
    pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
        let mut this = Self {
            rename: None,
            skip: false,
            type_override: None,
            type_as: None,
            rename_all: None,
            inline: false,
            untagged: false,
            optional_fields: Optional::Inherit,
        };

        for attr in attrs {
            if attr.path().is_ident("flow") {
                attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("rename") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.rename = Some(s.value());
                        }
                    } else if meta.path.is_ident("skip") {
                        this.skip = true;
                    } else if meta.path.is_ident("type") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.type_override = Some(s.value());
                        }
                    } else if meta.path.is_ident("as") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.type_as = Some(s.parse()?);
                        }
                    } else if meta.path.is_ident("rename_all") {
                        let value: Lit = meta.value()?.parse()?;
                        if let Lit::Str(s) = value {
                            this.rename_all = Some(Inflection::parse(&s.value()).expect("invalid rename_all value"));
                        }
                    } else if meta.path.is_ident("inline") {
                        this.inline = true;
                    } else if meta.path.is_ident("untagged") {
                        this.untagged = true;
                    } else if meta.path.is_ident("optional_fields") {
                        this.optional_fields = Optional::parse_from_meta(&meta)?;
                    } else {
                        let path = meta
                            .path
                            .get_ident()
                            .map(|i| i.to_string())
                            .unwrap_or_else(|| "unknown".to_string());
                        return Err(
                            meta.error(format!("unknown #[flow(...)] variant attribute: `{path}`"))
                        );
                    }
                    Ok(())
                })?;
            }

            #[cfg(feature = "serde-compat")]
            if attr.path().is_ident("serde") {
                attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("rename") {
                        let value: Lit = meta.value()?.parse()?;
                        if this.rename.is_none() {
                            if let Lit::Str(s) = value {
                                this.rename = Some(s.value());
                            }
                        }
                    } else if meta.path.is_ident("skip") {
                        if !this.skip {
                            this.skip = true;
                        }
                    } else if meta.path.is_ident("untagged") {
                        if !this.untagged {
                            this.untagged = true;
                        }
                    } else if meta.path.is_ident("rename_all") {
                        let value: Lit = meta.value()?.parse()?;
                        if this.rename_all.is_none() {
                            if let Lit::Str(s) = value {
                                this.rename_all = Some(Inflection::parse(&s.value()).expect("invalid rename_all value"));
                            }
                        }
                    } else {
                        // Skip other serde attributes — consume = value if present
                        let _ = meta.value().and_then(|v| v.parse::<Lit>()).ok();
                    }
                    Ok(())
                })?;
            }
        }

        Ok(this)
    }
}

// ── Inflection ──────────────────────────────────────────────────────────

/// Re-exported from `derive-inflection` crate.
pub use derive_inflection::Inflection;