Skip to main content

ferric_fred/
units.rs

1/// A units transformation FRED applies to a series' observations before
2/// returning them (the `units` request parameter).
3///
4/// A request-only, closed vocabulary — [`query_code`](Units::query_code) is the
5/// value sent to FRED. `#[non_exhaustive]` leaves room for FRED to add
6/// transformations without a breaking change.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum Units {
10    /// Levels — the data as reported (`lin`, FRED's default).
11    Levels,
12    /// Change from the previous period (`chg`).
13    Change,
14    /// Change from a year ago (`ch1`).
15    ChangeFromYearAgo,
16    /// Percent change from the previous period (`pch`).
17    PercentChange,
18    /// Percent change from a year ago (`pc1`).
19    PercentChangeFromYearAgo,
20    /// Compounded annual rate of change (`pca`).
21    CompoundedAnnualRateOfChange,
22    /// Continuously compounded rate of change (`cch`).
23    ContinuouslyCompoundedRateOfChange,
24    /// Continuously compounded annual rate of change (`cca`).
25    ContinuouslyCompoundedAnnualRateOfChange,
26    /// Natural log (`log`).
27    NaturalLog,
28}
29
30impl Units {
31    /// The FRED query code for this transformation.
32    pub fn query_code(self) -> &'static str {
33        match self {
34            Self::Levels => "lin",
35            Self::Change => "chg",
36            Self::ChangeFromYearAgo => "ch1",
37            Self::PercentChange => "pch",
38            Self::PercentChangeFromYearAgo => "pc1",
39            Self::CompoundedAnnualRateOfChange => "pca",
40            Self::ContinuouslyCompoundedRateOfChange => "cch",
41            Self::ContinuouslyCompoundedAnnualRateOfChange => "cca",
42            Self::NaturalLog => "log",
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn query_codes_match_fred() {
53        assert_eq!(Units::Levels.query_code(), "lin");
54        assert_eq!(Units::PercentChange.query_code(), "pch");
55        assert_eq!(Units::NaturalLog.query_code(), "log");
56    }
57}