Crates.io | serde-textual |
lib.rs | serde-textual |
version | |
source | src |
created_at | 2024-10-24 13:29:59.986986 |
updated_at | 2024-10-24 13:41:01.236751 |
description | derive Display/FromStr by using serde |
homepage | |
repository | https://github.com/ryo33/serde-textual |
max_upload_size | |
id | 1421318 |
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` |
size | 0 |
derive Display
/FromStr
by using serde
's Serialize
/Deserialize
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");
Uuid
, Url
, EmailAddress
, etcDon'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.
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());
Licensed under either of
at your option.