ploidy-codegen-rust 0.14.2

Rust codegen for the Ploidy OpenAPI compiler
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
use std::{collections::BTreeSet, fmt::Write, num::NonZeroUsize, ops::Deref};

use ploidy_core::{
    arena::Arena,
    ir::{
        ContainerView, CookedGraph, EnumVariant, EnumView, HasResource, HasTypeId,
        InlineTypePathRoot, InlineTypePathSegment, InlineTypePathView, InlineTypeView, OperationId,
        OperationUsage, PrimitiveType, SchemaTypeView, StructFieldName, StructView, TaggedView,
        TypeId, TypeView, UntaggedView, View,
    },
    parse::ParameterLocation,
};
use rustc_hash::FxHashMap;

use super::{
    config::{CodegenConfig, DateTimeFormat},
    naming::{CodegenIdentUsage, ResourceGroup, UniqueIdent, UniqueIdents},
};

/// A [`CookedGraph`] decorated with Rust-specific information.
#[derive(Debug)]
pub struct CodegenGraph<'a> {
    cooked: CookedGraph<'a>,
    idents: IdentMap<'a>,
    date_time_format: DateTimeFormat,
}

impl<'a> CodegenGraph<'a> {
    /// Wraps a type graph with the default configuration.
    #[inline]
    pub fn new(cooked: CookedGraph<'a>) -> Self {
        Self::with_config(cooked, &CodegenConfig::default())
    }

    /// Wraps a type graph with the given configuration.
    #[inline]
    pub fn with_config(cooked: CookedGraph<'a>, config: &CodegenConfig) -> Self {
        let idents = ident_map(&cooked);
        Self {
            cooked,
            idents,
            date_time_format: config.date_time_format,
        }
    }

    /// Returns the unique Rust identifier for a schema, operation, parameter,
    /// field, or variant.
    #[inline]
    pub fn ident(&self, key: impl Into<IdentMapping<'a>>) -> UniqueIdent<'a> {
        use {IdentMapKey as Key, IdentMapping::*};
        match key.into() {
            Operation(op) => self.idents[&Key::Operation(op)],
            Path(op, name) => self.idents[&Key::Parameter(op, ParameterLocation::Path, name)],
            Query(op, name) => self.idents[&Key::Parameter(op, ParameterLocation::Query, name)],
            Type(id) => self.idents[&Key::Type(id)],
            StructField(id, name) => self.idents[&Key::StructField(id, name)],
            EnumVariant(id, name) => self.idents[&Key::EnumVariant(id, name)],
            TaggedVariant(id, name) => self.idents[&Key::TaggedVariant(id, name)],
            UntaggedVariant(id, index) => self.idents[&Key::UntaggedVariant(id, index)],
            Resource(name) => self.idents[&IdentMapKey::Resource(name)],
        }
    }

    /// Returns the resource that contains the given view.
    #[inline]
    pub fn resource_for(&self, view: &impl HasResource<'a>) -> ResourceGroup<'a> {
        view.resource()
            .map(|name| ResourceGroup::Named(self.idents[&IdentMapKey::Resource(name)]))
            .unwrap_or_default()
    }

    /// Returns the format to use for `date-time` types.
    #[inline]
    pub fn date_time_format(&self) -> DateTimeFormat {
        self.date_time_format
    }
}

impl<'a> Deref for CodegenGraph<'a> {
    type Target = CookedGraph<'a>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.cooked
    }
}

/// An item with a uniquified Rust identifier in a [`CodegenGraph`].
pub enum IdentMapping<'a> {
    /// A schema or inline type.
    Type(TypeId),
    /// An operation method.
    Operation(&'a OperationId),
    /// A path parameter for an operation.
    Path(&'a OperationId, &'a str),
    /// A query parameter for an operation.
    Query(&'a OperationId, &'a str),
    /// A struct field.
    StructField(TypeId, StructFieldName<'a>),
    /// A string enum variant.
    EnumVariant(TypeId, &'a str),
    /// A tagged union variant.
    TaggedVariant(TypeId, &'a str),
    /// An untagged union variant.
    UntaggedVariant(TypeId, NonZeroUsize),
    /// A resource name for a type or an operation.
    Resource(&'a str),
}

impl<'a> From<&'a OperationId> for IdentMapping<'a> {
    #[inline]
    fn from(id: &'a OperationId) -> Self {
        Self::Operation(id)
    }
}

impl<'a> From<TypeId> for IdentMapping<'a> {
    #[inline]
    fn from(id: TypeId) -> Self {
        Self::Type(id)
    }
}

/// Builds the identifier table for every name that Rust code generation emits.
///
/// Names are assigned in dependency order. Schema types and operations are
/// uniquified first, then inline types are named from their paths, and finally
/// inline type members.
fn ident_map<'a>(cooked: &CookedGraph<'a>) -> IdentMap<'a> {
    let mut idents = FxHashMap::default();
    idents.extend({
        let mut scope = UniqueIdents::new(cooked.arena());
        cooked
            .schemas()
            .map(move |ty| (IdentMapKey::Type(ty.id()), scope.claim(ty.name())))
    });
    idents.extend({
        let mut scope = UniqueIdents::new(cooked.arena());
        cooked
            .operations()
            .map(move |op| (IdentMapKey::Operation(op.id()), scope.claim(op.id())))
    });
    idents.extend({
        let resources: BTreeSet<_> = cooked
            .operations()
            .filter_map(|op| op.resource())
            .chain(cooked.schemas().filter_map(|ty| ty.resource()))
            .collect();
        // Resources become feature names; `default` is a special feature name.
        let mut scope = UniqueIdents::with_reserved(cooked.arena(), &["default"]);
        resources
            .into_iter()
            .map(move |name| (IdentMapKey::Resource(name), scope.claim(name)))
    });
    for op in cooked.operations() {
        {
            // Path parameters become arguments, so we need to reserve
            // local variable and argument names that we use in the
            // generated operation method body.
            let mut scope = UniqueIdents::with_reserved(
                cooked.arena(),
                &["query", "request", "form", "url", "response"],
            );
            for param in op.path().params() {
                let ident = scope.claim(param.name());
                idents.insert(
                    IdentMapKey::Parameter(op.id(), ParameterLocation::Path, param.name()),
                    ident,
                );
            }
        }
        {
            // Query parameters become regular struct fields.
            let mut scope = UniqueIdents::new(cooked.arena());
            for param in op.query() {
                let ident = scope.claim(param.name());
                idents.insert(
                    IdentMapKey::Parameter(op.id(), ParameterLocation::Query, param.name()),
                    ident,
                );
            }
        }
    }

    for schema in cooked.schemas() {
        if let Some(domain) = MemberIdentDomain::from_schema_type(schema) {
            let map = domain.into_idents(cooked.arena(), &idents);
            idents.extend(map);
        }
    }

    // Inline type names depend on uniquified path segments. Build each inline
    // type after its parent, then name its members for child path segments.
    {
        let inlines = cooked
            .schemas()
            .flat_map(|schema| schema.inlines())
            .chain(cooked.operations().flat_map(|op| op.inlines()))
            .filter(|ty| {
                // Optional types are invisible for naming.
                !matches!(ty, InlineTypeView::Container(_, ContainerView::Optional(_)))
            });

        let mut scopes = FxHashMap::default();
        for inline in inlines {
            let path = inline.path();
            let domain = match path.root() {
                InlineTypePathRoot::Schema(id) => InlineTypeIdentDomain::Schema(id),
                InlineTypePathRoot::Operation { resource, .. } => InlineTypeIdentDomain::Resource(
                    resource
                        .map(|name| ResourceGroup::Named(idents[&IdentMapKey::Resource(name)]))
                        .unwrap_or_default(),
                ),
            };
            let name = inline_type_candidate_name(&idents, &path);
            let scope = scopes
                .entry(domain)
                .or_insert_with(|| UniqueIdents::new(cooked.arena()));
            idents.insert(IdentMapKey::Type(inline.id()), scope.claim(&name));
            if let Some(domain) = MemberIdentDomain::from_inline_type(inline) {
                let map = domain.into_idents(cooked.arena(), &idents);
                idents.extend(map);
            }
        }
    }
    idents
}

type IdentMap<'a> = FxHashMap<IdentMapKey<'a>, UniqueIdent<'a>>;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum IdentMapKey<'a> {
    Type(TypeId),
    Operation(&'a OperationId),
    Parameter(&'a OperationId, ParameterLocation, &'a str),
    Resource(&'a str),
    StructField(TypeId, StructFieldName<'a>),
    EnumVariant(TypeId, &'a str),
    TaggedVariant(TypeId, &'a str),
    UntaggedVariant(TypeId, NonZeroUsize),
}

/// A uniqueness domain for inline type identifiers.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum InlineTypeIdentDomain<'a> {
    Schema(TypeId),
    Resource(ResourceGroup<'a>),
}

enum MemberIdentDomain<'graph, 'a> {
    Struct(TypeId, StructView<'graph, 'a>),
    Enum(TypeId, EnumView<'graph, 'a>),
    Tagged(TypeId, TaggedView<'graph, 'a>),
    Untagged(TypeId, UntaggedView<'graph, 'a>),
}

impl<'graph, 'a> MemberIdentDomain<'graph, 'a> {
    fn from_schema_type(schema: SchemaTypeView<'graph, 'a>) -> Option<Self> {
        let id = schema.id();
        Some(match schema {
            SchemaTypeView::Struct(_, view) => Self::Struct(id, view),
            SchemaTypeView::Enum(_, view) => Self::Enum(id, view),
            SchemaTypeView::Tagged(_, view) => Self::Tagged(id, view),
            SchemaTypeView::Untagged(_, view) => Self::Untagged(id, view),
            _ => return None,
        })
    }

    fn from_inline_type(inline: InlineTypeView<'graph, 'a>) -> Option<Self> {
        let id = inline.id();
        Some(match inline {
            InlineTypeView::Struct(_, view) => Self::Struct(id, view),
            InlineTypeView::Enum(_, view) => Self::Enum(id, view),
            InlineTypeView::Tagged(_, view) => Self::Tagged(id, view),
            InlineTypeView::Untagged(_, view) => Self::Untagged(id, view),
            _ => return None,
        })
    }

    fn into_idents(self, arena: &'a Arena, idents: &IdentMap<'a>) -> IdentMap<'a> {
        let mut map = IdentMap::default();
        match self {
            Self::Struct(id, view) => {
                // Own, inherited, and synthesized struct fields.
                let mut scope = UniqueIdents::new(arena);
                for field in view.fields() {
                    let name = field.name();
                    let ident = match name {
                        StructFieldName::Name(name) => scope.claim(name),
                        StructFieldName::Ordinal(ordinal) => {
                            let ident = idents[&IdentMapKey::Type(id)];
                            scope.claim(&format!(
                                "{}_{ordinal}",
                                CodegenIdentUsage::Type(ident).display()
                            ))
                        }
                        StructFieldName::AdditionalProperties => {
                            scope.claim("additional_properties")
                        }
                    };
                    map.insert(IdentMapKey::StructField(id, name), ident);
                }
            }
            Self::Enum(id, view) => {
                let mut scope = UniqueIdents::with_reserved(
                    arena,
                    &[&format!(
                        "Other{}",
                        CodegenIdentUsage::Type(idents[&IdentMapKey::Type(id)]).display()
                    )],
                );
                for &variant in view.variants() {
                    if let EnumVariant::String(name) = variant {
                        map.insert(IdentMapKey::EnumVariant(id, name), scope.claim(name));
                    }
                }
            }
            Self::Tagged(id, view) => {
                // Tagged variant names and common fields form different scopes:
                // variant names must be unique within the generated enum;
                // common fields are for naming inline types.
                let mut scope = UniqueIdents::new(arena);
                for variant in view.variants() {
                    let name = variant.name();
                    let ident = scope.claim(name);
                    map.insert(IdentMapKey::TaggedVariant(id, name), ident);
                }
                let mut scope = UniqueIdents::new(arena);
                for field in view.fields() {
                    let name = field.name();
                    let ident = match name {
                        StructFieldName::Name(name) => scope.claim(name),
                        StructFieldName::Ordinal(ordinal) => {
                            let ident = idents[&IdentMapKey::Type(id)];
                            scope.claim(&format!(
                                "{}_{ordinal}",
                                CodegenIdentUsage::Type(ident).display()
                            ))
                        }
                        StructFieldName::AdditionalProperties => {
                            scope.claim("additional_properties")
                        }
                    };
                    map.insert(IdentMapKey::StructField(id, name), ident);
                }
            }
            Self::Untagged(id, view) => {
                let mut scope = UniqueIdents::new(arena);
                for variant in view.variants() {
                    use {ContainerView::*, InlineTypeView::*, TypeView::*};
                    let ordinal = variant.ordinal();
                    let ident = match variant.ty() {
                        Some(Schema(schema)) => {
                            let ident = idents[&IdentMapKey::Type(schema.id())];
                            scope.adopt(ident)
                        }
                        Some(Inline(Primitive(_, primitive))) => {
                            scope.claim(match primitive.ty() {
                                PrimitiveType::String => "String",
                                PrimitiveType::I8 => "I8",
                                PrimitiveType::U8 => "U8",
                                PrimitiveType::I16 => "I16",
                                PrimitiveType::U16 => "U16",
                                PrimitiveType::I32 => "I32",
                                PrimitiveType::U32 => "U32",
                                PrimitiveType::I64 => "I64",
                                PrimitiveType::U64 => "U64",
                                PrimitiveType::F32 => "F32",
                                PrimitiveType::F64 => "F64",
                                PrimitiveType::Bool => "Bool",
                                PrimitiveType::DateTime => "DateTime",
                                PrimitiveType::UnixTime => "UnixTime",
                                PrimitiveType::Date => "Date",
                                PrimitiveType::Url => "Url",
                                PrimitiveType::Uuid => "Uuid",
                                PrimitiveType::Bytes => "Bytes",
                                PrimitiveType::Binary => "Binary",
                            })
                        }
                        Some(Inline(Container(_, Array(_)))) => scope.claim("Array"),
                        Some(Inline(Container(_, Map(_)))) => scope.claim("Map"),
                        Some(Inline(..)) => {
                            let ident = idents[&IdentMapKey::Type(id)];
                            scope.claim(&format!(
                                "{}_{ordinal}",
                                CodegenIdentUsage::Type(ident).display()
                            ))
                        }
                        None => scope.claim("None"),
                    };
                    map.insert(IdentMapKey::UntaggedVariant(id, ordinal), ident);
                }
                // Common fields inherited by all untagged variants.
                let mut scope = UniqueIdents::new(arena);
                for field in view.fields() {
                    let name = field.name();
                    let ident = match name {
                        StructFieldName::Name(name) => scope.claim(name),
                        StructFieldName::Ordinal(ordinal) => {
                            let ident = idents[&IdentMapKey::Type(id)];
                            scope.claim(&format!(
                                "{}_{ordinal}",
                                CodegenIdentUsage::Type(ident).display()
                            ))
                        }
                        StructFieldName::AdditionalProperties => {
                            scope.claim("additional_properties")
                        }
                    };
                    map.insert(IdentMapKey::StructField(id, name), ident);
                }
            }
        }
        map
    }
}

fn inline_type_candidate_name<'a>(
    idents: &IdentMap<'a>,
    path: &InlineTypePathView<'_, 'a>,
) -> String {
    let mut name = String::new();

    for segment in path.segments() {
        match segment {
            InlineTypePathSegment::Field(parent, field) => {
                let ident = idents[&IdentMapKey::StructField(parent, field)];
                write!(name, "{}", CodegenIdentUsage::Type(ident).display()).unwrap();
            }
            InlineTypePathSegment::TaggedVariant(parent, variant) => {
                let ident = idents[&IdentMapKey::TaggedVariant(parent, variant)];
                write!(name, "{}", CodegenIdentUsage::Variant(ident).display()).unwrap();
            }
            InlineTypePathSegment::UntaggedVariant(parent, ordinal) => {
                let ident = idents[&IdentMapKey::UntaggedVariant(parent, ordinal)];
                write!(name, "{}", CodegenIdentUsage::Variant(ident).display()).unwrap();
            }
            InlineTypePathSegment::ArrayItem => name.push_str("Item"),
            InlineTypePathSegment::MapValue => name.push_str("Value"),
            InlineTypePathSegment::Optional => {
                // Optional types are invisible for naming.
            }
            InlineTypePathSegment::Inherits(parent, ordinal) => {
                let ident = idents[&IdentMapKey::Type(parent)];
                write!(
                    name,
                    "{}_{ordinal}",
                    CodegenIdentUsage::Type(ident).display()
                )
                .unwrap();
            }
        }
    }

    match path.root() {
        InlineTypePathRoot::Schema(id) if name.is_empty() => {
            let ident = idents[&IdentMapKey::Type(id)];
            CodegenIdentUsage::Type(ident).display().to_string()
        }
        InlineTypePathRoot::Schema(..) => name,
        InlineTypePathRoot::Operation { id, usage, .. } => {
            let mut full = String::new();

            let ident = idents[&IdentMapKey::Operation(id)];
            write!(full, "{}", CodegenIdentUsage::Type(ident).display()).unwrap();
            match usage {
                OperationUsage::Path(param) => {
                    let ident = idents[&IdentMapKey::Parameter(id, ParameterLocation::Path, param)];
                    write!(full, "Path{}", CodegenIdentUsage::Type(ident).display()).unwrap();
                }
                OperationUsage::Query(param) => {
                    let ident =
                        idents[&IdentMapKey::Parameter(id, ParameterLocation::Query, param)];
                    write!(full, "Query{}", CodegenIdentUsage::Type(ident).display()).unwrap();
                }
                OperationUsage::Request => full.push_str("Request"),
                OperationUsage::Response => full.push_str("Response"),
            }
            full.push_str(&name);

            full
        }
    }
}