Skip to main content

ferric_fred/
source.rs

1use serde::{Deserialize, Serialize};
2
3use crate::SourceId;
4
5/// A FRED data source — the organization that produces releases (e.g. the
6/// Bureau of Economic Analysis), from the `fred/source` and `fred/sources`
7/// endpoints.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
10pub struct Source {
11    /// The source's identifier.
12    pub id: SourceId,
13
14    /// Human-readable name, e.g. `"U.S. Bureau of Economic Analysis"`.
15    pub name: String,
16
17    /// A link to the source's site, when FRED provides one.
18    #[serde(default)]
19    pub link: Option<String>,
20}
21
22/// A page of sources with pagination metadata, from the `fred/sources`
23/// endpoint.
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
25#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
26pub struct SourcesResults {
27    /// Total number of sources available (across all pages).
28    pub count: u32,
29
30    /// The offset (number of sources skipped) for this page.
31    pub offset: u32,
32
33    /// The page-size limit that was applied.
34    pub limit: u32,
35
36    /// The sources on this page.
37    pub sources: Vec<Source>,
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn deserializes_a_source_with_a_link() {
46        let source: Source = serde_json::from_str(
47            r#"{"id":18,"realtime_start":"2013-08-14","realtime_end":"2013-08-14",
48                "name":"U.S. Bureau of Economic Analysis","link":"http://www.bea.gov/"}"#,
49        )
50        .unwrap();
51        assert_eq!(source.id, SourceId::new(18));
52        assert_eq!(source.name, "U.S. Bureau of Economic Analysis");
53        assert_eq!(source.link.as_deref(), Some("http://www.bea.gov/"));
54    }
55
56    #[test]
57    fn source_without_link_defaults_to_none() {
58        let source: Source =
59            serde_json::from_str(r#"{"id":3,"name":"Federal Reserve Board"}"#).unwrap();
60        assert!(source.link.is_none());
61    }
62
63    #[test]
64    fn sources_results_carry_pagination() {
65        let results: SourcesResults = serde_json::from_str(
66            r#"{"count":2,"offset":0,"limit":1000,"sources":[
67                {"id":1,"name":"Board of Governors of the Federal Reserve System (US)"},
68                {"id":3,"name":"Federal Reserve Bank of Philadelphia"}
69            ]}"#,
70        )
71        .unwrap();
72        assert_eq!(results.count, 2);
73        assert_eq!(results.sources.len(), 2);
74        assert_eq!(results.sources[1].id, SourceId::new(3));
75    }
76}