//! Builds generic specifications.
use std::convert::TryInto;
use std::fs;
use countries::Country;
use fred_api::{
Fred,
SeriesItems,
};
use keytree::{
KeyTree,
KeyTreeRef,
};
use keytree::Error;
use keytree::serialize::{
IntoKeyTree,
KeyTreeString,
};
use crate::DataType;
/// Return all the countries with good data as a `Vec`.
pub fn countries_with_data() -> Vec<Country> {
vec!(
Country::Australia,
Country::Austria,
Country::Belgium,
Country::Canada,
Country::Chile,
Country::CzechRepublic,
Country::Denmark,
Country::Estonia,
Country::Finland,
Country::France,
Country::Germany,
Country::Greece,
Country::Ireland,
Country::Israel,
Country::Italy,
Country::Japan,
Country::Latvia,
Country::Netherlands,
Country::NewZealand,
Country::Norway,
Country::Poland,
Country::Serbia,
Country::SouthKorea,
Country::Spain,
Country::Sweden,
Country::Switzerland,
Country::UnitedKingdom,
Country::UnitedStates,
)
}
/// Returns relevant CPI series for a country, by pre-selecting series titles for each country.
///
/// ```
/// println!("{}", generic_cpi_series_spec(&Country::NewZealand, "cpi_series.keytree"));
/// ```
pub fn generic_cpi_series_spec(country: &Country, path: &str) -> SeriesItems {
let titles = match country {
Country::Australia => {
vec!(
"Consumer Price Index: All items: Total: Total for Australia",
"Consumer Price Index: All Items for Australia",
"Consumer Price Index in Australia (DISCONTINUED)",
"Consumer Price Index: Total All Items for Australia",
"Consumer Price Index for Australia",
"Inflation, consumer prices for Australia",
)
}
Country::Austria => {
vec!(
"Consumer Price Index for Austria",
"Consumer Price Index: All items: Total: Total for Austria",
"Consumer Price Index: All Items for Austria",
"Consumer Price Index in Austria (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for Austria",
"Consumer Price Index: Harmonized Prices: Total All Items for Austria // (DISCONTINUED)",
"Consumer Price Index: Total All Items for Austria",
"Harmonized Index of Consumer Prices: All Items for Austria",
"Harmonized Index of Consumer Prices in Austria (DISCONTINUED)",
"Inflation, consumer prices for Austria",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for // Austria",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Austria (DISCONTINUED)",
)
}
Country::Belgium => {
vec!(
"Consumer Price Index for Belgium",
"Consumer Price Index: Harmonized Prices: Total All Items for Belgium",
"Consumer Price Index: Harmonized Prices: Total All Items for Belgium (DISCONTINUED)",
"Consumer Price Index: Total All Items for Belgium",
"Harmonized Index of Consumer Prices: All Items for Belgium",
"Harmonized Index of Consumer Prices in Belgium (DISCONTINUED)",
"Consumer Price Index: All Items for Belgium",
"Consumer Price Index in Belgium (DISCONTINUED)",
"Consumer Price Index: All items: Total: Total for Belgium",
)
}
Country::Canada => {
vec!(
"Inflation, consumer prices for Canada",
"Consumer Price Index for Canada",
"Consumer Price Index: Total All Items for Canada",
"Consumer Price Index in Canada (DISCONTINUED)",
"Consumer Price Index of All Items in Canada",
"Consumer Price Index: All items: Total: Total for Canada",
)
}
Country::Chile => {
vec!(
"Consumer Price Index for Chile",
"Inflation, consumer prices for Chile",
"Consumer Price Index: Total All Items for Chile",
"Consumer Price Index: All Items for Chile",
"Consumer Price Index: All items: Total: Total for Chile",
)
}
Country::CzechRepublic => {
vec!(
"Consumer Price Index for Czech Republic",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Czech Republic",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Czech Republic (DISCONTINUED)",
"Consumer Price Index: All Items for Czech Republic",
"Consumer Price Index: All items: Total: Total for the Czech Republic",
"Consumer Price Index: Harmonized Prices: Total All Items for the Czech Republic",
"Consumer Price Index: Harmonized Prices: Total All Items for the Czech Republic (DISCONTINUED)",
"Consumer Price Index: Total All Items for the Czech Republic",
"Harmonized Index of Consumer Prices: All Items for Czech Republic",
)
}
Country::Denmark => {
vec!(
"Inflation, consumer prices for Denmark",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Denmark",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Denmark (DISCONTINUED)",
"Harmonized Index of Consumer Prices in Denmark (DISCONTINUED)",
"Consumer Price Index: All Items for Denmark",
"Consumer Price Index in Denmark (DISCONTINUED)",
"Consumer Price Index: All items: Total: Total for Denmark",
"Consumer Price Index for Denmark",
"Consumer Price Index: Total All Items for Denmark",
"Harmonized Index of Consumer Prices: All Items for Denmark",
)
}
Country::Estonia => {
vec!(
"Inflation, consumer prices for Estonia",
"Consumer Price Index: All items: Total: Total for Estonia",
"Consumer Price Index for Estonia",
"Consumer Price Index: Harmonized Prices: Total All Items for Estonia",
"Consumer Price Index: Harmonized Prices: Total All Items for Estonia (DISCONTINUED)",
"Consumer Price Index: Total All Items for Estonia",
"Harmonized Index of Consumer Prices: All Items for Estonia",
)
}
Country::Finland => {
vec!(
"Inflation, consumer prices for Finland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Finland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Finland (DISCONTINUED)",
"Consumer Price Index: All Items for Finland",
"Consumer Price Index: OECD Groups: Services: Total for Finland",
"Consumer Price Index: All items: Total: Total for Finland",
"Consumer Price Index: Harmonized Prices: Total All Items for Finland (DISCONTINUED)",
"Consumer Price Index for Finland",
"Consumer Price Index: Harmonized Prices: Total All Items for Finland",
"Consumer Price Index: Total All Items for Finland",
"Harmonized Index of Consumer Prices: All Items for Finland",
)
}
Country::France => {
vec!(
"Harmonized Index of Consumer Prices in France (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for France (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for France",
"Consumer Price Index in France (DISCONTINUED)",
"Consumer Price Index of All Items in France",
"Consumer Price Index: All items: Total: Total for France",
"Inflation, consumer prices for France",
"Consumer Price Index for France",
"Consumer Price Index: Harmonized Prices: Total All Items for France (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for France",
"Consumer Price Index: Total All Items for France",
)
}
Country::Germany => {
vec!(
"Inflation, consumer prices for Germany",
"Harmonized Index of Consumer Prices in Germany (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Germany (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Germany",
"Consumer Price Index in Germany (DISCONTINUED)",
"Consumer Price Index of All Items in Germany",
"Consumer Price Index: All items: Total: Total for Germany",
"Consumer Price Index for Germany",
"Consumer Price Index: Harmonized Prices: Total All Items for Germany (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for Germany",
"Consumer Price Index: Total All Items for Germany",
"Harmonized Index of Consumer Prices: All Items for Germany (including former GDR from 1991)",
)
}
Country::Greece => {
vec!(
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Greece",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Greece (DISCONTINUED)",
"Consumer Price Index: All Items for Greece",
"Consumer Price Index: All items: Total: Total for Greece",
"Inflation, consumer prices for Greece",
"Consumer Price Index for Greece",
"Consumer Price Index: Harmonized Prices: Total All Items for Greece",
"Consumer Price Index: Harmonized Prices: Total All Items for Greece (DISCONTINUED)",
"Consumer Price Index: Total All Items for Greece",
"Harmonized Index of Consumer Prices: All Items for Greece",
)
}
Country::Ireland => {
vec!(
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Ireland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Ireland (DISCONTINUED)",
"Consumer Price Index: OECD Groups: Services: Total for Ireland",
// "Consumer Price Index: All Items for Ireland", // Does not parse
// "Consumer Price Index: All items: Total: Total for Ireland", // Does not parse
"Consumer Price Index: Harmonized Prices: Total All Items for Ireland (DISCONTINUED)",
"Consumer Price Index for Ireland",
"Consumer Price Index: Harmonized Prices: Total All Items for Ireland",
"Harmonized Index of Consumer Prices: All Items for Ireland",
)
}
Country::Israel => {
vec!(
"Consumer Price Index: All Items for Israel",
"Consumer Price Index: All items: Total: Total for Israel",
"Consumer Price Index for Israel",
"Inflation, consumer prices for Israel",
"Consumer Price Index: Total All Items for Israel",
)
}
Country::Italy => {
vec!(
"Harmonized Index of Consumer Prices in Italy (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Italy (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Italy",
"Consumer Price Index: Food for Italy",
"Consumer Price Index in Italy (DISCONTINUED)",
"Consumer Price Index of All Items in Italy",
"Consumer Price Index: All items: Total: Total for Italy",
"Inflation, consumer prices for Italy",
"Consumer Price Index for Italy",
"Consumer Price Index: Harmonized Prices: Total All Items for Italy (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for Italy",
"Consumer Price Index: Total All Items for Italy",
"Harmonized Index of Consumer Prices: All Items for Italy",
)
}
Country::Japan => {
vec!(
"Harmonized Index of Consumer Prices in Japan (DISCONTINUED)",
"Consumer Price Index in Japan (DISCONTINUED)",
"Consumer Price Index of All Items in Japan",
"Consumer Price Index: All items: Total: Total for Japan",
"Inflation, consumer prices for Japan",
"Not Seasonally Adjusted",
)
}
Country::Latvia => {
vec!(
"Consumer Price Index: All items: Total: Total for Latvia",
"Inflation, consumer prices for Latvia",
"Harmonized Index of Consumer Prices: Unprocessed Food for Latvia",
"Consumer Price Index for Latvia",
)
}
Country::Netherlands => {
vec!(
"Harmonized Index of Consumer Prices in Netherlands (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Netherlands (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Netherlands",
"Consumer Price Index in Netherlands (DISCONTINUED)",
"Consumer Price Index: All Items for Netherlands",
"Consumer Price Index: All items: Total: Total for the Netherlands",
"Consumer Price Index for Netherlands",
"Consumer Price Index: Harmonized Prices: Total All Items for the Netherlands",
"Consumer Price Index: Harmonized Prices: Total All Items for the Netherlands (DISCONTINUED)",
"Consumer Price Index: Total All Items for the Netherlands",
"Harmonized Index of Consumer Prices: All Items for Netherlands",
)
}
Country::NewZealand => {
vec!(
"Consumer Price Index: All Items for New Zealand",
"Consumer Price Index: All Items Excluding Food and Energy for New Zealand",
"Consumer Price Index: All items: Total: Total for New Zealand",
"Consumer Price Index for New Zealand",
"Inflation, consumer prices for New Zealand",
"Consumer Price Index: Total All Items for New Zealand",
)
}
Country::Norway => {
vec!(
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Norway",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Norway (DISCONTINUED)",
"Consumer Price Index: All Items for Norway",
"Consumer Price Index in Norway (DISCONTINUED)",
"Consumer Price Index: All items: Total: Total for Norway",
"Consumer Price Index: Harmonized Prices: Total All Items for Norway (DISCONTINUED)",
"Consumer Price Index for Norway",
"Consumer Price Index: Harmonized Prices: Total All Items for Norway",
"Consumer Price Index: Total All Items for Norway",
"Harmonized Index of Consumer Prices: All Items for Norway",
)
}
Country::Poland => {
vec!(
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Poland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Poland (DISCONTINUED)",
"Consumer Price Index: Food for Poland",
"Consumer Price Index: All Items for Poland",
"Consumer Price Index: All items: Total: Total for Poland",
"Inflation, consumer prices for Poland",
"Consumer Price Index for Poland",
"Consumer Price Index: Total All Items for Poland",
"Harmonized Index of Consumer Prices: All Items for Poland",
)
}
Country::Serbia => {
vec!(
"Consumer Price Index for Serbia",
"Inflation, consumer prices for Serbia",
)
}
Country::SouthKorea => {
vec!(
"Consumer Price Index: All Items for Korea",
"Consumer Price Index: All items: Total: Total for the Republic of Korea",
"Inflation, consumer prices for the Republic of Korea",
"Consumer Price Index for Republic of Korea",
"Consumer Price Index: Total All Items for the Republic of Korea",
)
}
Country::Spain => {
vec!(
"Inflation, consumer prices for Spain",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Spain (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Spain",
"Consumer Price Index in Spain (DISCONTINUED)",
"Consumer Price Index: All Items for Spain",
"Consumer Price Index: All items: Total: Total for Spain",
"Consumer Price Index for Spain",
"Consumer Price Index: Harmonized Prices: Total All Items for Spain (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for Spain",
"Consumer Price Index: Total All Items for Spain",
"Harmonized Index of Consumer Prices: All Items for Spain",
)
}
Country::Sweden => {
vec!(
"Harmonized Index of Consumer Prices in Sweden (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Sweden",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Sweden (DISCONTINUED)",
"Consumer Price Index: All Items for Sweden",
"Consumer Price Index in Sweden (DISCONTINUED)",
"Consumer Price Index: All items: Total: Total for Sweden",
"Consumer Price Index: Harmonized Prices: Total All Items for Sweden",
"Consumer Price Index: Harmonized Prices: Total All Items for Sweden (DISCONTINUED)",
"Consumer Price Index for Sweden",
"Consumer Price Index: Total, Net All Items for Sweden (DISCONTINUED)",
"Consumer Price Index: Total All Items for Sweden",
"Harmonized Index of Consumer Prices: All Items for Sweden",
)
}
Country::Switzerland => {
vec!(
"Consumer Price Index: Harmonized Prices: Total All Items for Switzerland",
"Consumer Price Index: Harmonized Prices: Total All Items for Switzerland (DISCONTINUED)",
"Consumer Price Index for Switzerland",
"Consumer Price Index: Total All Items for Switzerland",
"Harmonized Index of Consumer Prices: All Items for Switzerland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Switzerland",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for Switzerland (DISCONTINUED)",
)
}
Country::UnitedKingdom => {
vec!(
"Harmonized Index of Consumer Prices in the United Kingdom (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for United Kingdom (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for United Kingdom",
"Consumer Price Index in the United Kingdom (DISCONTINUED)",
"Consumer Price Index of All Items in the United Kingdom",
"Consumer Price Index: All Items for United Kingdom",
"Consumer Price Index: All items: Total: Total for the United Kingdom",
"Inflation, consumer prices for the United Kingdom",
"Consumer Price Index for United Kingdom",
"Consumer Price Index in the United Kingdom",
"Consumer Price Inflation in the United Kingdom",
"Consumer Price Index: Harmonized Prices: Total All Items for the United Kingdom (DISCONTINUED)",
"Consumer Price Index: Harmonized Prices: Total All Items for the United Kingdom",
"Consumer Price Index: Total All Items for the United Kingdom",
"Harmonized Index of Consumer Prices: All Items for United Kingdom",
)
}
Country::UnitedStates => {
vec!(
"Harmonized Index of Consumer Prices: All Items for United States",
"Consumer Price Index: Total All Items for the United States",
"Consumer Price Index: Harmonized Prices: Total All Items for the United States",
"Consumer Price Index: Harmonized Prices: Total All Items for the United States (DISCONTINUED)",
"Research Consumer Price Index: All Items",
"Consumer Price Index for United States",
"Flexible Price Consumer Price Index",
"Inflation, consumer prices for the United States",
"Consumer Price Index, All Items for United States",
"Rate of Change (6 Month Span at Annual Rate), Consumer Price Index, All Items (Centered) for United States",
"Median Consumer Price Index",
"Sticky Price Consumer Price Index",
"16% Trimmed-Mean Consumer Price Index",
"Consumer Price Index: All Items for the United States",
"Consumer Price Index of All Items in United States",
"Consumer Price Index in the United States (DISCONTINUED)",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for the United States",
"Consumer Price Index: All Items (Harmonized Index of Consumer Prices) for the United States (DISCONTINUED)",
"Harmonized Index of Consumer Prices in the United States (DISCONTINUED)",
)
}
_ => panic!(),
};
match country {
Country::UnitedStates => {
match Fred::tags_series("cpi;usa") {
Ok(tags_series) => {
tags_series.seriess
.equals_one_of(titles)
},
Err(json_error) => {
println!("{}", json_error);
panic!();
},
}
},
_ => {
match Fred::tags_series(&to_tag("cpi", &country)) {
Ok(tags_series) => {
tags_series.seriess
.equals_one_of(titles)
},
Err(json_error) => {
println!("{}", json_error);
panic!();
},
}
},
}
}
fn to_tag(tag: &str, country: &Country) -> String {
format!(
"{};{}",
tag,
country.to_string().to_lowercase()
)
}
/// Return relevant unemployment rate series for a country.
/// ```
/// println!("{}", unemployment_series(&Country::Canada, "u_series.keytree"));
/// ```
pub fn generic_u_series_spec(country: &Country) -> SeriesItems {
if let Country::UnitedStates = country {
// Need to use a different search technique for US data.
let exclude_phrase = vec!(
"Male",
"Female",
"Men",
"Women",
"Youth",
);
let one_of = vec!(
"Unemployment Rate for United States",
"Unemployment Rate: Aged 15 and Over: All Persons for the United States",
"Unemployment Rate: Aged 15-74: All Persons for the United States",
"Harmonized Unemployment Rate: Total: All Persons for the United States",
"Unemployment Rate - 18 Years and Over",
);
let tag_series = Fred::tags_series("unemployment;rate;usa;nation").unwrap().seriess;
tag_series
.exclude_phrases(exclude_phrase)
.equals_one_of(one_of)
} else {
let (exclude_phrase, include_phrase) = match country {
Country::Australia => {
(
vec!(
"Male",
"Female",
"55-64",
"25-54",
"15-24",
"20 to 24",
"Youth",
"Women",
"Teenagers",
),
vec!(
"Rate"
),
)
}
Country::Austria => {
(
vec!(
"Male",
"Female",
"55-64",
"25-54",
"15-24",
"15-64", // series includes 15-74
"20 to 24",
"Youth",
"Women",
"Teenagers",
),
vec!(
"Rate"
),
)
}
Country::Belgium => {
(
vec!(
"Male",
"Female",
"55-64",
"25-54",
"15-24",
"15-64", // series includes 15-74
"20 to 24",
"Youth",
"Women",
"Teenagers",
),
vec!(
"Rate"
),
)
}
Country::Canada => {
(
vec!(
"Male",
"Female",
"15-64",
"55-64",
"25-54",
"15-24",
"20 to 24",
"Youth",
"Women",
"Teenagers",
),
vec!(
"Rate"
),
)
}
Country::Chile => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate"
),
)
}
Country::CzechRepublic => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate"
),
)
}
Country::Denmark => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate"
),
)
}
Country::Estonia => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Finland => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::France => {
(
vec!(
"Male",
"Men",
"Female",
"Women",
"Youth",
"Teenagers",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Germany => {
(
vec!(
"Male",
"Men",
"Female",
"Youth",
"Women",
"Teenagers",
"20 to 24",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Greece => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Ireland => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Israel => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Italy => {
(
vec!(
"Male",
"Female",
"Youth",
"Men",
"Women",
"Teenagers",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Japan => {
(
vec!(
"Male",
"Female",
"Youth",
"Men",
"Women",
"Teenagers",
"20 to 24",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Latvia => {
(
vec!(
"Youth",
"Male",
"Female",
"25 and over",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"nemployment",
),
)
}
Country::Netherlands => {
(
vec!(
"Male",
"Female",
"Youth",
"Women",
"Teenagers",
"Men",
"20 to 24",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::NewZealand => {
(
vec!(
"Male",
"Female",
"55-64",
"25-54",
"15-24",
"Youth",
),
vec!(
"Rate",
),
)
}
Country::Norway => {
(
vec!(
"Male",
"Female",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Poland => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Serbia => {
(
vec!(
),
vec!(
"",
),
)
}
Country::SouthKorea => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Spain => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::Sweden => {
(
vec!(
"Male",
"Female",
"Youth",
"Men",
"Women",
"Teenagers",
"15-24",
"15-64",
"25-54",
"55-64",
"20 to 24",
),
vec!(
"Rate",
),
)
}
Country::Switzerland => {
(
vec!(
"Male",
"Female",
"Youth",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
Country::UnitedKingdom => {
(
vec!(
"Male",
"Female",
"Youth",
"Men",
"Women",
"Teenagers",
"20 to 24",
"15-24",
"15-64",
"25-54",
"55-64",
),
vec!(
"Rate",
),
)
}
_ => panic!(),
};
match Fred::tags_series(&to_tag("unemployment", country)) {
Ok(tags_series) => {
tags_series.seriess
.exclude_phrases(exclude_phrase)
.only_include(include_phrase)
},
Err(err) => {
println!("{}", err);
panic!();
},
}
}
}
/// ```
/// seriess:
/// series:
/// data_type: u
/// country: Australia
/// id: AUSURAMS
/// ```
pub fn build_data_spec() -> DataSpec {
let mut seriess = DataSpec(Vec::new());
let data_type = DataType::U;
for country in countries_with_data() {
println!("{}", country);
for series_item in generic_u_series_spec(&country).iter() {
seriess.0.push(
SourceSeries {
data_type,
country,
id: series_item.id.clone(),
}
)
}
}
seriess
}
/// ```
/// series:
/// data_type: u
/// country: United States
/// id: LRUNTTTTUSQ156S
/// ```
pub struct DataSpec(pub Vec<SourceSeries>);
impl DataSpec {
/// Read in keytree data from file.
pub fn from_file(path: &str) -> Self {
let source_spec = fs::read_to_string(path).unwrap();
let kt = KeyTree::parse(&source_spec).unwrap();
kt.to_ref().try_into().unwrap()
}
/// Read in source_data.keytree, download series, and return
/// `CheckedSourceSeries`.
pub fn update(&self) {
for series in &self.0 {
let data = match Fred::series(&series.id) {
Ok(data) => println!("{}", data.keytree()),
Err(err) => println!("{:?}", err),
};
}
}
}
impl<'a> TryInto<DataSpec> for KeyTreeRef<'a> {
type Error = Error;
fn try_into(self) -> Result<DataSpec, Error> {
Ok(
DataSpec(self.vec("seriess:series")?)
)
}
}
impl IntoKeyTree for DataSpec {
fn keytree(&self) -> KeyTreeString {
let mut kt = KeyTreeString::new();
kt.push_key(0, "seriess");
for series in &self.0 {
kt.push_keytree(1, series.keytree());
}
kt
}
}
pub struct SourceSeries {
pub data_type: DataType,
pub country: Country,
pub id: String,
}
impl IntoKeyTree for SourceSeries {
fn keytree(&self) -> KeyTreeString {
let mut kt = KeyTreeString::new();
kt.push_key(0, "series");
kt.push_keyvalue(1, "data_type", &self.data_type.to_string());
kt.push_keyvalue(1, "country", &self.country.to_string());
kt.push_keyvalue(1, "id", &self.id);
kt
}
}
impl<'a> TryInto<SourceSeries> for KeyTreeRef<'a> {
type Error = keytree::Error;
fn try_into(self) -> Result<SourceSeries, keytree::Error> {
let data_type_str: String = self.at("series::data_type")?;
let data_type = DataType::from_str(&data_type_str).unwrap();
let country_str: String = self.at("series::country")?;
let country = Country::from_str(&country_str).unwrap();
Ok(
SourceSeries{
data_type,
country,
id: self.at("series::id")?,
}
)
}
}