use serde::{Deserialize, Serialize};
use crate::SourceId;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Source {
pub id: SourceId,
pub name: String,
#[serde(default)]
pub link: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct SourcesResults {
pub count: u32,
pub offset: u32,
pub limit: u32,
pub sources: Vec<Source>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserializes_a_source_with_a_link() {
let source: Source = serde_json::from_str(
r#"{"id":18,"realtime_start":"2013-08-14","realtime_end":"2013-08-14",
"name":"U.S. Bureau of Economic Analysis","link":"http://www.bea.gov/"}"#,
)
.unwrap();
assert_eq!(source.id, SourceId::new(18));
assert_eq!(source.name, "U.S. Bureau of Economic Analysis");
assert_eq!(source.link.as_deref(), Some("http://www.bea.gov/"));
}
#[test]
fn source_without_link_defaults_to_none() {
let source: Source =
serde_json::from_str(r#"{"id":3,"name":"Federal Reserve Board"}"#).unwrap();
assert!(source.link.is_none());
}
#[test]
fn sources_results_carry_pagination() {
let results: SourcesResults = serde_json::from_str(
r#"{"count":2,"offset":0,"limit":1000,"sources":[
{"id":1,"name":"Board of Governors of the Federal Reserve System (US)"},
{"id":3,"name":"Federal Reserve Bank of Philadelphia"}
]}"#,
)
.unwrap();
assert_eq!(results.count, 2);
assert_eq!(results.sources.len(), 2);
assert_eq!(results.sources[1].id, SourceId::new(3));
}
}