mirror of
https://github.com/codewhale-hq/Codewhale.git
synced 2026-11-09 01:49:49 -10:00
Tighten two helpers in the protocol generator
- Reassigning a `&Ty` from a `&Box<Ty>` inside `while let` leaned on assignment coercion; `innermost` takes the item type through a function argument instead. - `kinds` takes the field slice and a required/optional flag instead of an `impl Iterator<Item = &'a Field>`, so no explicit lifetime is needed. No change to the rendered TypeScript. Evidence: rustfmt --check clean; cargo not run here (the lead compiles). Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: CodeWhale Bot <[email protected]>
This commit is contained in:
1 parent
b162845ff4
commit
72842e70b0
1 file changed
+15
-16
@@ -231,8 +231,11 @@ fn kind(ty: &Ty, defs: &BTreeMap<String, Def>) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
fn kinds<'a>(fields: impl Iterator<Item = &'a Field>, defs: &BTreeMap<String, Def>) -> String {
|
||||
/// The kinds of the `fields` that are (or are not) `required`.
|
||||
fn kinds(fields: &[Field], required: bool, defs: &BTreeMap<String, Def>) -> String {
|
||||
let rendered: Vec<String> = fields
|
||||
.iter()
|
||||
.filter(|field| field.required == required)
|
||||
.map(|field| format!("{}: {}", field.name, kind(&field.ty, defs)))
|
||||
.collect();
|
||||
if rendered.is_empty() {
|
||||
@@ -255,6 +258,14 @@ fn ts(ty: &Ty) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// An array's item type, however deeply nested.
|
||||
fn innermost(ty: &Ty) -> &Ty {
|
||||
match ty {
|
||||
Ty::Array(item) => innermost(item),
|
||||
ty => ty,
|
||||
}
|
||||
}
|
||||
|
||||
fn member(field: &Field) -> String {
|
||||
let optional = if field.required { "" } else { "?" };
|
||||
format!("{}{optional}: {}", field.name, ts(&field.ty))
|
||||
@@ -327,11 +338,7 @@ fn render() -> String {
|
||||
&& validated.insert(name.clone())
|
||||
{
|
||||
for field in fields {
|
||||
let mut ty = &field.ty;
|
||||
while let Ty::Array(item) = ty {
|
||||
ty = item;
|
||||
}
|
||||
if let Ty::Ref(name) = ty {
|
||||
if let Ty::Ref(name) = innermost(&field.ty) {
|
||||
pending.push(name.clone());
|
||||
}
|
||||
}
|
||||
@@ -372,16 +379,8 @@ fn render() -> String {
|
||||
};
|
||||
let _ = writeln!(out, " {name}: {{");
|
||||
let _ = writeln!(out, " strict: {strict},");
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" required: {},",
|
||||
kinds(fields.iter().filter(|field| field.required), &defs)
|
||||
);
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" optional: {},",
|
||||
kinds(fields.iter().filter(|field| !field.required), &defs)
|
||||
);
|
||||
let _ = writeln!(out, " required: {},", kinds(fields, true, &defs));
|
||||
let _ = writeln!(out, " optional: {},", kinds(fields, false, &defs));
|
||||
out.push_str(" },\n");
|
||||
}
|
||||
out.push_str(
|
||||
|
||||
Reference in new issue
Block a user