2 releases

0.1.1 Oct 24, 2024
0.1.0 Oct 24, 2024

#1250 in Rust patterns

23 downloads per month

MIT/Apache

10KB
125 lines

serde-textual

GitHub MIT/Apache 2.0 Crates.io docs.rs

derive Display/FromStr by using serde's Serialize/Deserialize

Usage examples

Enum using serde's rename_all or rename

use serde::{Serialize, Deserialize};
use serde_textual::{FromStrSerde, DisplaySerde};

#[derive(FromStrSerde, DisplaySerde, Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
enum Kind {
    A,
    B,
}

assert_eq!("a".parse::<Kind>().unwrap(), Kind::A);
assert_eq!(Kind::B.to_string(), "b");

New types with Uuid, Url, EmailAddress, etc

Don't forget to activate serde feature of those crates.

use serde::{Serialize, Deserialize};
use serde_textual::{FromStrSerde, DisplaySerde};

#[derive(FromStrSerde, DisplaySerde, Serialize, Deserialize, Debug, PartialEq)]
#[serde(transparent)]
struct NewUuid(uuid::Uuid);

let new_uuid = NewUuid("95c639c4-0c2f-4026-900c-6144c6c39b1f".parse().unwrap());
let s = "95c639c4-0c2f-4026-900c-6144c6c39b1f";

assert_eq!(new_uuid, s.parse::<NewUuid>().unwrap());
assert_eq!(s, new_uuid.to_string());

In the above example, NewUuid's Display/FromStr delegates to NewUuid's Serialize/Deserialize that delegates to Uuid's Serialize/Deserialize. If you need to implement NewUuid's Display/FromStr to directly delegate to Uuid's Display/FromStr (for compile-time or runtime performance reason), use other crates that supports Display/FromStr delegation, or do it manually.

[Advanced] Comma separated values of enum variants

serde_with's StringWithSeparator<Separator, T> requires T to implement Display/FromStr. It's convenient to use serde-textual to implement Display/FromStr for the T.

use serde::{Deserialize, Serialize};
use serde_textual::{assert_textual, DisplaySerde, FromStrSerde};
use serde_with::{formats::CommaSeparator, serde_as, StringWithSeparator};

#[serde_with::serde_as]
#[derive(FromStrSerde, DisplaySerde, Serialize, Deserialize, Debug, PartialEq)]
#[serde(transparent)]
struct RequiredFields(#[serde_as(as = "StringWithSeparator<CommaSeparator, Field>")] Vec<Field>);

#[derive(FromStrSerde, DisplaySerde, Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
enum Field {
    Id,
    Name,
    CreatedAt,
    UpdatedAt,
}

let required_fields = RequiredFields(vec![Field::Id, Field::Name, Field::CreatedAt]);
let s = "id,name,createdAt";

assert_eq!(required_fields, s.parse::<RequiredFields>().unwrap());
assert_eq!(s, required_fields.to_string());

License

Licensed under either of

  • Apache-2.0
  • MIT

at your option.

Dependencies

~225–660KB
~16K SLoC