4 Commits

Author SHA1 Message Date
8d1d49150f build: update version to 0.1.1 2023-02-11 19:45:26 +01:00
76727e73f4 docs: add documentation to macro function 2023-02-11 19:36:54 +01:00
d8a0dff1af docs: add README as library documentation 2023-02-11 18:51:49 +01:00
81b089be37 docs: fix docs.rs link 2023-02-11 18:49:17 +01:00
3 changed files with 31 additions and 5 deletions

View File

@ -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"

View File

@ -1,10 +1,10 @@
# 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) [![docs](https://img.shields.io/docsrs/serde-inline-default)](https://docs.rs/crunchyroll-rs/latest/serde-inline-default/)
# 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) [![docs](https://img.shields.io/docsrs/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>)

View File

@ -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);