Skip to main content

ecbdp_api/parameter/
data.rs

1use std::fmt::Display;
2use chrono::{DateTime, TimeZone};
3use crate::time;
4
5
6#[derive(Clone, Copy, Debug, Default)]
7pub enum PeriodFormat {
8    /// YYYY for annual data (e.g., 2013)
9    Annual,
10    /// YYYY-S[1-2] for semi-annual data (e.g., 2013-S1)
11    SemiAnnual,
12    /// YYYY-Q[1-4] for quarterly data (e.g., 2013-Q1)
13    Quarterly,
14    /// YYYY-MM for monthly data (e.g., 2013-01)
15    Monthly,
16    /// YYYY-W[01-53] for weekly data (e.g., 2013-W01)
17    Weekly,
18    #[default]
19    /// YYYY-MM-DD for daily data (e.g., 2013-01-01)
20    Daily,
21}
22
23
24#[derive(Clone, Copy, Debug, Default)]
25/// Using the detail parameter, it is possible to specify the desired amount of information to be returned by the web service.
26pub enum Detail {
27    #[default]
28    /// The data (Time series and Observations) and the Attributes will be returned. This is the default.
29    Full,
30    /// The Attributes will be excluded from the returned message.
31    DataOnly,
32    /// Only the Time series will be returned, excluding the Attributes and the Observations.
33    /// This can be used to list Time series that match a certain query without returning the actual data.
34    SeriesKeysOnly,
35    /// The Time series will be returned, including the Attributes, but the Observations will not.
36    NoData,
37}
38
39impl Display for Detail {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            Self::Full => write!(f, "full"),
43            Self::DataOnly => write!(f, "dataonly"),
44            Self::SeriesKeysOnly => write!(f, "serieskeysonly"),
45            Self::NoData => write!(f, "nodata"),
46        }
47    }
48}
49
50
51#[derive(Clone, Copy, Debug, Default)]
52pub enum Format {
53    #[default]
54    JSONData,
55}
56
57impl Display for Format {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            Self::JSONData => write!(f, "jsondata"),
61        }
62    }
63}
64
65
66#[derive(Clone, Debug)]
67/// Parameter types for `data` queries.
68pub enum DataParameter<Tz>
69where
70    Tz: TimeZone,
71    <Tz as TimeZone>::Offset: std::fmt::Display,
72{
73    /// It is possible to define a date range for which Observations are to be returned by using the startPeriod and/or endPeriod parameters.
74    StartPeriod { datetime: DateTime<Tz>, period_format: PeriodFormat, },
75    /// It is possible to define a date range for which Observations are to be returned by using the startPeriod and/or endPeriod parameters.
76    EndPeriod { datetime: DateTime<Tz>, period_format: PeriodFormat, },
77    /// By supplying a percent-encoded ISO 8601 timestamp for the updatedAfter parameter, it is possible to retrieve the
78    /// latest version of changed values in the database after a certain point in time (i.e., updates and revisions).
79    /// This will include:
80    /// - The Observations that have been added since the supplied timestamp
81    /// - TYhe Observations that have been revised since the supplied timestamp
82    /// - The Observations that have been deleted since the supplied timestamp
83    UpdatedAfter { datetime: DateTime<Tz>, },
84    /// Using the detail parameter, it is possible to specify the desired amount of information to be returned by the web service.
85    Detail { detail: Detail, },
86    /// Using the firstNObservations parameter, it is possible to specify the maximum number of Observations to be returned for
87    /// each of the matching Time series, starting from the first Observation (firstNObservations).
88    FirstNObservations { n: usize, },
89    /// Using the lastNObservations parameter, it is possible to specify the maximum number of Observations to be returned for
90    /// each of the matching Time series, counting back from the most recent Observation (lastNObservations).
91    LastNObservations { n: usize, },
92    /// Using the includeHistory parameter, you can instruct the web service to return previous versions of the matching data.
93    /// This allows you to see how the data have evolved over time (i.e., see when new data were released, revised or deleted).
94    /// Possible options are:
95    /// - `false`: Only the version currently in production will be returned. This is the default.
96    /// - `true`: The version currently in production and all previous versions will be returned.
97    IncludeHistory { yes: bool, },
98    /// Using the format parameter, you can instruct the web service to return data in different formats.
99    Format { format: Format, },
100}
101
102impl<Tz> Display for DataParameter<Tz>
103where
104    Tz: TimeZone,
105    <Tz as TimeZone>::Offset: std::fmt::Display,
106{
107    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108        match self {
109            Self::StartPeriod { datetime, period_format } => {
110                write!(f, "startPeriod={}", time::datetime_to_ecb_period(datetime, *period_format))
111            },
112            Self::EndPeriod { datetime, period_format } => {
113                write!(f, "endPeriod={}", time::datetime_to_ecb_period(datetime, *period_format))
114            },
115            Self::UpdatedAfter { datetime } => write!(f, "updatedAfter={}", time::percent_encode_datetime(datetime)),
116            Self::Detail { detail } => write!(f, "detail={}", detail.to_string()),
117            Self::FirstNObservations { n } => write!(f, "firstNObservations={n}"),
118            Self::LastNObservations { n } => write!(f, "lastNObservations={n}"),
119            Self::IncludeHistory { yes } => write!(f, "includeHistory={}", yes),
120            Self::Format { format } => write!(f, "format={}", format.to_string()),
121        }
122    }
123}