Prevent redudent serialize impl for json payload and url encoded params

I have the impl of a lot of types like the following, where I impl serde Serdialize/Deserialze traits, partially manually, mostly by derive, but I also need the type for filter queries.

Is there a nifty trick to use trait Serialize for trait fmt::Display and as_str()/to_string - respectively trait Deserialize with trait FromStr and parse

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum Status {
    Unknown,
    Scheduled,
    Sent,
    Buffered,
    Delivered,
    Expired,
    DeliveryFailed,
}

impl Status {
    pub fn as_str(&self) -> &str {
        match self {
            Status::Scheduled => "scheduled",
            Status::Sent => "sent",
            Status::Buffered => "buffered",
            Status::Delivered => "delivered",
            Status::Expired => "expired",
            Status::DeliveryFailed => "delivery_failed",
            _ => "invalid",
        }
    }
}

Any hint would be much appreciated :slight_smile:

https://crates.io/crates/serde_plain should help.

2 Likes

Thanks, with a custom derive macro this will work!