Skip to content

Commit

Permalink
Merge pull request #8 from ImaMapleTree/feature/release-0.2.3-default…
Browse files Browse the repository at this point in the history
…-all

Added default(all) and split crate into multiple features
  • Loading branch information
ImaMapleTree authored May 30, 2024
2 parents d7ad8ba + 100caaa commit 743255c
Show file tree
Hide file tree
Showing 25 changed files with 760 additions and 398 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "derive-ctor"
version = "0.2.2"
description = "Adds `#[derive(ctor)]` which allows for the auto-generation of a constructor."
version = "0.2.3"
description = "Adds `#[derive(ctor)]` which allows for the auto-generation of struct and enum constructors."
keywords = ["derive", "macro", "trait", "procedural", "no_std"]
authors = ["Evan Cowin"]
license = "MIT"
Expand All @@ -10,11 +10,16 @@ edition = "2021"
exclude = [".github/*", ".gitignore"]
categories = ["no-std", "rust-patterns"]

[features]
default = ["structs", "enums"]
enums = ["dep:heck"]
structs = []

[lib]
proc-macro = true

[dependencies]
syn = { version = "2.0.*" }
quote = { version = "1.*" }
proc-macro2 = { version = "1.0.*" }
heck = { version = "0.5.*" }
heck = { version = "0.5.*", optional = true }
18 changes: 15 additions & 3 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 = "0.2.2"
derive-ctor = "0.2.3"
```

Annotate your struct with `#[derive(ctor)]` to automatically generate a `new` constructor:
Expand Down Expand Up @@ -68,13 +68,14 @@ let my_struct3 = MyStruct::internal(300, "C".to_string());
```

### Auto-implement "Default" Trait
The `Default` trait can be auto implemented by specifying a ctor with the name "Default" in the ctor attribute. Note: all fields must have a generated value in order for the implementation to be valid.
The `Default` trait can be auto implemented by specifying a ctor with the name `default` in the ctor attribute. Note: all fields must have a generated value in order for the implementation to be valid.
Additionally, declaring `default(all)` will automatically mark all non-annotated fields with `#[ctor(default)]`

```rust
use derive_ctor::ctor;

#[derive(ctor)]
#[ctor(Default)]
#[ctor(default)]
struct MyStruct {
#[ctor(default)]
field1: i32,
Expand All @@ -83,6 +84,17 @@ struct MyStruct {
}

let default: MyStruct = Default::default();


#[derive(ctor)]
#[ctor(default(all))]
struct OtherStruct {
field1: i32,
#[ctor(expr(true))]
field2: bool
}

let default2: OtherStruct = Default::default();
```

## Enum Configurations
Expand Down
31 changes: 31 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// while redundant this file exists as a possible user-reference to discover all possible properties
// available to them for a certain element


// error messages
pub(crate) const CONFIG_PROP_ERR_MSG: &str =
"Unexpected property: \"{prop}\" (must be one of the following: \"{values}\")";
pub(crate) const DEFAULT_CTOR_ERR_MSG: &str =
"Default constructor requires field to generate its own value.";

pub(crate) const CTOR_WORD: &str = "ctor";

// valid field properties
pub(crate) const FIELD_PROP_CLONED: &str = "cloned";
pub(crate) const FIELD_PROP_DEFAULT: &str = "default";
pub(crate) const FIELD_PROP_EXPR: &str = "expr";
pub(crate) const FIELD_PROP_INTO: &str = "into";
pub(crate) const FIELD_PROP_ITER: &str = "iter";

// valid enum-config properties
pub(crate) const ENUM_PROP_PREFIX: &str = "prefix";
pub(crate) const ENUM_PROP_VISIBILITY: &str = "visibility";
pub(crate) const ENUM_PROP_VIS: &str = "vis";

// variation property
pub(crate) const ENUM_VARIATION_PROP_NONE: &str = "none";

// struct config properties
pub(crate) const STRUCT_PROP_DEFAULT: &str = "default";
// property used within the default() prop
pub(crate) const NESTED_PROP_ALL: &str = "all";
Loading

0 comments on commit 743255c

Please sign in to comment.