Skip to content

Commit 0ee8303

Browse files
committed
docs: simplify readme
1 parent 8a863f3 commit 0ee8303

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# serde-inline-default [![ci](https://github.com/ByteDream/serde-inline-default/actions/workflows/ci.yml/badge.svg)](https://github.com/ByteDream/serde-inline-default/actions/workflows/ci.yml) [![crates.io](https://img.shields.io/crates/v/serde-inline-default)](https://crates.io/crates/serde-inline-default) [![crates.io downloads](https://img.shields.io/crates/d/serde-inline-default)](https://crates.io/crates/serde-inline-default) [![docs](https://img.shields.io/docsrs/serde-inline-default)](https://docs.rs/serde-inline-default/latest/serde_inline_default/)
22

3-
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.
44

55
## Overview
66

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:
1010
```rust
1111
#[derive(Deserialize)]
1212
struct Test {
@@ -16,11 +16,7 @@ struct Test {
1616

1717
fn value_default() -> u32 { 42 }
1818
```
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:
2420
```rust
2521
#[serde_inline_default]
2622
#[derive(Deserialize)]
@@ -31,10 +27,10 @@ struct Test {
3127
```
3228

3329
> [!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)]`!**
3531
3632
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!`.
3834

3935
## Alternatives
4036

@@ -84,3 +80,4 @@ This project is licensed under either of the following licenses, at your option:
8480

8581
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
8682
- MIT License ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
83+
]()

0 commit comments

Comments
 (0)