-
|
Hello, use serde::Serialize;
use serde_with::{serde_as, skip_serializing_none, DisplayFromStr};
pub enum Section {
Authentication,
}
pub enum Documentation { // Use the `Display` trait of its variant to implement its own `Display` trait.
Section(Section), // Implements Display
Tag(Tag), // Enum like Section, Implements Display
Operation(Operation), // Enum Like Section Implements Display
}
#[skip_serializing_none]
#[serde_as]
#[derive(Serialize)]
pub struct ApiErrorModel {
error: ErrorCode,
error_description: Option<String>,
#[serde_as(as = "DisplayFromStr")]
error_uri: Option<Documentation>
}Cargo output: But this requires the implementation of the Does there is a way to do it with
Or do I have to implement the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
If your #[serde_as(as = "Option<DisplayFromStr>")]
error_uri: Option<Documentation>,Writing In general, the idea of An alternative is to use #[derive(serde_with::SerializeDisplay)]
pub enum Documentation { ... } |
Beta Was this translation helpful? Give feedback.
If your
DocumentationimplementsDisplay, then you write this. If you needDeserializeyou also want to add#[serde(default)], otherwise the field will not be optional while deserializing. This issue #185 has the details.Writing
#[serde_as(as = "Option<DisplayFromStr>")]means there is aOption<T>whereT: Display. If you write#[serde_as(as = "DisplayFromStr")]it requires your field to beDisplay, hereOption<Documentation>: Display, butOptionnever implementsDisplay, so this will not work.In general, the idea of
serde_asis that you attribute mirrors the structure of your type, but you place a modi…