Skip to main content

tmdb_rs/
param.rs

1use crate::{CountryCode, Date, GuestSessionId, Language, SessionId};
2
3/// a query param value; slices join with commas, as TMDB expects
4pub trait ToParam {
5    fn to_param(&self) -> String;
6}
7
8macro_rules! to_param {
9    ($($t:ty),* $(,)?) => {$(
10        impl ToParam for $t {
11            fn to_param(&self) -> String {
12                self.to_string()
13            }
14        }
15    )*};
16}
17
18to_param!(
19    u32,
20    u64,
21    i32,
22    i64,
23    f64,
24    bool,
25    &str,
26    String,
27    Date,
28    SessionId,
29    GuestSessionId,
30    crate::endpoints::account::AccountSort,
31    crate::endpoints::discover::MovieSort,
32    crate::endpoints::find::ExternalSource,
33    crate::endpoints::trending::TimeWindow,
34    crate::endpoints::discover::TvSort,
35);
36
37impl ToParam for Language {
38    fn to_param(&self) -> String {
39        // TMDB wants 639-1; a few languages only have a 639-3 code
40        self.to_639_1().unwrap_or_else(|| self.to_639_3()).into()
41    }
42}
43
44impl ToParam for CountryCode {
45    fn to_param(&self) -> String {
46        self.alpha2().into()
47    }
48}
49
50impl<T: ToParam> ToParam for &[T] {
51    fn to_param(&self) -> String {
52        self.iter()
53            .map(ToParam::to_param)
54            .collect::<Vec<_>>()
55            .join(",")
56    }
57}
58
59impl<T: ToParam> ToParam for Vec<T> {
60    fn to_param(&self) -> String {
61        self.as_slice().to_param()
62    }
63}