freddo/
base.rs

1use crate::client::FreddoClient;
2
3pub trait QueryTraits {
4    fn build_query_param_str(&self) -> Result<String, String>;
5
6    fn execute(&self, client: &FreddoClient) -> Result<Box<dyn ResultTrait>, String>;
7}
8
9pub trait ResultTrait {
10
11    fn print_value(&self);
12
13    fn write_to_file(&self, output_path: String);
14
15}
16
17pub fn check_units(unit: String) -> Result<String, String> {
18    let allowed_units: Vec<&str> = vec!["lin", "chg", "ch1",
19        "pch", "pc1", "pca", 
20        "cch", "cca", "log"];
21
22    if allowed_units.contains(&unit.to_lowercase().as_str()) {
23        Ok(unit.to_lowercase())
24    } else {
25        Err("Invalid unit specified!".to_owned())
26    }
27
28}
29
30pub fn check_frequency(freq: String) -> Result<String, String> {
31
32    let allowed_freqs: Vec<&str> = vec!["d", "w", "bw", "m", "q", 
33        "sa", "a", "wef", "weth", 
34        "wew", "wetu", "wem", "wesu", 
35        "wesa", "bwew", "bwem"];
36
37    if allowed_freqs.contains(&freq.to_lowercase().as_str()) {
38        Ok(freq.to_lowercase())
39    } else {
40        Err("Invalid frequency specified!".to_owned())
41    }
42
43}
44
45pub fn check_agg_mtd(method: String) -> Result<String, String> {
46
47    let allowed_methods: Vec<&str> = vec!["avg", "sum", "eop"];
48
49    if allowed_methods.contains(&method.to_lowercase().as_str()) {
50        Ok(method.to_lowercase())
51    } else {
52        Err("Invalid method specified!".to_owned())
53    }
54}
55
56pub fn check_output_type(output_type: usize) -> Result<usize, String> {
57
58    let allowed_types: Vec<usize> = vec![1, 2, 3, 4];
59
60    if allowed_types.contains(&output_type) {
61        Ok(output_type)
62    } else {
63        Err("Invalid output type specified!".to_owned())
64    }
65}
66
67pub fn check_search_type(search_type: String) -> Result<String, String> {
68
69    let allowed_types: Vec<&str> = vec!["full_text", "series_id"];
70
71    if allowed_types.contains(&search_type.to_lowercase().as_str()) {
72        Ok(search_type.to_lowercase())
73    } else {
74        Err("Invalid search type specified!".to_owned())
75    }
76}
77
78pub fn check_order_by(order_by: String) -> Result<String, String> {
79
80    let allowed_order: Vec<&str> = vec!["search_rank", "series_id", "title", "units", 
81        "frequency", "seasonal_adjustment", "realtime_start", "realtime_end", 
82        "last_updated", "observation_start", "observation_end", "popularity", 
83        "group_popularity"];
84
85    if allowed_order.contains(&order_by.to_lowercase().as_str()) {
86        Ok(order_by.to_lowercase())
87    } else {
88        Err("Invalid order specified!".to_owned())
89    }
90}
91
92pub fn check_sort_order(sort_order: String) -> Result<String, String> {
93
94    let allowed_sort: Vec<&str> = vec!["asc", "desc"];
95
96    if allowed_sort.contains(&sort_order.to_lowercase().as_str()) {
97        Ok(sort_order.to_lowercase())
98    } else {
99        Err("Invalid sort order specified!".to_owned())
100    }
101}
102
103pub fn check_filter_variable(filter_var: String) -> Result<String, String> {
104    
105    let allowed_filter: Vec<&str> = vec!["frequency", "units", "seasonal_adjustment"];
106
107    if allowed_filter.contains(&filter_var.to_lowercase().as_str()) {
108        Ok(filter_var.to_lowercase())
109    } else {
110        Err("Invalid filter variable specified!".to_owned())
111    }
112}
113
114#[cfg(test)]
115mod base_tests {
116
117    use super::*;
118
119    fn incorrect_param<T>(f: fn(T) -> Result<T, String>, val:T) {
120        assert!(f(val).is_err())
121    }
122
123    #[test]
124    fn incorrect_units() {
125        incorrect_param(check_units, "abs".to_string())
126    }
127
128    #[test]
129    fn incorrect_freqs() {
130        incorrect_param(check_frequency, "hahaha".to_string())
131    }
132
133    #[test]
134    fn incorrect_agg_mtd() {
135        incorrect_param(check_agg_mtd, "nonono".to_string())
136    }
137
138    #[test]
139    fn incorrect_output_type() {
140        incorrect_param(check_output_type, 6)
141    }
142
143    #[test]
144    fn incorrect_search_type() {
145        incorrect_param(check_search_type, "i don't Know".to_string())
146    }
147
148    #[test]
149    fn incorrect_order(){
150        incorrect_param(check_order_by, "no order".to_string())
151    }
152
153    #[test]
154    fn incorrect_sort(){
155        incorrect_param(check_sort_order, "steady".to_string())
156    }
157
158    #[test]
159    fn incorrect_filter_var() {
160        incorrect_param(check_filter_variable, "today".to_string())
161    }
162}