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
use ploidy_core::ir::{
ContainerView, EnumVariant, EnumView, ParameterView, QueryParameter, StructFieldView,
TaggedFieldView, TypeView, UntaggedFieldView,
};
/// Rust-specific extensions to [`EnumView`].
pub(crate) trait EnumViewExt {
/// Returns `true` if all variants of this enum can be represented as
/// unit variants in Rust. Enums with unrepresentable variants become
/// Rust strings instead.
fn representable(&self) -> bool;
}
impl EnumViewExt for EnumView<'_> {
fn representable(&self) -> bool {
self.variants().iter().all(|variant| match variant {
// Only non-empty string variants with at least one identifier
// character are representable as Rust enum variants.
EnumVariant::String(s) => s.chars().any(unicode_ident::is_xid_continue),
_ => false,
})
}
}
/// Rust-specific extensions to struct, tagged, and untagged union field views.
pub(crate) trait FieldViewExt<'a> {
/// Returns the inner type after peeling all `Optional` layers
/// (e.g., `Optional(T)`, `Optional(Optional(T))` both return `T`).
fn inner(&self) -> TypeView<'a>;
}
/// Peels all [`ContainerView::Optional`] wrapper layers from a type.
fn peel<'a>(mut ty: TypeView<'a>) -> TypeView<'a> {
while let Some(ContainerView::Optional(inner)) = ty.as_container() {
ty = inner.ty();
}
ty
}
impl<'view, 'a> FieldViewExt<'a> for StructFieldView<'view, 'a> {
fn inner(&self) -> TypeView<'a> {
peel(self.ty())
}
}
impl<'view, 'a> FieldViewExt<'a> for TaggedFieldView<'view, 'a> {
fn inner(&self) -> TypeView<'a> {
peel(self.ty())
}
}
impl<'view, 'a> FieldViewExt<'a> for UntaggedFieldView<'view, 'a> {
fn inner(&self) -> TypeView<'a> {
peel(self.ty())
}
}
/// Rust-specific extensions to [`ParameterView`].
pub(crate) trait ParameterViewExt {
/// Returns `true` if the struct field for this parameter
/// should be wrapped in an [`Option`]. This is the case when
/// the parameter isn't required and its schema type isn't
/// already [`Optional`][ContainerView::Optional].
fn optional(&self) -> bool;
}
impl<'view, 'a> ParameterViewExt for ParameterView<'view, 'a, QueryParameter> {
fn optional(&self) -> bool {
!self.required() && !matches!(self.ty().as_container(), Some(ContainerView::Optional(_)))
}
}