1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/// 5.3 [`Title`], 5.4 [`Image`], 5.5 [`Data`], 5.6 [`Video`]
///
/// [`Title`]: ./struct.Title.html
/// [`Image`]: ./struct.Image.html
/// [`Data`]: ./struct.Data.html
/// [`Video`]: ./struct.Video.html
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub enum AssetValue<'a> {
    /// optional; object; -
    /// Title object for title assets. See TitleObject definition.
    #[serde(borrow)]
    Title(crate::response::Title<'a>),
    /// optional; object; -
    /// Image object for image assets. See ImageObject definition.
    #[serde(borrow)]
    Img(crate::response::Image<'a>),
    /// optional; object; -
    /// Video object for video assets. See Video response object definition. Note that in-stream
    /// video ads are not part of Native. Native ads may contain a video as the ad creative itself.
    #[serde(borrow)]
    Video(crate::response::Video<'a>),
    /// optional; object; -
    /// Data object for ratings, prices etc.
    #[serde(borrow)]
    Data(crate::response::Data<'a>),
}

impl<'a> AssetValue<'a> {
    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(&'a mut self) -> Option<&'a 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(&'a mut self) -> Option<&'a 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(&'a mut self) -> Option<&'a 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(&'a mut self) -> Option<&'a 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(())
    }
}