fred_rs/category/mod.rs
1//! Get a category
2//!
3//! [https://research.stlouisfed.org/docs/api/fred/category.html](https://research.stlouisfed.org/docs/api/fred/category.html)
4//!
5//! ```
6//! use fred_rs::client::FredClient;
7//! use fred_rs::category::Response;
8//!
9//! let mut c = match FredClient::new() {
10//! Ok(c) => c,
11//! Err(msg) => {
12//! println!("{}", msg);
13//! assert_eq!(2, 1);
14//! return
15//! },
16//! };
17//!
18//! let resp: Response = match c.category(125) {
19//! Ok(resp) => resp,
20//! Err(msg) => {
21//! println!("{}", msg);
22//! assert_eq!(2, 1);
23//! return
24//! },
25//! };
26//!
27//! for s in resp.categories {
28//! println!("ID: {} Name: {} ParentID: {}", s.id, s.name, s.parent_id);
29//! }
30//! ```
31
32pub mod children;
33pub mod related;
34pub mod series;
35pub mod tags;
36pub mod related_tags;
37
38// -----------------------------------------------------------------------------
39
40use serde::Deserialize;
41use std::fmt::{self, Display, Formatter};
42
43#[derive(Deserialize, Clone, Debug, Default)]
44/// Response data structure for a collection of categories
45///
46/// [https://research.stlouisfed.org/docs/api/fred/category.html] (https://research.stlouisfed.org/docs/api/fred/category.html)
47pub struct Response {
48 /// List of categories returned by the query
49 pub categories: Vec<Category>,
50}
51
52impl Display for Response {
53 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
54 for item in self.categories.iter() {
55 match item.fmt(f) {
56 Ok(_) => (),
57 Err(e) => return Err(e),
58 }
59 match writeln!(f, "") {
60 Ok(_) => (),
61 Err(e) => return Err(e),
62 }
63 }
64 Ok(())
65 }
66}
67
68#[derive(Deserialize, Clone, Debug, Default)]
69/// Data structure containing infomation about a particular category
70///
71/// [https://research.stlouisfed.org/docs/api/fred/category.html](https://research.stlouisfed.org/docs/api/fred/category.html)
72pub struct Category {
73 /// The category ID number
74 pub id: usize,
75 /// The category name
76 pub name: String,
77 /// The parent ID number of the category
78 pub parent_id: usize,
79 /// Additional information about the category
80 pub notes: Option<String>,
81}
82
83impl Display for Category {
84 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
85 write!(f, "Category {}: {}", self.id, self.name)
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92 use crate::client::FredClient;
93
94 #[test]
95 fn category_no_options() {
96 let mut c = match FredClient::new() {
97 Ok(c) => c,
98 Err(msg) => {
99 println!("{}", msg);
100 assert_eq!(2, 1);
101 return
102 },
103 };
104
105 let resp: Response = match c.category(125) {
106 Ok(resp) => resp,
107 Err(msg) => {
108 println!("{}", msg);
109 assert_eq!(2, 1);
110 return
111 },
112 };
113
114 for s in resp.categories {
115 println!("ID: {} Name: {} ParentID: {}", s.id, s.name, s.parent_id);
116 }
117 }
118}