serde-textual

Crates.ioserde-textual
lib.rsserde-textual
version
sourcesrc
created_at2024-10-24 13:29:59.986986
updated_at2024-10-24 13:41:01.236751
descriptionderive Display/FromStr by using serde
homepage
repositoryhttps://github.com/ryo33/serde-textual
max_upload_size
id1421318
Cargo.toml error:TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Ryo Hirayama (ryo33)

documentation

README

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.

Commit count: 4

cargo fmt