pexels_api/domain/
models.rs

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use serde::{Deserialize, Serialize};

/// Represents the response for a list of collections.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CollectionsResponse {
    pub collections: Vec<Collection>,
    pub page: u32,
    pub per_page: u32,
    pub total_results: u32,
    pub next_page: Option<String>,
    pub prev_page: Option<String>,
}

/// Represents a Pexels collection.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Collection {
    pub id: String,
    pub title: String,
    pub description: Option<String>,
    pub private: bool,
    pub media_count: u32,
    pub photos_count: u32,
    pub videos_count: u32,
}

/// Represents the response for a list of media items.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MediaResponse {
    pub id: String,
    pub media: Vec<MediaType>, // An array of media objects. Each object has an extra type attribute to indicate the type of object.
    pub page: u32,
    pub per_page: u32,
    pub total_results: u32,
    pub next_page: Option<String>,
    pub prev_page: Option<String>,
}

/// Enum representing the type of media.
/// Supported values are `photos` and `videos`.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum MediaType {
    Photo(MediaPhoto),
    Video(MediaVideo),
}

// Manual implementation From<MediaType> to fill type_ field in MediaPhoto and MediaVideo
impl From<MediaType> for MediaPhoto {
    fn from(media: MediaType) -> Self {
        match media {
            MediaType::Photo(photo) => MediaPhoto { type_: "Photo".to_string(), ..photo },
            _ => panic!("Expected Photo"),
        }
    }
}

impl From<MediaType> for MediaVideo {
    fn from(media: MediaType) -> Self {
        match media {
            MediaType::Video(video) => MediaVideo { type_: "Video".to_string(), ..video },
            _ => panic!("Expected Video"),
        }
    }
}

/// Represents a photo media object.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MediaPhoto {
    #[serde(skip)]
    pub type_: String,
    pub id: u32,
    pub width: u32,
    pub height: u32,
    pub url: Option<String>,
    pub photographer: Option<String>,
    pub photographer_url: Option<String>,
    pub photographer_id: u32,
    pub avg_color: String,
    pub src: PhotoSrc,
    pub liked: bool,
    pub alt: String,
}

/// Represents a video media object.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MediaVideo {
    #[serde(skip)]
    pub type_: String,
    pub id: u32,
    pub width: u32,
    pub height: u32,
    pub duration: u32,
    pub full_res: Option<String>,
    pub tags: Vec<String>,
    pub url: Option<String>,
    pub image: Option<String>,
    pub avg_color: Option<String>,
    pub user: User,
    pub video_files: Vec<VideoFile>,
    pub video_pictures: Vec<VideoPicture>,
}

/// Represents a Pexels photo.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Photo {
    pub id: u32,
    pub width: u32,
    pub height: u32,
    pub url: String,
    pub photographer: String,
    pub photographer_url: String,
    pub photographer_id: u32,
    pub avg_color: String,
    pub src: PhotoSrc,
    pub liked: bool,
    pub alt: String,
}

/// Represents different image sizes for a photo.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PhotoSrc {
    pub original: String,
    pub large2x: String,
    pub large: String,
    pub medium: String,
    pub small: String,
    pub portrait: String,
    pub landscape: String,
    pub tiny: String,
}

/// Represents the response for a list of photos.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PhotosResponse {
    pub total_results: u32,
    pub page: u32,
    pub per_page: u32,
    pub photos: Vec<Photo>,
    pub next_page: Option<String>,
    pub prev_page: Option<String>,
}

/// Represents the response for a list of videos.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VideoResponse {
    pub page: u32,
    pub per_page: u32,
    pub total_results: u32,
    pub url: String,
    pub videos: Vec<Video>,
    pub prev_page: Option<String>,
    pub next_page: Option<String>,
}

/// Represents a Pexels video.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Video {
    #[serde(default)]
    pub avg_color: Option<String>,
    pub duration: u32,
    #[serde(default)]
    pub full_res: Option<String>,
    pub height: u32,
    pub id: u32,
    #[serde(rename = "image")]
    pub image_url: String,
    pub tags: Vec<String>,
    #[serde(rename = "url")]
    pub video_url: String,
    pub user: User,
    pub video_files: Vec<VideoFile>,
    pub video_pictures: Vec<VideoPicture>,
    pub width: u32,
}

/// Represents a user who created a media item.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User {
    pub id: u32,
    pub name: String,
    #[serde(rename = "url")]
    pub user_url: String,
}

/// Represents a video file with different qualities.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VideoFile {
    pub file_type: String,
    pub fps: f64,
    pub height: u32,
    pub id: u32,
    #[serde(rename = "link")]
    pub file_link: String,
    #[serde(default)]
    pub quality: Option<String>,
    pub size: u64,
    pub width: u32,
}

/// Represents a preview picture of a video.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VideoPicture {
    pub id: u32,
    pub nr: u32,
    #[serde(rename = "picture")]
    pub picture_url: String,
}