pexels_uri/
lib.rs

1//! Create URI's for Pexels API using.
2//!
3//! # API Documentation
4//! This is not an official crate from Pexels, their documentation can be found [here](https://www.pexels.com/api/documentation/)
5//!
6//! # Examples
7//!
8//! ```
9//! use pexels_uri::{videos, Orientation};
10//!
11//! fn main() -> Result<(), Box<dyn std::error::Error>> {
12//!     let uri_builder = videos::Search::builder()
13//!         .query("Dogs running")
14//!         .orientation(Orientation::Landscape)
15//!         .per_page(25)
16//!         .build();
17//!
18//!     assert_eq!(
19//!         "https://api.pexels.com/videos/search?query=Dogs+running&per_page=25&orientation=landscape",
20//!         uri_builder.create_uri()?
21//!     );
22//!     Ok(())
23//! }
24//!
25//! ```
26//!
27
28pub mod photos;
29pub mod videos;
30
31const PEXEL_SCHEME: &str = "https";
32const PEXEL_HOST: &str = "api.pexels.com";
33const PEXEL_VERSION: &str = "v1";
34
35/// Desired photo orientation.
36pub enum Orientation {
37    Landscape,
38    Portrait,
39    Square,
40}
41
42impl Orientation {
43    fn as_str(&self) -> &str {
44        match self {
45            Orientation::Landscape => "landscape",
46            Orientation::Portrait => "portrait",
47            Orientation::Square => "square",
48        }
49    }
50}
51
52/// The locale of the search you are performing.
53#[allow(non_camel_case_types)]
54pub enum Locale {
55    en_US,
56    pt_BR,
57    es_ES,
58    ca_ES,
59    de_DE,
60    it_IT,
61    fr_FR,
62    sv_SE,
63    id_ID,
64    pl_PL,
65    ja_JP,
66    zh_TW,
67    zh_CN,
68    ko_KR,
69    th_TH,
70    nl_NL,
71    hu_HU,
72    vi_VN,
73    cs_CZ,
74    da_DK,
75    fi_FI,
76    uk_UA,
77    el_GR,
78    ro_RO,
79    nb_NO,
80    sk_SK,
81    tr_TR,
82    ru_RU,
83}
84
85impl Locale {
86    fn as_str(&self) -> &str {
87        match self {
88            Locale::en_US => "en-US",
89            Locale::pt_BR => "pt-BR",
90            Locale::es_ES => "es-ES",
91            Locale::ca_ES => "ca-ES",
92            Locale::de_DE => "de-DE",
93            Locale::it_IT => "it-IT",
94            Locale::fr_FR => "fr-FR",
95            Locale::sv_SE => "sv-SE",
96            Locale::id_ID => "id-ID",
97            Locale::pl_PL => "pl-PL",
98            Locale::ja_JP => "ja-JP",
99            Locale::zh_TW => "zh-TW",
100            Locale::zh_CN => "zh-CN",
101            Locale::ko_KR => "ko-KR",
102            Locale::th_TH => "th-TH",
103            Locale::nl_NL => "nl-NL",
104            Locale::hu_HU => "hu-HU",
105            Locale::vi_VN => "vi-VN",
106            Locale::cs_CZ => "cs-CZ",
107            Locale::da_DK => "da-DK",
108            Locale::fi_FI => "fi-FI",
109            Locale::uk_UA => "uk-UA",
110            Locale::el_GR => "el-GR",
111            Locale::ro_RO => "ro-RO",
112            Locale::nb_NO => "nb-NO",
113            Locale::sk_SK => "sk-SK",
114            Locale::tr_TR => "tr-TR",
115            Locale::ru_RU => "-ES",
116        }
117    }
118}
119
120/// Minimum video/photo size.
121pub enum Size {
122    Large,
123    Medium,
124    Small,
125}
126
127impl Size {
128    fn as_str(&self) -> &str {
129        match self {
130            Size::Large => "large",
131            Size::Medium => "medium",
132            Size::Small => "small",
133        }
134    }
135}
136use std::fmt;
137
138pub(crate) type BuilderResult = Result<String, PexelsError>;
139
140/// Errors that can occurr while creating Pexels URI.
141#[derive(Debug, Clone, PartialEq)]
142pub enum PexelsError {
143    ParseError(url::ParseError),
144    HexColorCodeError(String),
145}
146
147impl fmt::Display for PexelsError {
148    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
149        write!(f, "")
150    }
151}
152
153impl std::error::Error for PexelsError {}
154
155impl From<url::ParseError> for PexelsError {
156    fn from(err: url::ParseError) -> PexelsError {
157        PexelsError::ParseError(err)
158    }
159}