#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub enum AssetValue {
Title(crate::response::Title),
Img(crate::response::Image),
Video(crate::response::Video),
Data(crate::response::Data),
}
impl AssetValue {
pub fn is_title(&self) -> bool {
self.as_title().is_some()
}
pub fn as_title(&self) -> Option<&crate::response::Title> {
match self {
Self::Title(title) => Some(title),
_ => None,
}
}
pub fn as_title_mut(&mut self) -> Option<&mut crate::response::Title> {
match self {
Self::Title(ref mut title) => Some(title),
_ => None,
}
}
pub fn is_image(&self) -> bool {
self.as_image().is_some()
}
pub fn as_image(&self) -> Option<&crate::response::Image> {
match self {
Self::Img(image) => Some(image),
_ => None,
}
}
pub fn as_image_mut(&mut self) -> Option<&mut crate::response::Image> {
match self {
Self::Img(ref mut image) => Some(image),
_ => None,
}
}
pub fn is_video(&self) -> bool {
self.as_video().is_some()
}
pub fn as_video(&self) -> Option<&crate::response::Video> {
match self {
Self::Video(video) => Some(video),
_ => None,
}
}
pub fn as_video_mut(&mut self) -> Option<&mut crate::response::Video> {
match self {
Self::Video(ref mut video) => Some(video),
_ => None,
}
}
pub fn is_data(&self) -> bool {
self.as_data().is_some()
}
pub fn as_data(&self) -> Option<&crate::response::Data> {
match self {
Self::Data(data) => Some(data),
_ => None,
}
}
pub fn as_data_mut(&mut self) -> Option<&mut crate::response::Data> {
match self {
Self::Data(ref mut data) => Some(data),
_ => None,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn json() -> serde_json::Result<()> {
let json = r#"{"title":{"text":""}}"#;
let o1 = AssetValue::Title(Default::default());
assert_eq!(serde_json::to_string(&o1)?, json);
assert_eq!(o1, serde_json::from_str::<AssetValue>(json)?);
Ok(())
}
}