Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.0.2 Enum Variant #[derive(default)] #11

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "derive-ctor"
version = "1.0.1"
version = "1.0.2"
description = "Adds `#[derive(ctor)]` which allows for the auto-generation of struct, enum, and union constructors."
keywords = ["derive", "macro", "trait", "procedural", "no_std"]
authors = ["Evan Cowin"]
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Add `derive-ctor` to your `Cargo.toml`:

```toml
[dependencies]
derive-ctor = "1.0.1"
derive-ctor = "1.0.2"
```

Annotate your struct with `#[derive(ctor)]` to automatically generate a `new` constructor:
Expand Down Expand Up @@ -105,7 +105,7 @@ respective variant and will be public. This default behaviour can be changed by
Specifying this attribute will change the **default** generated method for each variant, however, each variant
can additionally define its own configuration which overrides the one defined by the enum.

### Default variant constructor example
### Standard variant constructor example

```rust
use derive_ctor::ctor;
Expand Down Expand Up @@ -144,7 +144,9 @@ let v2 = MyEnum::new_variant2();
let v3 = MyEnum::new_variant3();
```

If a variant is derived with `#[ctor(none)]` it will **not** have a constructor generated for it.
If a variant is derived with `#[ctor(none)]` it will **not** have a constructor generated for it. Similarly,
if a variant is derived with `#[ctor(default)]` the `Default` trait will be automatically implemented to generate
that variant.

Unions express the same behaviours as enums except applicable to the fields of the union rather than the variants of
an enum.
Expand All @@ -161,7 +163,7 @@ union MyUnion {
v3: u32
}

const VAL地震: MyUnion = MyUnion::new(100);
const VAL: MyUnion = MyUnion::new(100);
let v2 = MyUnion::new_v2(123.231);
let v3 = MyUnion::new_v3(414224);
```
Expand Down
24 changes: 21 additions & 3 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn create_ctor_enum_impl(
configuration: CtorEnumConfiguration,
) -> TokenStream {
let mut methods = Vec::new();
let mut default_method = None;

for variant in variants {
let variant_code = match &variant.fields {
Expand Down Expand Up @@ -174,22 +175,39 @@ fn create_ctor_enum_impl(
} else {
quote! { Self::#variant_name }
};

methods.push(quote! {
let method_token_stream = quote! {
#visibility #const_tkn fn #name(#(#parameter_fields),*) -> Self {
#(#generated_fields)*
#enum_generation
}
})
};

if def.attrs.contains(&CtorAttribute::Default) {
default_method = Some(method_token_stream);
} else {
methods.push(method_token_stream);
}
}
}

let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let default_impl = if let Some(def_method) = default_method {
quote! {
impl #impl_generics Default for # ident # ty_generics #where_clause {
#def_method
}
}
} else {
quote! {}
};

TokenStream::from(quote! {
impl #impl_generics #ident #ty_generics #where_clause {
#(#methods)*
}
#default_impl
})
}

Expand Down
File renamed without changes.
31 changes: 31 additions & 0 deletions tests/enum_variant_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,34 @@ fn test_variant_config_overrides_default_values() {

// variant3 was not generated
}




#[derive(ctor, Debug, PartialEq)]
enum DefaultVariantEnum {
#[allow(dead_code)]
Variant1,
#[ctor(default)]
Variant2 { #[ctor(default)] value: i32 }
}

#[test]
fn test_default_variant_enum() {
let variant2 = Default::default();
assert_eq!(DefaultVariantEnum::Variant2 { value: 0 }, variant2);
}

#[derive(ctor, Debug, PartialEq)]
enum DefaultAllEnum {
#[allow(dead_code)]
Variant1,
#[ctor(default(all))]
Variant2 { value: i32, #[ctor(expr("A".to_string()))] other: String }
}

#[test]
fn test_default_all_variant_enum() {
let variant2 = Default::default();
assert_eq!(DefaultAllEnum::Variant2 { value: 0, other: "A".to_string() }, variant2)
}