#serde #serialization #validation #wrapper

macro derive-serialize-into

Derive Serialize and Deserialize for validating wrapper types

5 unstable releases

0.3.1 Apr 14, 2018
0.3.0 Apr 14, 2018
0.2.1 Apr 13, 2018
0.2.0 Apr 12, 2018
0.1.0 Apr 12, 2018

#28 in #validation

Download history 47/week @ 2019-11-27 52/week @ 2019-12-04 409/week @ 2019-12-11 8/week @ 2019-12-18 5/week @ 2019-12-25 35/week @ 2020-01-08 9/week @ 2020-01-15 1/week @ 2020-01-22 5/week @ 2020-02-12 11/week @ 2020-02-19 11/week @ 2020-02-26 10/week @ 2020-03-04 6/week @ 2020-03-11

267 downloads per month

MIT license

22KB
238 lines

Documentation Latest version

Derive Serialize, Deserialize for wrapper types

This crate provides several custom derives that provide implementations of serde's Serialize and Deserialize traits for wrapper types, as well as Deserialize implementations that perform some validation.

Sometimes you have a single-field type

#[derive(DeserializeFrom, FromInner, IntoInner, SerializeInto)]
struct Contact {
    email: String,
}

which you want to serialize and deserialize as a string instead of a struct, e.g. you want its JSON representation to just be ""user@domain.com"" instead of "{ "email": "user@domain.com" }". The above derive attribute creates Serialize and Deserialize implementations for that purpose, as well as Into and From implementations to convert between String and Contact.

Another example is a validated wrapper type like

#[derive(DeserializeTryFrom, TryFromInner)]
#[try_from_inner = "validator::validate_email"]
struct Email(String);

or

#[derive(DeserializeTryFrom, TryFromInner)]
#[try_from_inner_regex = "^\\+?[[:digit:]]+$"]
struct Phone(String);

that should never be instantianted with a string that doesn't represent a valid email address or phone number. The above examples create Deserialize and TryFrom implementations accordingly.

Dependencies

~1.5MB
~30K SLoC