insta/types/
additional_data_loaded.rs

1use std::collections::HashMap;
2use url::Url;
3
4/// A u8 was not a valid media type
5#[derive(Debug)]
6pub struct InvalidMediaTypeError(u8);
7
8impl std::fmt::Display for InvalidMediaTypeError {
9    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10        write!(f, "`{}` is not a valid media type", self.0)
11    }
12}
13
14impl std::error::Error for InvalidMediaTypeError {}
15
16#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, serde::Deserialize)]
17#[serde(try_from = "u8")]
18pub enum MediaType {
19    /// A Photo
20    Photo,
21
22    /// A video
23    Video,
24
25    /// A carousel
26    Carousel,
27}
28
29impl TryFrom<u8> for MediaType {
30    type Error = InvalidMediaTypeError;
31
32    fn try_from(n: u8) -> Result<Self, Self::Error> {
33        match n {
34            1 => Ok(Self::Photo),
35            2 => Ok(Self::Video),
36            8 => Ok(Self::Carousel),
37            _ => Err(InvalidMediaTypeError(n)),
38        }
39    }
40}
41
42/// AdditionalDataLoaded
43#[derive(Debug, serde::Deserialize)]
44pub struct AdditionalDataLoaded {
45    /// ?
46    pub num_results: u32,
47
48    /// ?
49    pub items: Vec<AdditionalDataLoadedItem>,
50
51    /// ?
52    pub auto_load_more_enabled: bool,
53
54    /// ?
55    pub more_available: bool,
56
57    /// Extra fields
58    #[serde(flatten)]
59    pub extra: HashMap<String, serde_json::Value>,
60}
61
62#[derive(Debug, serde::Deserialize)]
63pub struct AdditionalDataLoadedItem {
64    /// The media type
65    pub media_type: MediaType,
66
67    /// Versions of a video post.
68    ///
69    /// Only present on video posts
70    pub video_versions: Option<Vec<VideoVersion>>,
71
72    /// Versions of an image post
73    pub image_versions2: Option<ImageVersions2>,
74
75    /// Carousel media
76    pub carousel_media: Option<Vec<CarouselMediaItem>>,
77
78    /// The post code
79    pub code: String,
80
81    /// Extra fields
82    #[serde(flatten)]
83    pub extra: HashMap<String, serde_json::Value>,
84}
85
86impl AdditionalDataLoadedItem {
87    /// Get the best image_versions2 candidate
88    pub fn get_best_image_versions2_candidate(&self) -> Option<&ImageVersions2Candidate> {
89        self.image_versions2.as_ref()?.get_best()
90    }
91
92    /// Get the best video version
93    pub fn get_best_video_version(&self) -> Option<&VideoVersion> {
94        self.video_versions
95            .as_ref()?
96            .iter()
97            .max_by_key(|video_version| video_version.height)
98    }
99}
100
101#[derive(Debug, serde::Deserialize)]
102pub struct VideoVersion {
103    /// The height in pixels
104    pub height: u32,
105
106    /// The width in pixels
107    pub width: u32,
108
109    /// ?
110    #[serde(rename = "type")]
111    pub kind: u32,
112
113    /// the src url
114    pub url: Url,
115
116    /// ?
117    pub id: String,
118
119    /// Extra fields
120    #[serde(flatten)]
121    pub extra: HashMap<String, serde_json::Value>,
122}
123
124/// The image_versions2 field
125#[derive(Debug, serde::Deserialize)]
126pub struct ImageVersions2 {
127    /// Candidate images
128    pub candidates: Vec<ImageVersions2Candidate>,
129
130    /// Extra fields
131    #[serde(flatten)]
132    pub extra: HashMap<String, serde_json::Value>,
133}
134
135impl ImageVersions2 {
136    /// Get the best candidate
137    pub fn get_best(&self) -> Option<&ImageVersions2Candidate> {
138        self.candidates
139            .iter()
140            .max_by_key(|image_versions2_candidate| image_versions2_candidate.height)
141    }
142}
143
144/// A ImageVersions2 candidate
145#[derive(Debug, serde::Deserialize)]
146pub struct ImageVersions2Candidate {
147    /// The image height in pixels
148    pub width: u32,
149
150    /// The image width in pixels
151    pub height: u32,
152
153    /// The url
154    pub url: Url,
155
156    /// Extra fields
157    #[serde(flatten)]
158    pub extra: HashMap<String, serde_json::Value>,
159}
160
161/// An item in carousel_media
162#[derive(Debug, serde::Deserialize)]
163pub struct CarouselMediaItem {
164    /// The media type
165    pub media_type: MediaType,
166
167    /// Image versions
168    pub image_versions2: Option<ImageVersions2>,
169
170    /// Extra fields
171    #[serde(flatten)]
172    pub extra: HashMap<String, serde_json::Value>,
173}
174
175impl CarouselMediaItem {
176    /// Get the best image_versions2 candidate
177    pub fn get_best_image_versions2_candidate(&self) -> Option<&ImageVersions2Candidate> {
178        self.image_versions2.as_ref()?.get_best()
179    }
180}