mirror of
https://github.com/bytedream/serde-inline-default.git
synced 2025-06-27 18:40:31 +02:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
8d1d49150f | |||
76727e73f4 | |||
d8a0dff1af | |||
81b089be37 |
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "serde-inline-default"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
authors = ["ByteDream"]
|
||||
edition = "2021"
|
||||
description = "Serde default values via inline declaration"
|
||||
@ -13,6 +13,8 @@ categories = ["encoding"]
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
|
@ -1,10 +1,10 @@
|
||||
# serde-inline-default [](https://github.com/ByteDream/serde-inline-default/actions/workflows/ci.yml) [](https://crates.io/crates/serde-inline-default) [](https://docs.rs/crunchyroll-rs/latest/serde-inline-default/)
|
||||
# serde-inline-default [](https://github.com/ByteDream/serde-inline-default/actions/workflows/ci.yml) [](https://crates.io/crates/serde-inline-default) [](https://docs.rs/serde-inline-default/latest/serde_inline_default/)
|
||||
|
||||
Tiny crate to set default values for serde fields via inline attribute declaration.
|
||||
|
||||
## Overview
|
||||
|
||||
This crate is an approach to do what serde-rs/serde#368 purposes.
|
||||
This crate is an approach to do what [serde-rs/serde#368](https://github.com/serde-rs/serde/issues/368) purposes.
|
||||
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 = "...")`.
|
||||
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.
|
||||
```rust
|
||||
@ -37,5 +37,5 @@ So this macro is just some syntax sugar for you, but can get quiet handy if you
|
||||
|
||||
This project is licensed under either of the following licenses, at your option:
|
||||
|
||||
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
||||
- MIT License ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
|
||||
- MIT License ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
|
||||
|
24
src/lib.rs
24
src/lib.rs
@ -1,8 +1,32 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{parse_macro_input, Item};
|
||||
|
||||
mod expand;
|
||||
|
||||
/// The main macro of this crate.
|
||||
/// Use it to define default values of fields in structs you [`Serialize`] or [`Deserialize`].
|
||||
/// You do not need to create a extra function to provide the default value, as it is the case in serdes' implementation of default (`#[serde(default = "...")]`).
|
||||
///
|
||||
/// Set this macro on a struct where you use [`Serialize`] or [`Deserialize`] and use `#[serde_inline_default(...)]` on the field you want to have a inline default value.
|
||||
/// Replace the `...` with the value you want and it will be set as default if serde needs it.
|
||||
///
|
||||
/// Note that you must set this macro _before_ `#[derive(Serialize)]` / `#[derive(Deserialize)]` as it wouldn't work properly if set after the derive.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// #[serde_inline_default]
|
||||
/// #[derive(Deserialize)]
|
||||
/// struct Test {
|
||||
/// #[serde_inline_default(42)]
|
||||
/// value: u32
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// [`Serialize`]: https://docs.rs/serde/*/serde/trait.Serialize.html
|
||||
/// [`Deserialize`]: https://docs.rs/serde/*/serde/trait.Deserialize.html
|
||||
#[proc_macro_attribute]
|
||||
pub fn serde_inline_default(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
let item = parse_macro_input!(input as Item);
|
||||
|
Reference in New Issue
Block a user