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