use crate::config::{Config, FieldConfig, Layout, TypeConfig};
use crate::diagnostic::{Diagnostic, DiagnosticKind, SchemaId, Severity};
use crate::front::{Constraints, FloatWidth, IntWidth};
use crate::generate::GenerateError;
use crate::ir::{Additional, Ir, TypeDef, TypeKind, TypeRef};
use crate::rust::attrs::{cfg_gates, check_container_attrs, check_field_attrs, serde_aliases};
use crate::rust::naming::{Allocator, legal, pascal, snake};
use serde::Serialize;
use std::collections::BTreeMap;
#[derive(Debug, Clone, Serialize, Default)]
pub(crate) struct SupportView {
pub double_option: bool,
pub reject_null: bool,
pub require_present: bool,
pub int64: bool,
pub int32: bool,
pub decimal: bool,
pub exact_numbers: bool,
pub raw_holder: bool,
pub object_only: bool,
pub string_only: bool,
pub raw_json: bool,
pub catch_all: bool,
pub validation: bool,
}
#[derive(Debug, Clone, Serialize)]
pub(crate) struct FieldView {
pub name: String,
pub raw_name: String,
pub ty: String,
pub doc: Vec<String>,
pub attrs: Vec<String>,
#[serde(skip)]
pub cfg: Vec<String>,
}
#[derive(Debug, Clone, Serialize)]
pub(crate) struct CatchAllView {
pub field: String,
pub ty: String,
pub declared: Vec<DeclaredView>,
pub allowed: String,
}
#[derive(Debug, Clone, Serialize)]
pub(crate) struct DeclaredView {
pub cfg: Vec<String>,
pub names: String,
}
#[derive(Debug, Clone, Serialize)]
pub(crate) struct VariantView {
pub name: String,
pub value: String,
pub ty: String,
pub doc: Vec<String>,
pub attrs: Vec<String>,
pub cfg: Vec<String>,
pub literal: String,
pub synthetic: bool,
pub pattern: String,
}
#[derive(Debug, Clone, Serialize)]
pub(crate) struct BranchView {
pub accessor: String,
pub constructor: String,
pub ty: String,
pub doc: Vec<String>,
}
#[derive(Debug, Clone, Serialize)]
pub(crate) struct TypeView {
pub kind: &'static str,
pub name: String,
pub raw_name: String,
pub doc: Vec<String>,
pub derives: Vec<String>,
pub attrs: Vec<String>,
pub fields: Vec<FieldView>,
pub variants: Vec<VariantView>,
pub branches: Vec<BranchView>,
pub inner: String,
pub tag: String,
pub fallback: Option<usize>,
pub parent_variant: Option<usize>,
pub tag_literal: String,
pub tag_doc: String,
pub exclusive: bool,
pub validate: Option<Vec<String>>,
pub validate_null: Vec<String>,
pub object_only: bool,
pub string_only: bool,
pub catch_all: Option<CatchAllView>,
#[serde(skip)]
pub pattern_tys: Vec<Option<String>>,
#[serde(skip)]
pub others_ty: Option<String>,
pub cfg: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub file: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub(crate) struct FileView {
pub template_api: u32,
pub source: String,
pub header: String,
pub imports: Vec<String>,
pub extra_imports: Vec<String>,
pub support: SupportView,
pub types: Vec<TypeView>,
#[serde(skip_serializing_if = "Option::is_none")]
pub module: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub struct RustDependency {
pub name: String,
pub features: Vec<String>,
}
impl RustDependency {
pub fn new(name: impl Into<String>, features: Vec<String>) -> Self {
RustDependency {
name: name.into(),
features,
}
}
}
pub(crate) fn constraint_notes(c: &Constraints) -> Vec<String> {
let mut notes = Vec::new();
let mut push = |keyword: &str, value: String| notes.push(format!("`{keyword}`: {value}."));
if let Some(v) = c.min_length {
push("minLength", v.to_string());
}
if let Some(v) = c.max_length {
push("maxLength", v.to_string());
}
if let Some(v) = &c.pattern {
push("pattern", code_span(&v.escape_debug().to_string()));
}
if let Some(v) = &c.minimum {
push("minimum", v.to_string());
}
if let Some(v) = &c.exclusive_minimum {
push("exclusiveMinimum", v.to_string());
}
if let Some(v) = &c.maximum {
push("maximum", v.to_string());
}
if let Some(v) = &c.exclusive_maximum {
push("exclusiveMaximum", v.to_string());
}
if let Some(v) = &c.multiple_of {
push("multipleOf", v.to_string());
}
if let Some(v) = c.min_items {
push("minItems", v.to_string());
}
if let Some(v) = c.max_items {
push("maxItems", v.to_string());
}
if c.unique_items {
push("uniqueItems", "true".into());
}
if let Some(v) = c.min_properties {
push("minProperties", v.to_string());
}
if let Some(v) = c.max_properties {
push("maxProperties", v.to_string());
}
notes
}
pub(crate) struct Built {
pub file: FileView,
pub dependencies: Vec<RustDependency>,
pub diagnostics: Vec<Diagnostic>,
}
struct Builder<'a> {
ir: &'a Ir,
config: &'a Config,
names: BTreeMap<SchemaId, String>,
support: SupportView,
externs: BTreeMap<String, String>,
diagnostics: Vec<Diagnostic>,
}
pub(crate) fn build(ir: &Ir, config: &Config, source: &str) -> Result<Built, GenerateError> {
let mut builder = Builder {
ir,
config,
names: BTreeMap::new(),
support: SupportView {
exact_numbers: config.exact_numbers,
..SupportView::default()
},
externs: BTreeMap::new(),
diagnostics: Vec::new(),
};
select_check(ir, config)?;
builder.allocate_type_names();
super::validation::take_uses_regex();
let mut types = Vec::new();
for id in &ir.order {
if ir.suppressed.contains(id) {
continue;
}
let def = &ir.types[id];
if let Some(view) = builder.type_view(def)? {
types.push(view);
}
}
let uses_regex = super::validation::take_uses_regex();
let module = (config.layout == Layout::FilePerType).then(|| {
assign_files(&mut types);
config.rust.module.clone()
});
let mut dependencies = vec![
RustDependency {
name: "serde".into(),
features: vec!["derive".into()],
},
RustDependency {
name: "serde_json".into(),
features: if builder.config.validation {
vec!["raw_value".into(), "float_roundtrip".into()]
} else {
vec!["raw_value".into()]
},
},
];
if uses_regex {
dependencies.push(RustDependency {
name: "regex".into(),
features: Vec::new(),
});
}
for import in builder.externs.values() {
let dependency = RustDependency {
name: import.clone(),
features: Vec::new(),
};
if !dependencies.contains(&dependency) {
dependencies.push(dependency);
}
}
dependencies.sort();
let imports = vec!["use serde::{Deserialize, Serialize};".to_owned()];
Ok(Built {
file: FileView {
template_api: crate::templates::TEMPLATE_API,
source: source.to_owned(),
header: config.header.clone().unwrap_or_default(),
imports,
extra_imports: config.extra_imports.clone(),
support: builder.support,
types,
module,
},
dependencies,
diagnostics: builder.diagnostics,
})
}
fn assign_files(types: &mut [TypeView]) {
let mut taken = std::collections::BTreeSet::new();
for t in types {
let base = crate::write::capped_stem(crate::config::rust_module_name(&snake(
std::slice::from_ref(&t.name),
)));
let file = (1u32..)
.map(|i| {
if i == 1 {
base.clone()
} else {
format!("{base}{i}")
}
})
.find(|candidate| !taken.contains(candidate) && crate::write::windows_safe(candidate))
.expect("a free file name");
taken.insert(file.clone());
t.file = Some(file);
}
}
pub(crate) fn select_check(ir: &Ir, config: &Config) -> Result<(), GenerateError> {
for (name, type_config) in &config.types {
let Some(id) = ir.components.get(name) else {
return Err(GenerateError::UnknownSelector {
selector: format!("types.{name}"),
candidates: ir.components.keys().cloned().collect(),
});
};
if type_config.fields.is_empty() {
continue;
}
let def = &ir.types[id];
let fields: Vec<String> = match &def.kind {
TypeKind::Struct(s) => s.fields.iter().map(|f| f.raw_name.clone()).collect(),
_ => Vec::new(),
};
for field in type_config.fields.keys() {
if !fields.contains(field) {
return Err(GenerateError::UnknownSelector {
selector: format!("types.{name}.fields.{field}"),
candidates: fields.clone(),
});
}
}
}
Ok(())
}
impl Builder<'_> {
fn allocate_type_names(&mut self) {
let mut allocator = Allocator::default();
if self.config.exact_numbers {
allocator.reserve("Decimal");
}
for reserved in [
"Int64",
"Int32",
"Nullable",
"String",
"Option",
"Some",
"None",
"Vec",
"Box",
"Result",
"Ok",
"Err",
"Self",
"Serialize",
"Deserialize",
"Default",
"Clone",
"Debug",
"PartialEq",
"Eq",
"Hash",
"Copy",
"From",
"Into",
"double_option",
"reject_null",
"require_present",
"integer_token",
] {
allocator.reserve(reserved);
}
if self
.ir
.types
.values()
.any(|def| matches!(&def.kind, TypeKind::Struct(s) if !s.patterns.is_empty()))
{
allocator.reserve("RawJson");
}
if self.config.validation {
for reserved in [
"Validate",
"ValidationReport",
"Finding",
"FindingKind",
"child_pointer",
"wide_number",
] {
allocator.reserve(reserved);
}
}
let ir = self.ir;
let order = ir
.order
.iter()
.filter(|id| ir.types[*id].component.is_some());
let rest = ir
.order
.iter()
.filter(|id| ir.types[*id].component.is_none());
for id in order.chain(rest) {
let def = &self.ir.types[id];
if matches!(def.kind, TypeKind::Extern) {
continue;
}
let base = match def
.component
.as_ref()
.and_then(|c| self.config.types.get(c))
.and_then(|t| t.rename.clone())
{
Some(rename) => rename,
None => pascal(&def.suggested_name),
};
let name = allocator.allocate(legal(base));
self.names.insert(id.clone(), name);
}
}
fn type_gates(&self, id: &SchemaId, at: &str) -> Result<Vec<String>, GenerateError> {
let Some(def) = self.ir.types.get(id) else {
return Ok(Vec::new());
};
let mut attrs: Vec<String> = self
.type_config(def)
.map(|t| t.attrs.clone())
.unwrap_or_default();
if self.config.allow_codegen_extensions {
let described: Vec<String> = match def.extensions.get("x-rust-attrs") {
Some(serde_json::Value::String(a)) => vec![a.clone()],
Some(serde_json::Value::Array(items)) => items
.iter()
.filter_map(|v| v.as_str().map(str::to_owned))
.collect(),
_ => Vec::new(),
};
attrs.extend(
described
.into_iter()
.filter(|a| check_container_attrs(std::slice::from_ref(a), "").is_ok()),
);
}
cfg_gates(&attrs, at)
}
fn type_config(&self, def: &TypeDef) -> Option<&TypeConfig> {
def.component
.as_ref()
.and_then(|c| self.config.types.get(c))
}
fn reshaped(&self, id: &SchemaId) -> bool {
let def = &self.ir.types[id];
let TypeKind::Struct(s) = &def.kind else {
return false;
};
s.fields.iter().any(|field| {
let mut attrs: Vec<String> = self
.field_config(def, &field.raw_name)
.map(|f| f.attrs.clone())
.unwrap_or_default();
if self.config.allow_codegen_extensions {
let described: Vec<String> = match field.extensions.get("x-rust-attrs") {
Some(serde_json::Value::String(a)) => vec![a.clone()],
Some(serde_json::Value::Array(items)) => items
.iter()
.filter_map(|v| v.as_str().map(str::to_owned))
.collect(),
_ => Vec::new(),
};
attrs.extend(
described
.into_iter()
.filter(|a| check_field_attrs(std::slice::from_ref(a), "").is_ok()),
);
}
attrs.iter().any(|attr| {
let one = std::slice::from_ref(attr);
!matches!(cfg_gates(one, ""), Ok(g) if g.is_empty())
|| !matches!(serde_aliases(one, ""), Ok(a) if a.is_empty())
})
})
}
fn field_config<'c>(&'c self, def: &TypeDef, raw_name: &str) -> Option<&'c FieldConfig> {
self.type_config(def).and_then(|t| t.fields.get(raw_name))
}
fn spell(&mut self, ty: &TypeRef) -> Result<String, GenerateError> {
Ok(match ty {
TypeRef::Named(id) => match &self.ir.types[id].kind {
TypeKind::Extern => {
let component = self.ir.types[id].component.clone().unwrap_or_default();
self.extern_spelling(&component)?
}
_ => self.names[id].clone(),
},
TypeRef::Boxed(inner) => format!("Box<{}>", self.spell(inner)?),
TypeRef::Nullable(inner) => format!("Option<{}>", self.spell(inner)?),
TypeRef::String => "String".into(),
TypeRef::Integer(IntWidth::Int64) => {
self.support.int64 = true;
"Int64".into()
}
TypeRef::Integer(IntWidth::Int32) => {
self.support.int32 = true;
"Int32".into()
}
TypeRef::Number(FloatWidth::Double) => "f64".into(),
TypeRef::Number(FloatWidth::Float) => "f32".into(),
TypeRef::Decimal => {
self.support.decimal = true;
"Decimal".into()
}
TypeRef::Boolean => "bool".into(),
TypeRef::Null => "()".into(),
TypeRef::Any => "serde_json::Value".into(),
TypeRef::Array(inner) => format!("Vec<{}>", self.spell(inner)?),
TypeRef::Map(inner) => {
format!("std::collections::BTreeMap<String, {}>", self.spell(inner)?)
}
TypeRef::Extern(key) => self.extern_spelling(key)?,
})
}
fn extern_spelling(&mut self, key: &str) -> Result<String, GenerateError> {
let spelling = self
.config
.substitutions
.get(key)
.and_then(|s| s.rust.as_ref())
.ok_or_else(|| {
GenerateError::Config(format!("substitution `{key}` has no `rust` spelling"))
})?;
if let Some(import) = &spelling.import {
self.externs.insert(key.to_owned(), import.clone());
}
Ok(spelling.name.clone())
}
fn doc(
description: Option<&str>,
default: Option<&serde_json::Value>,
read_only: bool,
write_only: bool,
constraints: &Constraints,
) -> Vec<String> {
let mut lines: Vec<String> = description
.map(|d| d.lines().map(|l| l.trim_end().to_owned()).collect())
.unwrap_or_default();
let mut notes = Vec::new();
if let Some(default) = default {
notes.push(format!("Default: `{default}`."));
}
if read_only {
notes.push("Read only.".to_owned());
}
if write_only {
notes.push("Write only.".to_owned());
}
notes.extend(constraint_notes(constraints));
if !notes.is_empty() {
if !lines.is_empty() {
lines.push(String::new());
}
lines.extend(notes);
}
lines
.into_iter()
.flat_map(|l| l.split(['\n', '\r']).map(str::to_owned).collect::<Vec<_>>())
.collect()
}
fn codegen_attrs(
&mut self,
extensions: &BTreeMap<String, serde_json::Value>,
at: &SchemaId,
field: bool,
) -> Vec<String> {
let Some(value) = extensions.get("x-rust-attrs") else {
return Vec::new();
};
if !self.config.allow_codegen_extensions {
self.diagnostics.push(Diagnostic {
severity: Severity::Warning,
kind: DiagnosticKind::IgnoredExtension { keyword: "x-rust-attrs".into() },
schema_id: at.clone(),
pointer: crate::diagnostic::source_of(&format!("{}/x-rust-attrs", at.pointer)),
message: "`x-rust-attrs` is code from the description and is not applied unless `allow_codegen_extensions` is set".into(),
});
return Vec::new();
}
let attrs: Vec<String> = match value {
serde_json::Value::String(s) => vec![s.clone()],
serde_json::Value::Array(items) => items
.iter()
.filter_map(|v| v.as_str().map(str::to_owned))
.collect(),
_ => Vec::new(),
};
let mut kept = Vec::new();
for attr in attrs {
let one = std::slice::from_ref(&attr);
let checked = if field {
check_field_attrs(one, &crate::diagnostic::source_of(&at.pointer))
} else {
check_container_attrs(one, &crate::diagnostic::source_of(&at.pointer))
};
match checked {
Ok(()) => kept.push(attr),
Err(error) => self.diagnostics.push(Diagnostic {
severity: Severity::Warning,
kind: DiagnosticKind::IgnoredExtension {
keyword: "x-rust-attrs".into(),
},
schema_id: at.clone(),
pointer: crate::diagnostic::source_of(&format!("{}/x-rust-attrs", at.pointer)),
message: format!("`x-rust-attrs` entry not applied: {error}"),
}),
}
}
kept
}
fn type_view(&mut self, def: &TypeDef) -> Result<Option<TypeView>, GenerateError> {
let name = match self.names.get(&def.id) {
Some(name) => name.clone(),
None => return Ok(None),
};
let raw_name = def
.component
.clone()
.unwrap_or_else(|| def.id.pointer.clone());
let mut doc = Vec::new();
if let Some(title) = def
.title
.as_deref()
.filter(|t| !t.trim().is_empty() && def.component.as_deref() != Some(t))
{
doc.extend(title.trim().lines().map(|l| l.trim_end().to_owned()));
doc.push(String::new());
}
doc.extend(Self::doc(
def.description.as_deref(),
None,
false,
false,
&def.constraints,
));
if def.nullable {
doc.push(
"`null` is a valid instance; a position holding this type is an `Option`."
.to_owned(),
);
}
while doc.last().is_some_and(String::is_empty) {
doc.pop();
}
let mut attrs = Vec::new();
if def.deprecated {
attrs.push("#[deprecated]".to_owned());
}
let user_attrs: Vec<String> = self
.type_config(def)
.map(|t| t.attrs.clone())
.unwrap_or_default();
check_container_attrs(&user_attrs, &raw_name)?;
attrs.extend(user_attrs);
attrs.extend(self.codegen_attrs(&def.extensions, &def.id, false));
if let TypeKind::UnionTagged(u) = &def.kind
&& let Some(parent) = &u.parent
{
for gate in self.type_gates(parent, &raw_name)? {
if !attrs.contains(&gate) {
attrs.push(gate);
}
}
}
let cfg = cfg_gates(&attrs, &raw_name)?;
let mut derives: Vec<String> = ["Debug", "Clone", "PartialEq"]
.iter()
.map(|s| (*s).to_owned())
.collect();
derives.extend(self.config.rust.derives.iter().cloned());
let mut view = TypeView {
kind: "struct",
name,
raw_name,
doc,
derives,
attrs,
fields: Vec::new(),
variants: Vec::new(),
branches: Vec::new(),
inner: String::new(),
tag: String::new(),
fallback: None,
parent_variant: None,
tag_literal: String::new(),
tag_doc: String::new(),
exclusive: false,
validate: None,
validate_null: Vec::new(),
object_only: false,
string_only: false,
catch_all: None,
pattern_tys: Vec::new(),
others_ty: None,
cfg,
file: None,
};
let result = self.fill_kind(def, &mut view)?;
if !result {
return Ok(None);
}
if self.config.validation {
self.support.validation = true;
let opted_out = self.type_config(def).and_then(|t| t.validation) == Some(false);
let emitted = |id: &SchemaId| self.names.contains_key(id);
let generated = |ty: &TypeRef| crate::checks::validates(self.ir, &emitted, ty);
let reshaped = |id: &SchemaId| self.reshaped(id);
view.validate =
super::validation::body(self.ir, def, &view, &generated, &reshaped, opted_out);
if view.validate.is_some() && !opted_out {
view.validate_null = super::validation::null_body(def);
if let TypeKind::Alias(inner) = &def.kind
&& generated(inner)
{
view.validate_null = vec![format!(
"<{} as Validate>::validate_null(pointer, report);",
view.inner
)];
}
}
if opted_out && view.validate.is_none() {
return Err(crate::go::view::opt_out_on_alias(def));
}
}
let mut seen = std::collections::BTreeSet::new();
view.derives.retain(|d| seen.insert(d.clone()));
Ok(Some(view))
}
fn fill_kind(&mut self, def: &TypeDef, view: &mut TypeView) -> Result<bool, GenerateError> {
match &def.kind {
TypeKind::Extern => return Ok(false),
TypeKind::Struct(s) => {
view.kind = "struct";
view.derives
.extend(["Serialize".to_owned(), "Deserialize".to_owned()]);
let mut allocator = Allocator::default();
let mut declared = Vec::new();
for field in &s.fields {
let rename = self
.field_config(def, &field.raw_name)
.and_then(|f| f.rename.clone());
let base =
rename.unwrap_or_else(|| snake(std::slice::from_ref(&field.raw_name)));
let name = allocator.allocate(legal(base));
let inner = self.spell(&field.ty)?;
let (ty, mut attrs) = match (field.required, field.nullable) {
(true, false) => (inner, vec![]),
(true, true) => {
self.support.require_present = true;
(
format!("Option<{inner}>"),
vec![
"#[serde(deserialize_with = \"require_present::deserialize\")]"
.to_owned(),
],
)
}
(false, false) => {
self.support.reject_null = true;
(
format!("Option<{inner}>"),
vec!["#[serde(default, skip_serializing_if = \"Option::is_none\", deserialize_with = \"reject_null::deserialize\")]".to_owned()],
)
}
(false, true) => {
self.support.double_option = true;
(
format!("Option<Option<{inner}>>"),
vec!["#[serde(default, skip_serializing_if = \"Option::is_none\", deserialize_with = \"double_option::deserialize\")]".to_owned()],
)
}
};
if name.trim_start_matches("r#") != field.raw_name {
attrs.insert(0, format!("#[serde(rename = {:?})]", field.raw_name));
}
if field.deprecated {
attrs.push("#[deprecated]".to_owned());
}
let user_attrs: Vec<String> = self
.field_config(def, &field.raw_name)
.map(|f| f.attrs.clone())
.unwrap_or_default();
check_field_attrs(
&user_attrs,
&format!("{}.{}", view.raw_name, field.raw_name),
)?;
attrs.extend(user_attrs);
let mut at = def.id.clone();
at.pointer = field.pointer.clone();
attrs.extend(self.codegen_attrs(&field.extensions, &at, true));
let cfg = cfg_gates(&attrs, &format!("{}.{}", view.raw_name, field.raw_name))?;
if matches!(s.additional, Additional::CatchAll(_)) {
let mut names = vec![field.raw_name.clone()];
names.extend(serde_aliases(
&attrs,
&format!("{}.{}", view.raw_name, field.raw_name),
)?);
declared.push(DeclaredView {
cfg: cfg.clone(),
names: names
.iter()
.map(|n| format!("{n:?}"))
.collect::<Vec<_>>()
.join(" | "),
});
}
view.fields.push(FieldView {
cfg,
name,
raw_name: field.raw_name.clone(),
ty,
doc: Self::doc(
field.description.as_deref(),
field.default.as_ref(),
field.read_only,
field.write_only,
&field.constraints,
),
attrs,
});
}
match &s.additional {
Additional::Denied => {
view.attrs.push("#[serde(deny_unknown_fields)]".to_owned());
view.attrs.push("#[serde(remote = \"Self\")]".to_owned());
view.object_only = true;
self.support.object_only = true;
}
Additional::CatchAll(ty) => {
let inner = if s.patterns.is_empty() {
self.spell(ty)?
} else {
self.support.raw_json = true;
"RawJson".to_owned()
};
for pattern in &s.patterns {
let spelled = match pattern.branch() {
Some(b) => Some(self.spell(&b.ty)?),
None => None,
};
view.pattern_tys.push(spelled);
}
if let crate::ir::Others::Schema(b) = &s.others {
view.others_ty = Some(self.spell(&b.ty)?);
}
let name = allocator.allocate("additional_properties".to_owned());
view.fields.push(FieldView {
name: name.clone(),
raw_name: "additionalProperties".to_owned(),
ty: format!("std::collections::BTreeMap<String, {inner}>"),
doc: vec!["Members not named above.".to_owned()],
attrs: vec!["#[serde(flatten, skip_deserializing)]".to_owned()],
cfg: Vec::new(),
});
view.attrs.push("#[serde(remote = \"Self\")]".to_owned());
let allowed = match &s.only_patterns {
None => "|_: &str| true".to_owned(),
Some(patterns) => {
super::validation::mark_uses_regex();
let list = patterns
.iter()
.map(|p| format!("{p:?}"))
.collect::<Vec<_>>()
.join(", ");
format!(
"|name: &str| {{ static PATTERNS: std::sync::LazyLock<Option<Vec<regex::Regex>>> = std::sync::LazyLock::new(|| [{list}].iter().map(|p| regex::Regex::new(p).ok()).collect()); PATTERNS.as_ref().is_none_or(|patterns| patterns.iter().any(|re| re.is_match(name))) }}"
)
}
};
view.catch_all = Some(CatchAllView {
field: name,
ty: inner,
declared,
allowed,
});
self.support.catch_all = true;
}
}
}
TypeKind::StringEnum(values) => {
view.kind = "enum";
view.attrs.push("#[serde(remote = \"Self\")]".to_owned());
view.string_only = true;
self.support.string_only = true;
view.derives.extend([
"Eq".to_owned(),
"Hash".to_owned(),
"Serialize".to_owned(),
"Deserialize".to_owned(),
]);
let mut allocator = Allocator::default();
for value in values {
let base = if value.trim().is_empty() {
"Empty".to_owned()
} else {
pascal(std::slice::from_ref(value))
};
let name = allocator.allocate(legal(base));
view.variants.push(VariantView {
name,
value: value.clone(),
ty: String::new(),
doc: Vec::new(),
attrs: vec![format!("#[serde(rename = {value:?})]")],
cfg: Vec::new(),
literal: format!("{value:?}"),
synthetic: false,
pattern: format!("{value:?}"),
});
}
}
TypeKind::UnionTagged(u) => {
view.kind = "union_tagged";
view.derives.push("Serialize".to_owned());
view.tag = u.tag.clone();
view.tag_literal = format!("{:?}", u.tag);
view.tag_doc = code_span(&u.tag.escape_debug().to_string());
view.fallback = u.fallback;
view.parent_variant = u.parent.as_ref().and_then(|p| {
u.variants
.iter()
.position(|v| v.ty.direct_named() == Some(p))
});
let mut allocator = Allocator::default();
for (index, variant) in u.variants.iter().enumerate() {
let name = allocator.allocate(legal(pascal(&variant.suggested_name)));
let ty = self.spell(&variant.ty)?;
let cfg = match variant.ty.direct_named() {
Some(id) if u.parent.is_some() => self
.type_gates(id, &view.raw_name)?
.into_iter()
.filter(|g| !view.cfg.contains(g))
.collect(),
_ => Vec::new(),
};
view.variants.push(VariantView {
name,
value: variant.value.clone(),
ty,
doc: vec![if !variant.synthetic {
std::iter::once(&variant.value)
.chain(&variant.aliases)
.map(|tag| code_span(&format!("{}: {tag:?}", u.tag.escape_debug())))
.collect::<Vec<_>>()
.join(" or ")
} else if u.fallback == Some(index) {
format!("Read when {} names no other variant.", view.tag_doc)
} else {
"No tag selects this variant: it is read only when the fallback is not available.".to_owned()
}],
attrs: cfg.clone(),
cfg,
literal: format!("{:?}", variant.value),
synthetic: variant.synthetic,
pattern: std::iter::once(&variant.value)
.chain(&variant.aliases)
.map(|tag| format!("{tag:?}"))
.collect::<Vec<_>>()
.join(" | "),
});
}
}
TypeKind::UnionOpen(u) => {
view.kind = "union_open";
view.derives.retain(|d| d != "PartialEq");
view.derives
.extend(["Serialize".to_owned(), "Deserialize".to_owned()]);
view.exclusive = u.exclusive;
self.support.raw_holder = true;
view.branches = self.branches(&u.branches)?;
}
TypeKind::ComposedOpen(branches) => {
view.kind = "composed_open";
view.derives.retain(|d| d != "PartialEq");
view.derives
.extend(["Serialize".to_owned(), "Deserialize".to_owned()]);
self.support.raw_holder = true;
view.branches = self.branches(branches)?;
}
TypeKind::Newtype(inner) => {
view.kind = "newtype";
view.derives
.extend(["Serialize".to_owned(), "Deserialize".to_owned()]);
view.attrs.push("#[serde(transparent)]".to_owned());
view.inner = self.spell(inner)?;
}
TypeKind::Alias(inner) => {
view.inner = self.spell(inner)?;
if view.attrs.is_empty() {
view.kind = "alias";
view.derives.clear();
} else {
view.kind = "newtype";
view.derives
.extend(["Serialize".to_owned(), "Deserialize".to_owned()]);
view.attrs.push("#[serde(transparent)]".to_owned());
}
}
}
Ok(true)
}
fn branches(
&mut self,
branches: &[crate::ir::Branch],
) -> Result<Vec<BranchView>, GenerateError> {
let mut allocator = Allocator::default();
let mut out = Vec::new();
for branch in branches {
let base = snake(&branch.suggested_name);
let name = allocator.allocate(base);
let ty = self.spell(&branch.ty)?;
out.push(BranchView {
accessor: format!("as_{name}"),
constructor: format!("from_{name}"),
doc: vec![format!("The value read as `{ty}`, when it is one.")],
ty,
});
}
Ok(out)
}
}
fn code_span(text: &str) -> String {
let longest = text.split(|c| c != '`').map(str::len).max().unwrap_or(0);
let fence = "`".repeat(longest + 1);
let pad = if text.starts_with('`') || text.ends_with('`') {
" "
} else {
""
};
format!("{fence}{pad}{text}{pad}{fence}")
}