openrtb_native1/response/
asset_value.rs1#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
8#[serde(rename_all = "snake_case")]
9pub enum AssetValue {
10 Title(crate::response::Title),
13 Img(crate::response::Image),
16 Video(crate::response::Video),
20 Data(crate::response::Data),
23}
24
25impl AssetValue {
26 pub fn is_title(&self) -> bool {
27 self.as_title().is_some()
28 }
29
30 pub fn as_title(&self) -> Option<&crate::response::Title> {
31 match self {
32 Self::Title(title) => Some(title),
33 _ => None,
34 }
35 }
36
37 pub fn as_title_mut(&mut self) -> Option<&mut crate::response::Title> {
38 match self {
39 Self::Title(ref mut title) => Some(title),
40 _ => None,
41 }
42 }
43
44 pub fn is_image(&self) -> bool {
45 self.as_image().is_some()
46 }
47
48 pub fn as_image(&self) -> Option<&crate::response::Image> {
49 match self {
50 Self::Img(image) => Some(image),
51 _ => None,
52 }
53 }
54
55 pub fn as_image_mut(&mut self) -> Option<&mut crate::response::Image> {
56 match self {
57 Self::Img(ref mut image) => Some(image),
58 _ => None,
59 }
60 }
61
62 pub fn is_video(&self) -> bool {
63 self.as_video().is_some()
64 }
65
66 pub fn as_video(&self) -> Option<&crate::response::Video> {
67 match self {
68 Self::Video(video) => Some(video),
69 _ => None,
70 }
71 }
72
73 pub fn as_video_mut(&mut self) -> Option<&mut crate::response::Video> {
74 match self {
75 Self::Video(ref mut video) => Some(video),
76 _ => None,
77 }
78 }
79
80 pub fn is_data(&self) -> bool {
81 self.as_data().is_some()
82 }
83
84 pub fn as_data(&self) -> Option<&crate::response::Data> {
85 match self {
86 Self::Data(data) => Some(data),
87 _ => None,
88 }
89 }
90
91 pub fn as_data_mut(&mut self) -> Option<&mut crate::response::Data> {
92 match self {
93 Self::Data(ref mut data) => Some(data),
94 _ => None,
95 }
96 }
97}
98
99#[cfg(test)]
100mod test {
101 use super::*;
102
103 #[test]
104 fn json() -> serde_json::Result<()> {
105 let json = r#"{"title":{"text":""}}"#;
106 let o1 = AssetValue::Title(Default::default());
107 assert_eq!(serde_json::to_string(&o1)?, json);
108 assert_eq!(o1, serde_json::from_str::<AssetValue>(json)?);
109
110 Ok(())
111 }
112}