Skip to main content

gapirs_common/utils/
converters.rs

1pub trait ConvertToQueryParams {
2    fn to_google_param_str(&self) -> String;
3}
4
5/// Generates an implementation that just does to_string() on the value.
6///
7/// It was not automatically implemented for anything that implements Display or ToString
8/// because something other than what is defined here is probably a bug and might lead to some
9/// other Problems, so I tried to catch these early.
10macro_rules! impl_convert_to_query_params_with_to_string {
11    (for $($t:ty),+) => {
12        $(
13        impl ConvertToQueryParams for $t {
14            fn to_google_param_str(&self) -> String {
15                self.to_string()
16            }
17        }
18        impl ConvertToQueryParams for & $t {
19            fn to_google_param_str(&self) -> String {
20                self.to_string()
21            }
22        }
23        )*
24    }
25}
26impl_convert_to_query_params_with_to_string!(for
27    bool,
28    u8, u16, u32, u64,
29    i8, i16, i32, i64,
30    f32, f64,
31    str, String
32);
33#[cfg(test)]
34mod test {
35    use super::ConvertToQueryParams;
36
37    #[test]
38    fn hi() {
39        let x = 4u32.to_google_param_str();
40        assert_eq!("4", x);
41    }
42}