#[derive(SerdeField)]
{
// Attributes available to this derive:
#[serde]
}
Expand description
Derive enum and constants for Serde field-names.
This macro generates:
- A
const SERDE_FIELDS: &'static [&'static str]on the struct, containing the serialized names of all fields (taking#[serde(rename = "...")]and#[serde(rename_all = "...")]into account). - An enum named
{StructName}SerdeFieldwith variants for each field:- Each variant is named after the Rust field name (PascalCase).
- Each variant is annotated with
#[serde(rename = "...")].
- Implementations for:
as_str() -> &'static strDisplayFrom<{StructName}SerdeField> for &'static strFrom<&{StructName}SerdeField> for &'static strTryFrom<&str>andTryFrom<String>with errorInvalid{StructName}SerdeFieldFromStrAsRef<str>
ยงExample
use serde_fields::SerdeField;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, SerdeField)]
#[serde(rename_all = "camelCase")]
struct User {
#[serde(rename = "id")]
user_id: u32,
email: String,
}
// Access field-names as string slice
assert_eq!(User::SERDE_FIELDS, &["id", "email"]);
// Use the generated enum
let f = UserSerdeField::UserId;
assert_eq!(f.as_str(), "id");
assert_eq!(f.to_string(), "id");
// TryFrom & FromStr
let parsed: UserSerdeField = "id".parse().unwrap();
assert_eq!(parsed, UserSerdeField::UserId);