pexels-api 0.1.0

A Rust client for the Pexels API. API Address: https://www.pexels.com/api/documentation/
Documentation
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use crate::{MediaSort, MediaType, Orientation, Size};

#[derive(Debug, Clone, Default)]
pub struct SearchParams {
    pub page: Option<u32>,
    pub per_page: Option<u32>,
    pub orientation: Option<Orientation>,
    pub size: Option<Size>,
    pub color: Option<String>,
    pub locale: Option<String>,
}

impl SearchParams {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn page(mut self, page: u32) -> Self {
        self.page = Some(page);
        self
    }

    pub fn per_page(mut self, per_page: u32) -> Self {
        self.per_page = Some(per_page);
        self
    }

    pub fn orientation(mut self, orientation: Orientation) -> Self {
        self.orientation = Some(orientation);
        self
    }

    pub fn size(mut self, size: Size) -> Self {
        self.size = Some(size);
        self
    }

    pub fn color(mut self, color: impl Into<String>) -> Self {
        self.color = Some(color.into());
        self
    }

    pub fn locale(mut self, locale: impl Into<String>) -> Self {
        self.locale = Some(locale.into());
        self
    }

    pub fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();

        if let Some(page) = self.page {
            params.push(("page".to_string(), page.to_string()));
        }

        if let Some(per_page) = self.per_page {
            params.push(("per_page".to_string(), per_page.to_string()));
        }

        if let Some(orientation) = &self.orientation {
            params.push(("orientation".to_string(), orientation.to_string()));
        }

        if let Some(size) = &self.size {
            params.push(("size".to_string(), size.to_string()));
        }

        if let Some(color) = &self.color {
            params.push(("color".to_string(), color.clone()));
        }

        if let Some(locale) = &self.locale {
            params.push(("locale".to_string(), locale.clone()));
        }

        params
    }
}

// Pagination parameters for API requests
#[derive(Debug, Clone, Default)]
pub struct PaginationParams {
    /// Page number to retrieve
    pub page: Option<u32>,

    /// Number of items per page
    pub per_page: Option<u32>,
}

impl PaginationParams {
    /// Create a new PaginationParams instance
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the page number
    pub fn page(mut self, page: u32) -> Self {
        self.page = Some(page);
        self
    }

    /// Set the number of items per page
    pub fn per_page(mut self, per_page: u32) -> Self {
        self.per_page = Some(per_page);
        self
    }

    pub(crate) fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();

        if let Some(page) = self.page {
            params.push(("page".to_string(), page.to_string()));
        }

        if let Some(per_page) = self.per_page {
            params.push(("per_page".to_string(), per_page.to_string()));
        }

        params
    }
}

/// Video search parameters
#[derive(Debug, Clone, Default)]
pub struct VideoSearchParams {
    /// Page number to retrieve
    pub page: Option<u32>,

    /// Number of videos per page
    pub per_page: Option<u32>,

    /// Orientation filter (landscape, portrait, square)
    pub orientation: Option<String>,

    /// Size filter (large, medium, small)
    pub size: Option<String>,

    /// Locale for localized results
    pub locale: Option<String>,
}

impl VideoSearchParams {
    /// Create a new VideoSearchParams instance
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the page number
    pub fn page(mut self, page: u32) -> Self {
        self.page = Some(page);
        self
    }

    /// Set the number of items per page
    pub fn per_page(mut self, per_page: u32) -> Self {
        self.per_page = Some(per_page);
        self
    }

    /// Set the orientation filter
    pub fn orientation(mut self, orientation: impl Into<String>) -> Self {
        self.orientation = Some(orientation.into());
        self
    }

    /// Set the size filter
    pub fn size(mut self, size: impl Into<String>) -> Self {
        self.size = Some(size.into());
        self
    }

    /// Set the locale for localized results
    pub fn locale(mut self, locale: impl Into<String>) -> Self {
        self.locale = Some(locale.into());
        self
    }

    pub(crate) fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();

        if let Some(page) = self.page {
            params.push(("page".to_string(), page.to_string()));
        }

        if let Some(per_page) = self.per_page {
            params.push(("per_page".to_string(), per_page.to_string()));
        }

        if let Some(orientation) = &self.orientation {
            params.push(("orientation".to_string(), orientation.clone()));
        }

        if let Some(size) = &self.size {
            params.push(("size".to_string(), size.clone()));
        }

        if let Some(locale) = &self.locale {
            params.push(("locale".to_string(), locale.clone()));
        }

        params
    }
}

/// Popular video parameters.
#[derive(Debug, Clone, Default)]
pub struct PopularVideoParams {
    /// Page number to retrieve
    pub page: Option<u32>,

    /// Number of videos per page
    pub per_page: Option<u32>,

    /// Minimum video width in pixels
    pub min_width: Option<u32>,

    /// Minimum video height in pixels
    pub min_height: Option<u32>,

    /// Minimum video duration in seconds
    pub min_duration: Option<u32>,

    /// Maximum video duration in seconds
    pub max_duration: Option<u32>,
}

impl PopularVideoParams {
    /// Create a new PopularVideoParams instance
    pub fn new() -> Self {
        Self::default()
    }

    /// Create popular video params from simple pagination params.
    pub fn from_pagination(params: &PaginationParams) -> Self {
        Self { page: params.page, per_page: params.per_page, ..Self::default() }
    }

    /// Set the page number
    pub fn page(mut self, page: u32) -> Self {
        self.page = Some(page);
        self
    }

    /// Set the number of items per page
    pub fn per_page(mut self, per_page: u32) -> Self {
        self.per_page = Some(per_page);
        self
    }

    /// Set the minimum video width in pixels
    pub fn min_width(mut self, min_width: u32) -> Self {
        self.min_width = Some(min_width);
        self
    }

    /// Set the minimum video height in pixels
    pub fn min_height(mut self, min_height: u32) -> Self {
        self.min_height = Some(min_height);
        self
    }

    /// Set the minimum video duration in seconds
    pub fn min_duration(mut self, min_duration: u32) -> Self {
        self.min_duration = Some(min_duration);
        self
    }

    /// Set the maximum video duration in seconds
    pub fn max_duration(mut self, max_duration: u32) -> Self {
        self.max_duration = Some(max_duration);
        self
    }

    pub(crate) fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();

        if let Some(page) = self.page {
            params.push(("page".to_string(), page.to_string()));
        }

        if let Some(per_page) = self.per_page {
            params.push(("per_page".to_string(), per_page.to_string()));
        }

        if let Some(min_width) = self.min_width {
            params.push(("min_width".to_string(), min_width.to_string()));
        }

        if let Some(min_height) = self.min_height {
            params.push(("min_height".to_string(), min_height.to_string()));
        }

        if let Some(min_duration) = self.min_duration {
            params.push(("min_duration".to_string(), min_duration.to_string()));
        }

        if let Some(max_duration) = self.max_duration {
            params.push(("max_duration".to_string(), max_duration.to_string()));
        }

        params
    }
}

/// Collection media parameters.
#[derive(Debug, Clone, Default)]
pub struct CollectionMediaParams {
    /// Page number to retrieve
    pub page: Option<u32>,

    /// Number of media items per page
    pub per_page: Option<u32>,

    /// Media type filter
    pub media_type: Option<MediaType>,

    /// Sort order
    pub sort: Option<MediaSort>,
}

impl CollectionMediaParams {
    /// Create a new CollectionMediaParams instance
    pub fn new() -> Self {
        Self::default()
    }

    /// Create collection media params from simple pagination params.
    pub fn from_pagination(params: &PaginationParams) -> Self {
        Self { page: params.page, per_page: params.per_page, ..Self::default() }
    }

    /// Set the page number
    pub fn page(mut self, page: u32) -> Self {
        self.page = Some(page);
        self
    }

    /// Set the number of items per page
    pub fn per_page(mut self, per_page: u32) -> Self {
        self.per_page = Some(per_page);
        self
    }

    /// Filter collection media to photos or videos.
    pub fn media_type(mut self, media_type: MediaType) -> Self {
        self.media_type = Some(media_type);
        self
    }

    /// Set collection media sort order.
    pub fn sort(mut self, sort: MediaSort) -> Self {
        self.sort = Some(sort);
        self
    }

    pub(crate) fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();

        if let Some(page) = self.page {
            params.push(("page".to_string(), page.to_string()));
        }

        if let Some(per_page) = self.per_page {
            params.push(("per_page".to_string(), per_page.to_string()));
        }

        if let Some(media_type) = &self.media_type {
            if !matches!(media_type, MediaType::Empty) {
                params.push(("type".to_string(), media_type.as_str().to_string()));
            }
        }

        if let Some(sort) = &self.sort {
            params.push(("sort".to_string(), sort.as_str().to_string()));
        }

        params
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn popular_video_params_include_documented_filters() {
        let params = PopularVideoParams::new()
            .page(2)
            .per_page(20)
            .min_width(1280)
            .min_height(720)
            .min_duration(5)
            .max_duration(60)
            .to_query_params();

        assert_eq!(
            params,
            vec![
                ("page".to_string(), "2".to_string()),
                ("per_page".to_string(), "20".to_string()),
                ("min_width".to_string(), "1280".to_string()),
                ("min_height".to_string(), "720".to_string()),
                ("min_duration".to_string(), "5".to_string()),
                ("max_duration".to_string(), "60".to_string()),
            ]
        );
    }

    #[test]
    fn collection_media_params_include_type_and_sort() {
        let params = CollectionMediaParams::new()
            .page(1)
            .per_page(10)
            .media_type(MediaType::Photo)
            .sort(MediaSort::Desc)
            .to_query_params();

        assert_eq!(
            params,
            vec![
                ("page".to_string(), "1".to_string()),
                ("per_page".to_string(), "10".to_string()),
                ("type".to_string(), "photos".to_string()),
                ("sort".to_string(), "desc".to_string()),
            ]
        );
    }
}