You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tiny crate to set default values for serde fields via inline attribute declaration.
3
+
A tiny crate to set default values for serde struct fields via inline attribute declaration.
4
4
5
5
## Overview
6
6
7
-
This crate is an approach to do what [serde-rs/serde#368](https://github.com/serde-rs/serde/issues/368) purposes.
8
-
If you want to set default values in plain [`serde`](https://serde.rs/), you have to create a function and link to it with `#[serde(default = "...")`.
9
-
This may be good if you need to do calculations to get the default value, but often you just want a simple integer or string to be the default value and have to create a whole function to return a hard-coded value.
7
+
This crate is an approach to do what [serde-rs/serde#368](https://github.com/serde-rs/serde/issues/368) purposes: Defining default values for struct fields via inline declaration instead of creating a separate function for it.
8
+
9
+
So instead of writing something like this, which can get very verbose quickly with many fields:
10
10
```rust
11
11
#[derive(Deserialize)]
12
12
structTest {
@@ -16,11 +16,7 @@ struct Test {
16
16
17
17
fnvalue_default() ->u32 { 42 }
18
18
```
19
-
20
-
That can get quiet messy if you have many fields with many (different) default values.
21
-
This crate tries to solve this issue by providing the `#[serde_inline_default]` proc macro.
22
-
With this macro set at the struct level, you can set default values via `#[serde_inline_default(...)]` for your serde fields inline, without creating an extra function.
23
-
19
+
you can just do this:
24
20
```rust
25
21
#[serde_inline_default]
26
22
#[derive(Deserialize)]
@@ -31,10 +27,10 @@ struct Test {
31
27
```
32
28
33
29
> [!IMPORTANT]
34
-
> **`#[serde_inline_default]` must be set before `#[derive(Deserialize)]`/`#[derive(Serialize)]`, otherwise it's not working correctly!**
30
+
> **`#[serde_inline_default]` must be set before `#[derive(Deserialize)]`/`#[derive(Serialize)]`!**
35
31
36
32
Internally, `#[serde_inline_default(...)]` gets expanded to a function which returns the set value and the attribute is replaced with `#[serde(default = "<function name>")]`.
37
-
So this macro is just some syntax sugar for you, but can get quiet handy if you want to keep your code clean or write declarative macros / `macro_rules!`.
33
+
So this macro is just some syntax sugar for you, but can get quite handy if you want to keep your code clean or write declarative macros / `macro_rules!`.
38
34
39
35
## Alternatives
40
36
@@ -84,3 +80,4 @@ This project is licensed under either of the following licenses, at your option:
84
80
85
81
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
86
82
- MIT License ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
0 commit comments