1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
//! Get a source of economic data //! //! [https://research.stlouisfed.org/docs/api/fred/source.html](https://research.stlouisfed.org/docs/api/fred/source.html) //! //! ``` //! use fred_rs::client::FredClient; //! use fred_rs::source::Response; //! //! let mut c = match FredClient::new() { //! Ok(c) => c, //! Err(msg) => { //! println!("{}", msg); //! assert_eq!(2, 1); //! return //! }, //! }; //! //! let resp: Response = match c.source(1, None) { //! Ok(resp) => resp, //! Err(msg) => { //! println!("{}", msg); //! assert_eq!(2, 1); //! return //! }, //! }; //! //! for item in resp.sources { //! match item.link { //! Some(l) => println!("{}: {}", item.name, l), //! None => println!("{}: null", item.name), //! } //! } //! ``` pub mod releases; // ----------------------------------------------------------------------------- use serde::Deserialize; use std::fmt::{self, Display, Formatter}; #[derive(Deserialize, Clone, Debug, Default)] /// Response data structure for the fred/source endpoint /// /// [https://research.stlouisfed.org/docs/api/fred/source.html] (https://research.stlouisfed.org/docs/api/fred/source.html) pub struct Response { /// The Real Time start date for the request pub realtime_start: String, /// The Real Time end data for the request pub realtime_end: String, /// How the results are ordered pub order_by: Option<String>, // Results are listed in ascending or descending pub sort_order: Option<String>, /// Number of results returned pub count: Option<usize>, /// ??? pub offset: Option<usize>, /// Maximum number of results to return pub limit: Option<usize>, /// Series returned by the search pub sources: Vec<Source>, } impl Display for Response { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { for item in self.sources.iter() { match item.fmt(f) { Ok(_) => (), Err(e) => return Err(e), } match writeln!(f, "") { Ok(_) => (), Err(e) => return Err(e), } } Ok(()) } } #[derive(Deserialize, Clone, Debug, Default)] /// Data structure containing infomation about a particular tag /// /// [https://research.stlouisfed.org/docs/api/fred/source.html](https://research.stlouisfed.org/docs/api/fred/source.html) pub struct Source { /// The source ID pub id: usize, /// The Real Time start date for the request pub realtime_start: String, /// The Real Time end data for the request pub realtime_end: String, /// The source name pub name: String, /// A link to the source's website pub link: Option<String>, /// Additional notes about the source pub notes: Option<String> } impl Display for Source { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "Soruce {}: {}", self.id, self.name) } } pub struct Builder { option_string: String, } impl Builder { /// Initializes a new sources::Builder that can be used to add commands to an API request /// /// The builder does not do validity checking of the arguments nor does it check for duplicates. /// /// ``` /// use fred_rs::sources::Builder; /// // Create a new builder /// let mut builder = Builder::new(); /// // add arguments to the builder /// builder /// .realtime_start("1900-01-01") /// .realtime_end("2000-01-01"); /// ``` pub fn new() -> Builder { Builder { option_string: String::new(), } } /// Returns the current arguments as a URL formatted string pub(crate) fn build(self) -> String { self.option_string } /// Adds a realtime_start argument to the builder /// /// # Arguments /// * `start_date` - date formatted as YYYY-MM-DD /// /// [https://research.stlouisfed.org/docs/api/fred/source.html#realtime_start](https://research.stlouisfed.org/docs/api/fred/source.html#realtime_start) pub fn realtime_start(&mut self, start_date: &str) -> &mut Builder { self.option_string += format!("&realtime_start={}", start_date).as_str(); self } /// Adds a realtime_end argument to the builder /// /// # Arguments /// * `end_date` - date formatted as YYYY-MM-DD /// /// [https://research.stlouisfed.org/docs/api/fred/source.html#realtime_end](https://research.stlouisfed.org/docs/api/fred/source.html#realtime_end) pub fn realtime_end(&mut self, end_date: &str) -> &mut Builder { self.option_string += format!("&realtime_end={}", end_date).as_str(); self } } #[cfg(test)] mod tests { use super::*; use crate::client::FredClient; #[test] fn source_no_options() { let mut c = match FredClient::new() { Ok(c) => c, Err(msg) => { println!("{}", msg); assert_eq!(2, 1); return }, }; let resp: Response = match c.source(1, None) { Ok(resp) => resp, Err(msg) => { println!("{}", msg); assert_eq!(2, 1); return }, }; for s in resp.sources { match s.link { Some(l) => println!("{}: {}", s.name, l), None => println!("{}", s.name) } } } }