Finished code for building TSJson struct.

[?]
CrEcTsRjb1hHQjHuumqRfqdbVV4X58iLEubi4noaDPFa
Jul 22, 2021, 5:32 AM
XPXYFEZMZJSF2GOFG5HTCMLVHINNVRYIVWV7KJ6A7A3PG3CM2TGAC

Dependencies

  • [2] BB2T6X3X Improved documentation.
  • [3] 5B2HBV3J Completed first try at ts Json data-structure.
  • [4] SPSFTMLR Completed loading ts_data from specification.
  • [5] 2SABVMY3 Finished into_json() functionality.
  • [6] 2CCG6KUP Redo time-series spec.
  • [7] TTR5IFSG Working on building generic TSSpec.
  • [8] GQVS55HI Finished generate_ts_spec() function.
  • [9] SAHJYVNB Removed checking functionality.
  • [10] UKQAGL5F Checked ts_json toolchain.
  • [11] 77SIQZ3E Separating out spec generation.
  • [12] K4CH53V4 Added GPL2 license, included missing source files.
  • [13] UUD3CJZL Making error handling more comprehensive.
  • [14] XI5ALEH6 Take advantage of keytree FromStr functionality.
  • [15] TSY4YBBZ Changed ts Spec datastructures to new format.
  • [*] GUXZCEWW Added Country enum.
  • [*] 4MG5JFXT First record.

Change contents

  • replacement in src/ts.rs at line 25
    [3.194][3.194:267]()
    pub struct Json(BTreeMap<(Country, DataType, usize), Vec<GraphicJson>>);
    [3.194]
    [3.267]
    pub struct Json(BTreeMap<(Country, DataType, usize), (Vec<SeriesJson>, Vec<GraphicJson>)>);
  • edit in src/ts.rs at line 30
    [3.321]
    [3.321]
    // Title is displayed if Some otherwise no or default title displayed.
  • replacement in src/ts.rs at line 32
    [3.353][3.353:386]()
    graphics: Vec<SeriesJson>,
    [3.353]
    [3.386]
    // Reference to series data.
    series_ref: Vec<usize>,
    // Specifies what kind of text appears under the graphic.
  • replacement in src/ts.rs at line 78
    [2.1229][3.611:689](),[3.611][3.611:689]()
    pub (crate) fn into_json(&self, root_path: &str) -> Result<Json, Error> {
    [2.1229]
    [3.689]
    pub fn into_json(&self, root_path: &str) -> Result<Json, Error> {
  • edit in src/ts.rs at line 81
    [3.730]
    [3.730]
    // Iterate over pages.
  • replacement in src/ts.rs at line 85
    [3.766][3.766:780]()
    [3.766]
    [3.780]
  • replacement in src/ts.rs at line 96
    [3.1001][3.1001:1055]()
    let mut v: Vec<GraphicJson> = Vec::new();
    [3.1001]
    [3.1055]
    let mut v_graphics: Vec<GraphicJson> = Vec::new();
    let mut m_series: BTreeMap<SeriesId, (usize, SeriesJson)> = BTreeMap::new();
  • edit in src/ts.rs at line 100
    [3.1056]
    [3.1056]
    // Iterate over graphics on one page
  • edit in src/ts.rs at line 118
    [3.1523]
    [3.1523]
    // Iterate over series_ids and build series_json first. There is a one-to-one
    // correspondence between SeriesSpec and SeriesJson so we can use the indices in
    // SeriesSpec to build GraphicJson.
    // Push SeriesJson Vec<SeriesJson>
    for (i, series_id) in series_ids.iter().enumerate() {
    m_series.insert(
    series_id.clone(),
    (
    i,
    page_spec.into_series_json(series_id, *country, root_path)?,
    )
    );
    }
  • edit in src/ts.rs at line 136
    [3.1524]
    [3.1524]
    // Construct GraphicJson for a given Graphic
  • replacement in src/ts.rs at line 140
    [3.1617][3.1617:1661]()
    graphics: Vec::new(),
    [3.1617]
    [3.1661]
    series_ref: Vec::new(),
  • replacement in src/ts.rs at line 145
    [3.1783][3.1783:1892]()
    graphic_json.graphics.push(page_spec.into_series_json(series_id, *country, root_path)?);
    [3.1783]
    [3.1892]
    let series_ref = match m_series.get(series_id) {
    Some((series_ref, _)) => series_ref,
    None => {
    return Err(failed_to_reference_series(
    file!(),
    line!(),
    &series_id.to_string(),
    ))
    },
    };
    graphic_json.series_ref.push(*series_ref);
  • replacement in src/ts.rs at line 158
    [3.1911][3.1911:1965]()
    // Push GraphicJson to temporary Vec.
    [3.1911]
    [3.1965]
    // Push GraphicJson to Vec<GraphicJson>
  • replacement in src/ts.rs at line 160
    [3.1966][3.1966:2004]()
    v.push(graphic_json);
    [3.1966]
    [3.2004]
    v_graphics.push(graphic_json);
  • replacement in src/ts.rs at line 163
    [3.2019][3.2019:2061]()
    // Push key and value to json
    [3.2019]
    [3.2061]
    // Now that we have iterated over all the graphics on one page we can insert the
    // collection of SeriesJsons and the collection of GraphicsJsons into Json.
    // Convert v_series to a Vec.
  • replacement in src/ts.rs at line 170
    [3.2062][3.2062:2119]()
    json.insert((country, data_type, index), v);
    [3.2062]
    [3.2119]
    let mut v_series: Vec<SeriesJson> = Vec::new();
    for (_, (_, series_json)) in m_series.into_iter() {
    v_series.push(series_json);
    };
    json.insert((*country, *data_type, *index), (v_series, v_graphics));
  • replacement in src/ts.rs at line 177
    [3.2130][3.2130:2164]()
    Ok(Json(BTreeMap::new()))
    [3.2130]
    [3.9460]
    Ok(Json(json))
  • edit in src/ts.rs at line 235
    [3.2421]
    [3.2421]
  • replacement in src/ts.rs at line 248
    [3.2798][3.2798:2910]()
    let rts = series_spec.data_with_transforms()?;
    let meta = series_spec.meta(country, root_path);
    [3.2798]
    [3.2910]
    let rts = series_spec.read_data_with_transforms(country, root_path)?;
    let meta = series_spec.read_meta_from_file(country, root_path);
  • replacement in src/ts.rs at line 302
    [3.560][3.560:604]()
    /// Specifies what text a graphic displays.
    [3.560]
    [3.3437]
    /// Specifies what kind of text is displayed under a graphic.
  • replacement in src/main.rs at line 45
    [3.11132][3.4123:4201]()
    let data_spec = DataSpec::from_file("source_data.keytree").unwrap();
    [3.11132]
    [3.11210]
    // let data_spec = DataSpec::from_file("source_data.keytree").unwrap();
  • replacement in src/main.rs at line 50
    [3.10088][3.4202:4265]()
    println!("{}", data_spec.generic_ts_spec().keytree());
    [3.10088]
    [3.11347]
    // println!("{}", data_spec.generic_ts_spec().keytree());
  • replacement in src/main.rs at line 56
    [3.11388][3.4471:4545]()
    // let ts_spec = ts::Spec::from_file("ts_spec.keytree").unwrap();
    [3.11388]
    [3.11459]
    let ts_spec = ts::Spec::from_file("ts_spec.keytree").unwrap();
    // println!("{}", ts_spec.keytree());
    ts_spec.into_json(&root_dir).unwrap();
  • replacement in src/lib.rs at line 278
    [3.5422][3.2869:2902](),[3.2869][3.2869:2902]()
    /// Represents a FRED series id.
    [3.5422]
    [3.2617]
    /// Represents a FRED series id like `LRHUTTTTAUA156N` or a transformation on a FRED series_id
    /// like `LRHUTTTTAUA156N_a`.
  • edit in src/lib.rs at line 288
    [3.11355]
    [3.11355]
    }
    /// Return the component without transformation modifications.
    pub fn stem(&self) -> Self {
    let inner = self.0.split('_').next().unwrap().clone();
    SeriesId(String::from(inner))
  • edit in src/lib.rs at line 392
    [3.3049]
    [3.3049]
    #[derive(Debug)]
  • replacement in src/lib.rs at line 572
    [3.969][3.14458:14563]()
    series_spec.write_data(root_path)?;
    series_spec.write_meta(root_path)?;
    [3.969]
    [3.14563]
    series_spec.write_data_to_file(root_path)?;
    series_spec.write_meta_to_file(root_path)?;
  • replacement in src/lib.rs at line 590
    [3.15173][3.15173:15285]()
    series_spec.write_data(root_path)?;
    series_spec.write_meta(root_path)?;
    [3.15173]
    [3.15285]
    series_spec.write_data_to_file(root_path)?;
    series_spec.write_meta_to_file(root_path)?;
  • replacement in src/lib.rs at line 651
    [3.2051][3.16952:17056]()
    series_spec.write_data(root_path)?;
    series_spec.write_meta(root_path)?;
    [3.2051]
    [3.2154]
    series_spec.write_data_to_file(root_path)?;
    series_spec.write_meta_to_file(root_path)?;
  • replacement in src/lib.rs at line 697
    [3.5968][3.5968:6155]()
    // We pass country because `series_spec.country` is None.
    /// Return the meta data for a `SeriesSpec`.
    pub fn meta(&self, country: Country, root_path: &str) -> SeriesMetaData
    [3.5968]
    [3.6155]
    /// Read metadata from file.
    pub fn read_meta_from_file(&self, country: Country, root_path: &str) -> SeriesMetaData
  • replacement in src/lib.rs at line 704
    [3.6263][3.6263:6299]()
    self.series_id.clone(),
    [3.6263]
    [3.6299]
    self.series_id.stem(),
  • replacement in src/lib.rs at line 714
    [3.6502][3.17098:17131](),[3.1253][3.17098:17131]()
    /// Check if a file exists.
    [3.6502]
    [3.17131]
    /// Check if data and meta-data exist in a file.
  • replacement in src/lib.rs at line 741
    [3.17682][3.1253:1497](),[3.1253][3.1253:1497]()
    /// Return `Self` given a `series_id`.
    pub fn from_series_id(series_id: &str, root_path: &str) -> Result<Self, Error> {
    let sid = SeriesId::from_str(series_id).unwrap();
    let data_spec = DataSpec::from_file(root_path)?;
    [3.17682]
    [3.1497]
    // /// Return `Self` given a `series_id`.
    // pub fn from_series_id(series_id: &str, root_path: &str) -> Result<Self, Error> {
    // let sid = SeriesId::from_str(series_id).unwrap();
    // let data_spec = DataSpec::from_file(root_path)?;
  • replacement in src/lib.rs at line 746
    [3.1498][3.1498:1582](),[3.1582][3.17683:17895](),[3.17895][3.1669:1680](),[3.1669][3.1669:1680]()
    let key = match data_spec.reverse.get(&sid) {
    Some(key) => key,
    None => { return Err(
    series_id_not_in_dataspec(
    file!(),
    line!(),
    &series_id.to_string()
    )
    )},
    };
    [3.1498]
    [3.1680]
    // let key = match data_spec.reverse.get(&sid) {
    // Some(key) => key,
    // None => { return Err(
    // series_id_not_in_dataspec(
    // file!(),
    // line!(),
    // &series_id.to_string()
    // )
    // )},
    // };
  • replacement in src/lib.rs at line 757
    [3.1681][3.1681:2027](),[3.2027][3.6703:6800]()
    match data_spec.map.get(&key) {
    Some(series_specs) => {
    Ok(
    series_specs.iter()
    .find(|series_spec| series_spec.series_id == sid)
    .unwrap()
    .clone()
    )
    },
    None => unreachable!(),
    }
    }
    pub fn data_with_transforms(&self) -> Result<RegularTimeSeries<1>, Error> {
    [3.1681]
    [3.6800]
    // match data_spec.map.get(&key) {
    // Some(series_specs) => {
    // Ok(
    // series_specs.iter()
    // .find(|series_spec| series_spec.series_id == sid)
    // .unwrap()
    // .clone()
    // )
    // },
    // None => unreachable!(),
    // }
    // }
  • replacement in src/lib.rs at line 770
    [3.6801][3.6801:6855]()
    let mut rts = self.data_without_transform()?;
    [3.6801]
    [3.6855]
    pub fn read_data_with_transforms(
    &self,
    country: Country,
    root_path: &str) -> Result<RegularTimeSeries<1>, Error>
    {
    let mut rts = self.read_data_without_transform(country, root_path)?;
  • replacement in src/lib.rs at line 803
    [3.2043][3.7724:7733]()
    [3.2043]
    [3.8091]
    pub fn read_data_without_transform(
    &self,
    country: Country,
    root_path: &str) -> Result<RegularTimeSeries<1>, Error>
    {
    let series_id_stem = self.series_id.stem();
  • replacement in src/lib.rs at line 812
    [3.8092][3.17896:17989](),[3.17989][3.7734:7816]()
    /// Use `Self` to make a Fred request for series data, and return a `RegularTimeSeries`.
    pub fn data_without_transform(&self) -> Result<RegularTimeSeries<1>, Error> {
    [3.8092]
    [3.18065]
    let csv_path = data_path(
    root_path,
    self.data_type,
    country,
    series_id_stem,
    "csv",
    );
    let ts = TimeSeries::<1>::from_csv(&csv_path)
    .map_err(|err| {
    external(file!(), line!(), &err.to_string())
    })?;
    let rts = ts.try_into()
    .map_err(|err: time_series::error::Error| {
    external(file!(), line!(), &err.to_string())
    })?;
    Ok(rts)
    }
    pub fn data_from_fred(
    &self,
    country: Country) -> Result<RegularTimeSeries<1>, Error>
    {
  • replacement in src/lib.rs at line 870
    [3.7893][3.7893:7953]()
    &self.country.unwrap().to_string(),
    [3.7893]
    [3.19220]
    &country.to_string(),
  • edit in src/lib.rs at line 896
    [3.20015]
    [3.20015]
    &country.to_string(),
  • edit in src/lib.rs at line 898
    [3.20064][3.7954:8082]()
    // Expect country to be Some while building source.
    &self.country.unwrap().to_string(),
  • edit in src/lib.rs at line 907
    [3.20298]
    [3.20298]
    // pub fn data_without_transform(
    // &self,
    // country: Country,
    // root_path: &str) -> Result<RegularTimeSeries<1>, Error>
    // {
    // // Select the observation data from the FRED data.
    //
    // let observations = match Fred::series_observations(&self.series_id.to_string()) {
    // Ok(series_obs) => series_obs.observations,
    // Err(err) => { return Err(fred_error(file!(), line!(), &err.to_string())) },
    // };
    // // We need to build a RegularTimeSeries here, and then use RegularTimeSeries here
    // // to build csv file.
    // // Data is parsed into a RegularTimeSeries
    // // Want power to drop_first
    // let mut v = Vec::new();
    // // Drop the first n items
    // let skip = match self.drop_first {
    // None => 0,
    // Some(n) => n,
    // };
    // for (i, obs) in observations.iter().enumerate().skip(skip) {
    // let date = MonthlyDate::from_str(&obs.date)?;
    // let value: f32 = match obs.value.parse() {
    // Ok(n) => n,
    // Err(_) => {
    // let err = parse_fred_value_failed(
    // file!(),
    // line!(),
    // &self.data_type.to_string(),
    // // Expect country to be Some while building source.
    // &country.to_string(),
    // &self.series_id.to_string(),
    // &format!(
    // "{}, {}",
    // obs.date,
    // obs.value,
    // ),
    // i + 1,
    // );
    // self.soft_parse(observations, err);
    // unreachable!();
    // },
    // };
    // let date_point = DatePoint::<1>::new(date.0, [value]);
    // v.push(date_point)
    // }
    // let ts = TimeSeries::new(v);
    // let rts = match ts.try_into() {
    // Ok(rts) => {
    // rts
    // },
    // Err(_) => {
    // let err = expected_regular_time_series(
    // file!(),
    // line!(),
    // &country.to_string(),
    // &self.data_type.to_string(),
    // &self.series_id.to_string(),
    // );
    // self.soft_parse(observations, err);
    // unreachable!();
    // },
    // };
    // Ok(rts)
    // }
  • replacement in src/lib.rs at line 997
    [3.20783][3.20783:20852]()
    pub fn write_data(&self, root_path: &str) -> Result<(), Error> {
    [3.20783]
    [3.9222]
    pub fn write_data_to_file(&self, root_path: &str) -> Result<(), Error> {
    // When this function is used, Self.country should be Some.
    let rts = self.data_from_fred(self.country.unwrap())?;
  • replacement in src/lib.rs at line 1002
    [3.9223][3.8083:8133]()
    let rts = self.data_without_transform()?;
    [3.9223]
    [3.2044]
    // There should be a function here to get the data from Fred.
  • edit in src/lib.rs at line 1025
    [3.9959][3.8134:8198]()
    // Expect country to be Some while building source.
  • replacement in src/lib.rs at line 1059
    [3.22019][3.22019:22088]()
    pub fn write_meta(&self, root_path: &str) -> Result<(), Error> {
    [3.22019]
    [3.10432]
    pub fn write_meta_to_file(&self, root_path: &str) -> Result<(), Error> {
  • edit in src/error.rs at line 33
    [3.15196][3.15196:15217]()
    data_type: &str,
  • edit in src/error.rs at line 34
    [3.15236]
    [3.15236]
    data_type: &str,
  • edit in src/error.rs at line 41
    [3.15402]
    [3.15402]
    country,
  • edit in src/error.rs at line 43
    [3.15421][3.15421:15438]()
    country,
  • edit in src/error.rs at line 44
    [3.15457]
    [3.15457]
    ))
    }
    pub fn external(
    code_file: &str,
    code_line: u32,
    msg: &str) -> Error
    {
    Error(format!(
    "[ui_data:20:{}:{}] External: {}",
    code_file,
    code_line,
    msg,
  • edit in src/error.rs at line 320
    [3.19861]
    [3.19861]
  • edit in src/error.rs at line 341
    [3.20259]