TSY4YBBZ4AEW2JTEI4AS5FGTTUZR5WE3TENDHVIOWWY6V2DRT7ZQC
736HJN5EZZL5KLZP52TZQLDFE6ZXWEBLUHTHUE4IZ3EOUE3MNHAAC
2CCG6KUP6VL2Q7WQKLVNIPPGZWHSWIOWETRCS3APZTV4WSF5GLWAC
AZQVIGSM6OJHNHTTWY3X5D6YEYWQ5IXNJ2OM7DDVP2MPJ3R37MTAC
SPSFTMLRZE2R4EBAAQKWBUZDRYZ36S2VFTBIJRE6XS7JMKELV4AQC
GQVS55HIQLU7KPJNRMF57QUM4EATSWFQRCS7ZEJMJPUXFX2NHSYAC
2SABVMY3A2RZDF3KJXZSMJ2UQ4Q5EW422G4DVBJRKK26S2ESGVQAC
YJXKWWM6TQLANMJZ2KGCLYUPSHYQXBD6JGZZLGF4BHAPL57YFNAQC
TTR5IFSG25VNBQ2F2FNOLUMTEIHVBHFOEXYB2ZWEHWOURUV4GJMQC
SAHJYVNBUBBIUBI4ZMAXK4QJFOT54M5UA3W2HQMTNDSP3GGCRX7QC
K4CH53V4MO5KCCJOOQUQKI3LEFSUSCNAJS24VUWOZISXTCQD4FZQC
GUXZCEWWPBCHXO26JVWZ74CTSDFDDO775YR7FKY7UGVZCA7GCSYAC
77SIQZ3EGGV6KSECMLPDKQFGEC7CCFAPWGER7ZARQ5STDKJNU6GQC
XI5ALEH6NPTQWB6O62QV62EP4H3K7WSNTHCOGT3LZIIU6I2YDGIQC
4MG5JFXTKAE3SOVKGGNKEUTNCKOWEBHTGKVZHJWLWE3PTZTQKHPAC
//! [Fred API key](https://fred.stlouisfed.org/docs/api/api_key.html) needs to
//! be stored as the environment variable FRED_API_KEY.
//! ```
//! use fred_api::Fred;
//! use fred_api::response::Categories;
//!
//! let categories = Fred::category(125);
use std::env;
use std::fs;
use crate::response::*;
pub mod response;
pub struct Fred;
impl Fred {
/// [Get a category](https://fred.stlouisfed.org/docs/api/fred/category.html)
pub fn category(category_id: usize) -> Categories {
serde_json::from_str(&response(
"category",
vec!(
format!("category_id={}", category_id)
),
)).unwrap()
}
/// [Get the child categories for a specified parent category.](https://fred.stlouisfed.org/docs/api/fred/category_children.html)
pub fn category_children(category_id: usize) -> Categories {
serde_json::from_str(&response(
"category/children",
vec!(
format!("category_id={}", category_id)
),
)).unwrap()
}
/// [Get the related categories for a category.](https://fred.stlouisfed.org/docs/api/fred/category_related.html)
pub fn category_related(category_id: usize) -> Categories {
serde_json::from_str(&response(
"category/related",
vec!(
format!("category_id={}", category_id)
),
)).unwrap()
}
/// [Get the series in a category.](https://fred.stlouisfed.org/docs/api/fred/category_series.html)
pub fn category_series(category_id: usize) -> CategorySeries {
serde_json::from_str(&response(
"category/series",
vec!(
format!("category_id={}", category_id)
),
)).unwrap()
}
/// [Get the tags for a category.](https://fred.stlouisfed.org/docs/api/fred/category_tags.html)
pub fn category_tags(category_id: usize) -> CategoryTags {
serde_json::from_str(&response(
"category/tags",
vec!(
format!("category_id={}", category_id)
),
)).unwrap()
}
/// [Get the related tags for a category.](https://fred.stlouisfed.org/docs/api/fred/category_related_tags.html)
pub fn category_related_tags(category_id: usize, tag_names: &str) -> CategoryRelatedTags {
serde_json::from_str(&response(
"category/related_tags",
vec!(
format!("category_id={}", category_id),
format!("tag_names={}", tag_names),
),
)).unwrap()
}
/// [Get all releases of economic data.](https://fred.stlouisfed.org/docs/api/fred/releases.html)
pub fn releases() -> Releases {
serde_json::from_str(&response(
"releases",
Vec::new(),
)).unwrap()
}
/// [Get release dates for all releases of economic data.](https://fred.stlouisfed.org/docs/api/fred/releases_dates.html)
pub fn releases_dates() -> ReleaseDates {
serde_json::from_str(&response(
"releases/dates",
Vec::new(),
)).unwrap()
}
/// [Get a release of economic data.](https://fred.stlouisfed.org/docs/api/fred/release.html)
pub fn release(release_id: usize) -> Release {
serde_json::from_str(&response(
"release",
vec!(
format!("release_id={}", release_id)
),
)).unwrap()
}
/// [Get release dates for a release of economic data.](https://fred.stlouisfed.org/docs/api/fred/release_dates.html)
pub fn release_dates(release_id: usize) -> ReleaseDates {
serde_json::from_str(&response(
"release/dates",
vec!(
format!("release_id={}", release_id)
),
)).unwrap()
}
/// [Get the series on a release of economic data.](https://fred.stlouisfed.org/docs/api/fred/release_series.html)
pub fn release_series(release_id: usize) -> ReleaseSeries {
serde_json::from_str(&response(
"release/series",
vec!(
format!("release_id={}", release_id)
),
)).unwrap()
}
/// [Get the sources for a release of economic data.](https://fred.stlouisfed.org/docs/api/fred/release_sources.html)
pub fn release_sources(release_id: usize) -> ReleaseSources {
serde_json::from_str(&response(
"release/sources",
vec!(
format!("release_id={}", release_id)
),
)).unwrap()
}
/// [Get the tags for a release.](https://fred.stlouisfed.org/docs/api/fred/release_tags.html)
pub fn release_tags(release_id: usize) -> ReleaseTags {
serde_json::from_str(&response(
"release/tags",
vec!(
format!("release_id={}", release_id)
),
)).unwrap()
}
/// [Get the related tags for a release.](https://fred.stlouisfed.org/docs/api/fred/release_related_tags.html)
pub fn release_related_tags(release_id: usize, tag_names: &str) -> ReleaseRelatedTags {
serde_json::from_str(&response(
"release/related_tags",
vec!(
format!("release_id={}", release_id),
format!("tag_names={}", tag_names),
),
)).unwrap()
}
/// [Get the release tables for a given release.](https://fred.stlouisfed.org/docs/api/fred/release_tables.html)
pub fn release_tables(release_id: usize) -> ReleaseTables {
serde_json::from_str(&response(
"release/tables",
vec!(
format!("release_id={}", release_id)
),
)).unwrap()
}
/// [Get an economic data series.](https://fred.stlouisfed.org/docs/api/fred/series.html)
pub fn series(series_id: &str) -> Series {
serde_json::from_str(&response(
"series",
vec!(
format!("series_id={}", series_id)
),
)).unwrap()
}
/// [Get the categories for an economic data series.](https://fred.stlouisfed.org/docs/api/fred/series_categories.html)
pub fn series_categories(series_id: &str) -> Categories {
serde_json::from_str(&response(
"series/categories",
vec!(
format!("series_id={}", series_id)
),
)).unwrap()
}
/// [Get the observations or data values for an economic data series.](https://fred.stlouisfed.org/docs/api/fred/series_observations.html)
pub fn series_observations(series_id: &str) -> SeriesObservations {
serde_json::from_str(&response(
"series/observations",
vec!(
format!("series_id={}", series_id)
),
)).unwrap()
}
/// [Get the release for an economic data series.](https://fred.stlouisfed.org/docs/api/fred/series_release.html)
pub fn series_release(series_id: &str) -> SeriesRelease {
serde_json::from_str(&response(
"series/release",
vec!(
format!("series_id={}", series_id)
),
)).unwrap()
}
/// [Get economic data series that match keywords.](https://fred.stlouisfed.org/docs/api/fred/series_search.html)
pub fn series_search(search_text: &str) -> SeriesSearch {
serde_json::from_str(&response(
"series/search",
vec!(
format!("search_text={}", search_text)
),
)).unwrap()
}
/// [Get the tags for a series search.](https://fred.stlouisfed.org/docs/api/fred/series_search_tags.html)
pub fn series_search_tags(series_search_text: &str) -> SeriesSearchTags {
serde_json::from_str(&response(
"series/search/tags",
vec!(
format!("series_search_text={}", series_search_text)
),
)).unwrap()
}
/// [Get the related tags for a series search.](https://fred.stlouisfed.org/docs/api/fred/series_search_related_tags.html)
pub fn series_search_related_tags(series_search_text: &str, tag_names: &str) -> SeriesSearchRelatedTags {
serde_json::from_str(&response(
"series/search/related_tags",
vec!(
format!("series_search_text={}", series_search_text),
format!("tag_names={}", tag_names),
),
)).unwrap()
}
/// [Get the tags for an economic data series.](https://fred.stlouisfed.org/docs/api/fred/series_tags.html)
pub fn series_tags(series_id: &str) -> SeriesTags {
serde_json::from_str(&response(
"series/tags",
vec!(
format!("series_id={}", series_id)
),
)).unwrap()
}
/// [Get economic data series sorted by when observations were updated on the FRED® server.](https://fred.stlouisfed.org/docs/api/fred/series_updates.html)
pub fn series_updates() -> SeriesUpdates {
serde_json::from_str(&response(
"series/updates",
Vec::new(),
)).unwrap()
}
/// [Get the dates in history when a series' data values were revised or new data values were released.](https://fred.stlouisfed.org/docs/api/fred/series_vintagedates.html)
pub fn series_vintagedates(series_id: &str) -> SeriesVintageDates {
serde_json::from_str(&response(
"series/vintagedates",
vec!(
format!("series_id={}", series_id)
),
)).unwrap()
}
/// [Get all sources of economic data.](https://fred.stlouisfed.org/docs/api/fred/sources.html)
pub fn sources() -> Sources {
serde_json::from_str(&response(
"sources",
Vec::new(),
)).unwrap()
}
/// [Get a source of economic data.](https://fred.stlouisfed.org/docs/api/fred/source.html)
pub fn source(source_id: usize) -> ReleaseSources {
serde_json::from_str(&response(
"source",
vec!(
format!("source_id={}", source_id)
),
)).unwrap()
}
/// [Get the releases for a source.](https://fred.stlouisfed.org/docs/api/fred/source_releases.html)
pub fn source_releases(source_id: usize) -> SourceReleases {
serde_json::from_str(&response(
"source/releases",
vec!(
format!("source_id={}", source_id)
),
)).unwrap()
}
/// [Get all tags, search for tags, or get tags by name.](https://fred.stlouisfed.org/docs/api/fred/tags.html)
pub fn tags() -> Tags {
serde_json::from_str(&response(
"tags",
Vec::new(),
)).unwrap()
}
/// [Get the related tags for one or more tags.](https://fred.stlouisfed.org/docs/api/fred/related_tags.html)
pub fn related_tags(tag_names: &str) -> Tags {
serde_json::from_str(&response(
"related_tags",
vec!(
format!("tag_names={}", tag_names)
),
)).unwrap()
}
/// [Get the series matching tags.](https://fred.stlouisfed.org/docs/api/fred/tags_series.html)
pub fn tags_series(tag_names: &str) -> TagsSeries {
serde_json::from_str(&response(
"tags/series",
vec!(
format!("tag_names={}", tag_names)
),
)).unwrap()
}
}
fn response(url: &str, keyvals: Vec<String>) -> String {
json(&request_str(url, keyvals))
}
fn json(req: &str) -> String {
let json = reqwest::blocking::get(req)
.unwrap()
.text_with_charset("utf-8")
.unwrap();
fs::write("temp.json", &json).unwrap();
json
}
fn request_str(url: &str, keyvals: Vec<String>) -> String {
let mut s = format!(
"https://api.stlouisfed.org/fred/{}?",
url,
);
for kv in keyvals {
s.push_str(&kv);
s.push('&');
};
s.push_str(&format!(
"api_key={}&file_type=json",
env::var("FRED_API_KEY").unwrap(),
));
s
}
//! Javascript-like loosely typed data-structures based on FRED API.
use std::fmt;
use serde::{Deserialize};
#[derive(Debug, Deserialize)]
pub struct Categories {
pub categories: Vec<Category>,
}
/// See [Fred docs: /fred/category](https://fred.stlouisfed.org/docs/api/fred/category.html).
///
#[derive(Debug, Deserialize)]
pub struct Category {
pub id: usize,
pub name: String,
pub parent_id: usize,
pub notes: Option<String>,
}
/// See [Fred docs: /fred/category/children](https://fred.stlouisfed.org/docs/api/fred/category_children.html).
#[derive(Debug, Deserialize)]
pub struct CategoryChildren {
pub categories: Vec<Category>,
}
/// See [Fred docs: /fred/category/related](https://fred.stlouisfed.org/docs/api/fred/category_related.html).
#[derive(Debug, Deserialize)]
pub struct CategoryRelated {
categories: Vec<Category>,
}
/// See Fred docs
#[derive(Debug, Deserialize)]
pub struct CategorySeries {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub seriess: Vec<SeriesItem>,
}
/// See [Fred docs: /fred/category/tags](https://fred.stlouisfed.org/docs/api/fred/category_tags.html).
#[derive(Debug, Deserialize)]
pub struct CategoryTags {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub tags: Vec<Tag>,
}
#[derive(Debug, Deserialize)]
pub struct Tag {
pub name: String,
pub group_id: String,
pub notes: Option<String>,
pub created: String,
pub popularity: isize,
pub series_count: isize,
}
impl fmt::Display for Tag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,
"name: {}\nnotes: {:?}\nseries_count: {}",
self.name,
self.notes,
self.series_count
)
}
}
/// See [Fred docs: /fred/category/related_tags](https://fred.stlouisfed.org/docs/api/fred/category_related_tags.html).
#[derive(Debug, Deserialize)]
pub struct CategoryRelatedTags {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub tags: Vec<Tag>,
}
/// See [Fred docs: /fred/releases](https://fred.stlouisfed.org/docs/api/fred/releases.html).
#[derive(Debug, Deserialize)]
pub struct Releases {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub releases: Vec<ReleaseItem>,
}
/// See [Fred docs: /fred/release](https://fred.stlouisfed.org/docs/api/fred/release.html).
#[derive(Debug, Deserialize)]
pub struct Release {
pub realtime_start: String,
pub realtime_end: String,
pub releases: Vec<ReleaseItem>,
}
#[derive(Debug, Deserialize)]
pub struct ReleaseItem {
pub id: isize,
pub realtime_start: String,
pub realtime_end: String,
pub name: String,
pub press_release: bool,
pub link: Option<String>,
}
/// See [Fred docs: /fred/release/dates](https://fred.stlouisfed.org/docs/api/fred/release_dates.html).
#[derive(Debug, Deserialize)]
pub struct ReleasesDates {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub release_dates: Vec<ReleaseDate>,
}
#[derive(Debug, Deserialize)]
pub struct ReleaseDates {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub release_dates: Vec<ReleaseDateItem>,
}
#[derive(Debug, Deserialize)]
pub struct ReleaseDateItem {
pub release_id: isize,
pub date: String,
}
#[derive(Debug, Deserialize)]
pub struct ReleaseDate {
pub release_id: isize,
pub release_name: String,
pub date: String,
}
/// See [Fred docs: /fred/release/series](https://fred.stlouisfed.org/docs/api/fred/release_series.html).
#[derive(Debug, Deserialize)]
pub struct ReleaseSeries {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub seriess: Vec<SeriesItem>,
}
/// See [Fred docs: /fred/release/sources](https://fred.stlouisfed.org/docs/api/fred/release_sources.html).
#[derive(Debug, Deserialize)]
pub struct ReleaseSources {
pub realtime_start: String,
pub realtime_end: String,
pub sources: Vec<SourceItem>,
}
#[derive(Debug, Deserialize)]
pub struct SourceItem {
pub id: isize,
pub realtime_start: String,
pub realtime_end: String,
pub name: String,
pub link: Option<String>,
}
/// See [Fred docs: /fred/release/tags](https://fred.stlouisfed.org/docs/api/fred/release_tags.html).
#[derive(Debug, Deserialize)]
pub struct ReleaseTags {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub tags: Vec<TagItem>,
}
#[derive(Debug, Deserialize)]
pub struct TagItem {
pub name: String,
pub group_id: String,
pub notes: Option<String>,
pub created: String,
pub popularity: isize,
pub series_count: isize,
}
/// See [Fred docs: /fred/release/related_tags](https://fred.stlouisfed.org/docs/api/fred/release_related_tags.html).
#[derive(Debug, Deserialize)]
pub struct ReleaseRelatedTags {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub tags: Vec<TagItem>,
}
/// See [Fred docs: /fred/release/related_tags](https://fred.stlouisfed.org/docs/api/fred/release_related_tags.html).
#[derive(Debug, Deserialize)]
pub struct ReleaseTables {
pub name: String,
pub element_id: isize,
pub release_id: String,
pub elements: Vec<ReleaseKeyVal>,
}
/// See [Fred docs: /fred/release/related_tags](https://fred.stlouisfed.org/docs/api/fred/release_related_tags.html).
#[derive(Debug, Deserialize)]
pub struct ReleaseKeyVal {
pub key: isize,
pub value: ReleaseElement,
}
/// See [Fred docs: /fred/release/related_tags](https://fred.stlouisfed.org/docs/api/fred/release_related_tags.html).
#[derive(Debug, Deserialize)]
pub struct ReleaseElement {
pub element_id: isize,
pub release_id: String,
pub series_id: String,
pub parent_id: String,
pub line: String,
#[serde(rename = "type")]
pub ty: String,
pub name: String,
pub level: String,
pub children: Vec<ReleaseElement>,
}
#[derive(Debug, Deserialize)]
pub struct Series {
pub realtime_start: String,
pub realtime_end: String,
pub seriess: Vec<SeriesItem>,
}
impl fmt::Display for Series {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = format!(
"{}-{}\n",
self.realtime_start,
self.realtime_end,
);
for item in &self.seriess {
s.push_str(&item.to_string())
};
write!(f, "{}", s)
}
}
#[derive(Debug, Deserialize)]
pub struct SeriesItem {
pub id: String,
pub realtime_start: String,
pub realtime_end: String,
pub title: String,
pub observation_start: String,
pub observation_end: String,
pub frequency: String,
pub units: String,
pub units_short: String,
pub seasonal_adjustment: String,
pub seasonal_adjustment_short: String,
pub last_updated: String,
pub popularity: isize,
pub group_popularity: Option<isize>,
pub notes: Option<String>,
}
impl fmt::Display for SeriesItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"{}\n{}\n{}-{}\n{}\n{}",
self.id,
self.title,
self.observation_start,
self.observation_end,
self.frequency,
self.seasonal_adjustment,
)
}
}
/// See [Fred docs: /fred/series/categories](https://fred.stlouisfed.org/docs/api/fred/series_categories.html).
#[derive(Debug, Deserialize)]
pub struct SeriesCategories {
pub categories: Vec<CategoryItem>,
}
#[derive(Debug, Deserialize)]
pub struct CategoryItem {
pub id: String,
pub name: String,
pub parent_id: isize,
}
/// See [Fred docs: /fred/series/observations](https://fred.stlouisfed.org/docs/api/fred/series_observations.html).
#[derive(Debug, Deserialize)]
pub struct SeriesObservations {
pub realtime_start: String,
pub realtime_end: String,
pub observation_start: String,
pub observation_end: String,
pub units: String,
pub output_type: isize,
pub file_type: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub observations: Vec<Observation>,
}
/// See [Fred docs: /fred/series/observations](https://fred.stlouisfed.org/docs/api/fred/series_observations.html).
#[derive(Debug, Deserialize)]
pub struct Observation {
pub realtime_start: String,
pub realtime_end: String,
pub date: String,
pub value: String,
}
/// See [Fred docs: /fred/series/release](https://fred.stlouisfed.org/docs/api/fred/series_release.html).
#[derive(Debug, Deserialize)]
pub struct SeriesRelease {
pub realtime_start: String,
pub realtime_end: String,
pub releases: Vec<ReleaseItem>,
}
/// See [Fred docs: /fred/series/search](https://fred.stlouisfed.org/docs/api/fred/series_search.html).
#[derive(Debug, Deserialize)]
pub struct SeriesSearch {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub seriess: Vec<SeriesItem>,
}
#[derive(Debug, Deserialize)]
pub struct SeriesSearchTags {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub tags: Vec<TagItem>,
}
/// See [Fred docs: /fred/series/search/related_tags](https://fred.stlouisfed.org/docs/api/fred/series_search_related_tags.html).
#[derive(Debug, Deserialize)]
pub struct SeriesSearchRelatedTags {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub tags: Vec<TagItem>,
}
/// See [Fred docs: /fred/series/tags](https://fred.stlouisfed.org/docs/api/fred/series_tags.html).
#[derive(Debug, Deserialize)]
pub struct SeriesTags {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub tags: Vec<TagItem>,
}
/// See [Fred docs: /fred/series/updates](https://fred.stlouisfed.org/docs/api/fred/series_updates.html).
#[derive(Debug, Deserialize)]
pub struct SeriesUpdates {
pub realtime_start: String,
pub realtime_end: String,
pub filter_variable: String,
pub filter_value: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub seriess: Vec<SeriesItem>,
}
/// See [Fred docs: /fred/series/vintage_dates](https://fred.stlouisfed.org/docs/api/fred/series_vintagedates.html).
#[derive(Debug, Deserialize)]
pub struct SeriesVintageDates {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub vintage_dates: Vec<String>,
}
/// See [Fred docs: /fred/sources](https://fred.stlouisfed.org/docs/api/fred/sources.html).
#[derive(Debug, Deserialize)]
pub struct Sources {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub sources: Vec<SourceItem>,
}
/// See [Fred docs: /fred/source/releases](https://fred.stlouisfed.org/docs/api/fred/source_releases.html).
#[derive(Debug, Deserialize)]
pub struct SourceReleases {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub releases: Vec<ReleaseItem>,
}
/// See [Fred docs: /fred/tags](https://fred.stlouisfed.org/docs/api/fred/tags.html).
#[derive(Debug, Deserialize)]
pub struct Tags {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub tags: Vec<Tag>,
}
impl fmt::Display for Tags {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut tags = String::new();
for (i, tag) in self.tags.iter().enumerate() {
tags.push_str(&i.to_string());
tags.push('\n');
tags.push_str(&tag.to_string());
tags.push('\n');
tags.push('\n');
};
write!(f, "{}", tags)
}
}
/// See [Fred docs: /fred/tags/series](https://fred.stlouisfed.org/docs/api/fred/tags_series.html).
#[derive(Debug, Deserialize)]
pub struct TagsSeries {
pub realtime_start: String,
pub realtime_end: String,
pub order_by: String,
pub sort_order: String,
pub count: isize,
pub offset: isize,
pub limit: isize,
pub seriess: Vec<SeriesItem>,
}
impl fmt::Display for TagsSeries {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut series = String::new();
for (i, s) in self.seriess.iter().enumerate() {
series.push_str(&format!("{}\n", i));
series.push_str(&s.to_string());
series.push('\n');
};
write!(f, "{}", series)
}
}
impl TagsSeries {
pub fn series_titles(&self) -> String {
let mut s = String::new();
for series in &self.seriess {
s.push_str(&series.title);
s.push('\n');
}
s
}
}
}
impl fmt::Display for SeriesTags {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = String::new();
for (i, tags) in self.tags.iter().enumerate() {
s.push_str(&i.to_string());
s.push('\n');
s.push_str(&tags.to_string());
s.push('\n');
};
write!(f, "{}", s)
}
impl fmt::Display for TagItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,
"name: {}\ngroup_id: {}\nnotes: {:?}\n",
self.name,
self.group_id,
self.notes,
)
}
}
impl fmt::Display for CategorySeries {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = String::new();
for (i, series) in self.seriess.iter().enumerate() {
s.push_str(&i.to_string());
s.push('\n');
s.push_str(&series.to_string());
s.push('\n');
};
write!(f, "{}", s)
}
}
impl fmt::Display for Category {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,
"id: {}\nname: {}\nparent_id: {}\nnotes: {:?}\n",
self.id,
self.name,
self.parent_id,
self.notes,
)
}
}
}
impl fmt::Display for Categories {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = String::new();
for category in &self.categories {
s.push_str(&category.to_string());
};
write!(f, "{}", s)
}
[package]
name = "fred_api"
version = "0.1.0"
authors = ["Eric Findlay <e.findlay@protonmail.ch>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.0"
reqwest = { version = "0.10", features = ["blocking", "json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.60"
[lib]
doctest = false
}
pub (crate) fn new() -> Self {
Spec(Vec::new())
}
pub (crate) fn push(&mut self, page_spec: PageSpec) {
self.0.push(page_spec)
country: Country,
data_type: DataType,
index: usize,
series: Vec<SeriesSpec>,
graphics: Vec<GraphicSpec>,
}
impl PageSpec {
///
pub country: Country,
///
pub data_type: DataType,
pub fn new(
country: Country,
data_type: DataType,
index: usize,
series: Vec<SeriesSpec>,
graphics: Vec<GraphicSpec>) -> Self
{
PageSpec { country, data_type, index, series, graphics }
}
pub index: usize,
///
pub series: Vec<SeriesSpec>,
///
pub graphics: Vec<GraphicSpec>,
}
}
impl IntoKeyTree for PageSpec {
fn keytree(&self) -> KeyTreeString {
let mut kt = KeyTreeString::new();
kt.push_key(0, "page");
kt.push_value(1, "country", self.country);
kt.push_value(1, "data_type", self.data_type);
kt.push_value(1, "index", self.index);
for series in &self.series {
kt.push_keytree(1, series.keytree());
}
for graphic in &self.graphics {
kt.push_keytree(1, graphic.keytree());
}
kt
title: Option<String>,
height: Option<f32>,
series_id: Vec<SeriesId>,
fid: Vec<String>,
text: TextSpec,
}
impl GraphicSpec {
pub (crate) fn empty() -> Self {
GraphicSpec {
title: None,
height: None,
series_id: Vec::new(),
fid: Vec::new(),
text: TextSpec::None,
}
}
// pub (crate) fn push(
// &mut self,
// title: Option<Title>,
// height: Option<f32>,
// transform_spec: Vec<TransformSpec>)
// {
// let graph_spec = GraphSpec {
// title,
// height,
// transform_spec,
// };
// self.seriess.push(series_spec)
// }
///
pub title: Option<String>,
///
pub height: Option<f32>,
///
pub series_id: Vec<SeriesId>,
///
pub fid: Vec<String>,
///
pub text_spec: TextSpec,
if let Some(title) = &self.title {
kt.push_value(1, "title", title);
}
if let Some(h) = &self.height {
kt.push_value(1, "height", h);
}
for series_id in &self.series_id {
kt.push_value(1, "series_id", series_id);
}
for fid in &self.fid {
kt.push_value(1, "fid", fid)
}
kt.push_value(1, "text", &self.text_spec);
kt
}
}
data_type: DataType,
series_id: SeriesId,
date_range: DateRange,
transforms: Vec<Transform>,
fid: Option<FID>,
}
impl SeriesSpec {
///
pub fn new(
data_type: DataType,
series_id: SeriesId,
date_range: DateRange,
transforms: Vec<Transform>,
fid: Option<FID>,
) -> Self {
SeriesSpec {
data_type,
series_id,
date_range,
transforms,
fid,
}
}
///
pub data_type: DataType,
///
pub series_id: SeriesId,
///
pub date_range: DateRange,
///
pub transforms: Vec<Transform>,
///
pub fid: Option<FID>,
if let Some(first_date) = self.date_range.first_date() {
kt.push_value(1, "first_date", first_date);
}
if let Some(last_date) = self.date_range.last_date() {
kt.push_value(1, "last_date", last_date);
};
for f in &self.transforms {
kt.push_value(1, "f", f);
}
if let Some(fid) = &self.fid {
kt.push_value(1, "fid", fid);
}
kt
}
}
impl fmt::Display for FID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
// for series_spec in series_specs {
// let series_spec = ts::SeriesSpec::new(
// series_spec.data_type,
// series_id,
// DateRange::new(&None, &None),
// f,
// FID::new(series_id, f),
// );
// graphic_spec.push(series_spec);
// };
let mut single_graphics = Vec::new();
// let page_spec = ts::PageSpec::new(
// *country,
// *data_type,
// 0,
// Vec::new(),
// vec!(graphic_spec),
// );
for series_spec in series_specs {
let ts_series_spec = ts::SeriesSpec {
data_type: series_spec.data_type,
series_id: series_spec.series_id.clone(),
date_range: DateRange::new(&None, &None),
transforms: Vec::new(),
fid: None,
};
// Series
page_spec.series.push(ts_series_spec);
// Single-series graphics
let single_series_graphic = ts::GraphicSpec {
title: None,
height: None,
series_id: vec!(series_spec.series_id.clone()),
fid: Vec::new(),
text_spec: TextSpec::Meta,
};
single_graphics.push(single_series_graphic);
// Collated graphic
collated_graphic.series_id.push(series_spec.series_id.clone());
};
let mut graphics = vec!(collated_graphic);
graphics.append(&mut single_graphics);
page_spec.graphics = graphics;