pexels_sdk/domain/
models.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents the response for a list of collections.
4#[derive(Serialize, Deserialize, Debug, Clone)]
5pub struct CollectionsResponse {
6    pub collections: Vec<Collection>,
7    pub page: u32,
8    pub per_page: u32,
9    pub total_results: u32,
10    pub next_page: Option<String>,
11    pub prev_page: Option<String>,
12}
13
14/// Represents a Pexels collection.
15#[derive(Serialize, Deserialize, Debug, Clone)]
16pub struct Collection {
17    pub id: String,
18    pub title: String,
19    pub description: Option<String>,
20    pub private: bool,
21    pub media_count: u32,
22    pub photos_count: u32,
23    pub videos_count: u32,
24}
25
26/// Represents the response for a list of media items.
27#[derive(Serialize, Deserialize, Debug, Clone)]
28pub struct MediaResponse {
29    pub id: String,
30    pub media: Vec<MediaType>, // An array of media objects. Each object has an extra type attribute to indicate the type of object.
31    pub page: u32,
32    pub per_page: u32,
33    pub total_results: u32,
34    pub next_page: Option<String>,
35    pub prev_page: Option<String>,
36}
37
38/// Enum representing the type of media.
39/// Supported values are `photos` and `videos`.
40#[derive(Serialize, Deserialize, Debug, Clone)]
41#[serde(tag = "type")]
42pub enum MediaType {
43    Photo(MediaPhoto),
44    Video(MediaVideo),
45}
46
47// Manual implementation From<MediaType> to fill type_ field in MediaPhoto and MediaVideo
48impl From<MediaType> for MediaPhoto {
49    fn from(media: MediaType) -> Self {
50        match media {
51            MediaType::Photo(photo) => MediaPhoto {
52                type_: "Photo".to_string(),
53                ..photo
54            },
55            _ => panic!("Expected Photo"),
56        }
57    }
58}
59
60impl From<MediaType> for MediaVideo {
61    fn from(media: MediaType) -> Self {
62        match media {
63            MediaType::Video(video) => MediaVideo {
64                type_: "Video".to_string(),
65                ..video
66            },
67            _ => panic!("Expected Video"),
68        }
69    }
70}
71
72/// Represents a photo media object.
73#[derive(Serialize, Deserialize, Debug, Clone)]
74pub struct MediaPhoto {
75    #[serde(skip)]
76    pub type_: String,
77    pub id: u32,
78    pub width: u32,
79    pub height: u32,
80    pub url: Option<String>,
81    pub photographer: Option<String>,
82    pub photographer_url: Option<String>,
83    pub photographer_id: u32,
84    pub avg_color: String,
85    pub src: PhotoSrc,
86    pub liked: bool,
87    pub alt: String,
88}
89
90/// Represents a video media object.
91#[derive(Serialize, Deserialize, Debug, Clone)]
92pub struct MediaVideo {
93    #[serde(skip)]
94    pub type_: String,
95    pub id: u32,
96    pub width: u32,
97    pub height: u32,
98    pub duration: u32,
99    pub full_res: Option<String>,
100    pub tags: Vec<String>,
101    pub url: Option<String>,
102    pub image: Option<String>,
103    pub avg_color: Option<String>,
104    pub user: User,
105    pub video_files: Vec<VideoFile>,
106    pub video_pictures: Vec<VideoPicture>,
107}
108
109/// Represents a Pexels photo.
110#[derive(Serialize, Deserialize, Debug, Clone)]
111pub struct Photo {
112    pub id: u32,
113    pub width: u32,
114    pub height: u32,
115    pub url: String,
116    pub photographer: String,
117    pub photographer_url: String,
118    pub photographer_id: u32,
119    pub avg_color: String,
120    pub src: PhotoSrc,
121    pub liked: bool,
122    pub alt: String,
123}
124
125/// Represents different image sizes for a photo.
126#[derive(Serialize, Deserialize, Debug, Clone)]
127pub struct PhotoSrc {
128    pub original: String,
129    pub large2x: String,
130    pub large: String,
131    pub medium: String,
132    pub small: String,
133    pub portrait: String,
134    pub landscape: String,
135    pub tiny: String,
136}
137
138/// Represents the response for a list of photos.
139#[derive(Serialize, Deserialize, Debug, Clone)]
140pub struct PhotosResponse {
141    pub total_results: u32,
142    pub page: u32,
143    pub per_page: u32,
144    pub photos: Vec<Photo>,
145    pub next_page: Option<String>,
146    pub prev_page: Option<String>,
147}
148
149/// Represents the response for a list of videos.
150#[derive(Serialize, Deserialize, Debug, Clone)]
151pub struct VideoResponse {
152    pub page: u32,
153    pub per_page: u32,
154    pub total_results: u32,
155    pub url: String,
156    pub videos: Vec<Video>,
157    pub prev_page: Option<String>,
158    pub next_page: Option<String>,
159}
160
161/// Represents a Pexels video.
162#[derive(Serialize, Deserialize, Debug, Clone)]
163pub struct Video {
164    #[serde(default)]
165    pub avg_color: Option<String>,
166    #[serde(default)]
167    pub duration: Option<u32>,
168    #[serde(default)]
169    pub full_res: Option<String>,
170    pub height: u32,
171    pub id: u32,
172    #[serde(rename = "image")]
173    pub image_url: String,
174    #[serde(default)]
175    pub tags: Vec<String>,
176    #[serde(rename = "url")]
177    pub video_url: String,
178    pub user: User,
179    pub video_files: Vec<VideoFile>,
180    pub video_pictures: Vec<VideoPicture>,
181    pub width: u32,
182}
183
184/// Represents a user who created a media item.
185#[derive(Serialize, Deserialize, Debug, Clone)]
186pub struct User {
187    pub id: u32,
188    pub name: String,
189    #[serde(rename = "url")]
190    pub user_url: String,
191}
192
193/// Represents a video file with different qualities.
194#[derive(Serialize, Deserialize, Debug, Clone)]
195pub struct VideoFile {
196    pub file_type: String,
197    pub fps: f64,
198    pub height: u32,
199    pub id: u32,
200    #[serde(rename = "link")]
201    pub file_link: String,
202    #[serde(default)]
203    pub quality: Option<String>,
204    pub size: u64,
205    pub width: u32,
206}
207
208/// Represents a preview picture of a video.
209#[derive(Serialize, Deserialize, Debug, Clone)]
210pub struct VideoPicture {
211    pub id: u32,
212    pub nr: u32,
213    #[serde(rename = "picture")]
214    pub picture_url: String,
215}