Unverified Commit 89a62cb1 authored by Nugine's avatar Nugine
Browse files

codegen: dto: fluent style builder

resolves #62
parent e8bba54f
Loading
Loading
Loading
Loading
+32 −2
Original line number Diff line number Diff line
@@ -612,14 +612,44 @@ fn codegen_struct_builder(ty: &rust::Struct, rust_types: &RustTypes) {
            Cow::Borrowed(&field.type_)
        };

        g!("pub fn set_{field_name}(&mut self, field: {struct_field_type}) {{");
        let needs_wrap = !(field.option_type || field.default_value.is_some() || is_list_or_map(&field.type_, rust_types));

        if field.option_type || field.default_value.is_some() || is_list_or_map(&field.type_, rust_types) {
        g!("pub fn set_{field_name}(&mut self, field: {struct_field_type}) -> &mut Self {{");

        if needs_wrap {
            g!("    self.{field_name} = Some(field);");
        } else {
            g!("    self.{field_name} = field;");
        }

        g!("self");

        g!("}}");
        g!();
    }

    for field in &ty.fields {
        let field_name = field.name.as_str();

        let struct_field_type = if field.option_type {
            Cow::Owned(format!("Option<{}>", field.type_))
        } else {
            Cow::Borrowed(&field.type_)
        };

        let needs_wrap = !(field.option_type || field.default_value.is_some() || is_list_or_map(&field.type_, rust_types));

        g!("#[must_use]");
        g!("pub fn {field_name}(mut self, field: {struct_field_type}) -> Self {{");

        if needs_wrap {
            g!("    self.{field_name} = Some(field);");
        } else {
            g!("    self.{field_name} = field;");
        }

        g!("self");

        g!("}}");
        g!();
    }
+4817 −680

File changed.

Preview size limit exceeded, changes collapsed.