-
Notifications
You must be signed in to change notification settings - Fork 72
/
mod.rs
297 lines (273 loc) · 10.3 KB
/
mod.rs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use std::{
collections::{HashMap, HashSet},
fmt::Debug,
num::{ParseFloatError, ParseIntError},
};
use bytes::Bytes;
use chrono::{DateTime, LocalResult, ParseError as ChronoParseError, TimeZone as _, Utc};
use ordered_float::NotNan;
use snafu::{ResultExt, Snafu};
use super::datetime::{datetime_to_utc, TimeZone};
#[cfg(test)]
mod tests;
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Snafu)]
pub enum ConversionError {
#[snafu(display("Unknown conversion name {:?}", name))]
UnknownConversion { name: String },
}
/// `Conversion` is a place-holder for a type conversion operation, to convert
/// from a plain `Bytes` into another type. The inner type of every `Value`
/// variant is represented here.
#[derive(Clone, Debug)]
pub enum Conversion {
Bytes,
Integer,
Float,
Boolean,
Timestamp(TimeZone),
TimestampFmt(String, TimeZone),
TimestampTzFmt(String),
}
#[derive(Debug, Eq, PartialEq, Snafu)]
pub enum Error {
#[snafu(display("Invalid boolean value {:?}", s))]
BoolParse { s: String },
#[snafu(display("Invalid integer {:?}: {}", s, source))]
IntParse { s: String, source: ParseIntError },
#[snafu(display("NaN number not supported {:?}", s))]
NanFloat { s: String },
#[snafu(display("Invalid floating point number {:?}: {}", s, source))]
FloatParse { s: String, source: ParseFloatError },
#[snafu(
display("Invalid timestamp {:?}: {}", s, source),
visibility(pub(super))
)]
TimestampParse { s: String, source: ChronoParseError },
#[snafu(display("No matching timestamp format found for {:?}", s))]
AutoTimestampParse { s: String },
}
/// Helper function to parse a conversion map and check against a list of names
///
/// # Errors
///
/// See `fn Conversion::parse`.
#[allow(clippy::implicit_hasher)]
pub fn parse_check_conversion_map(
types: &HashMap<String, String>,
names: &[impl AsRef<str>],
tz: TimeZone,
) -> Result<HashMap<String, Conversion>, ConversionError> {
// Check if any named type references a nonexistent field
let names = names
.iter()
.map(std::convert::AsRef::as_ref)
.collect::<HashSet<_>>();
for name in types.keys() {
if !names.contains(name.as_str()) {
tracing::warn!(
message = "Field was specified in the types but is not a valid field name.",
field = &name[..]
);
}
}
parse_conversion_map(types, tz)
}
/// Helper function to parse a mapping of conversion descriptions into actual Conversion values.
///
/// # Errors
///
/// See `fn Conversion::parse`.
#[allow(clippy::implicit_hasher)]
pub fn parse_conversion_map(
types: &HashMap<String, String>,
tz: TimeZone,
) -> Result<HashMap<String, Conversion>, ConversionError> {
types
.iter()
.map(|(field, typename)| Conversion::parse(typename, tz).map(|conv| (field.clone(), conv)))
.collect()
}
impl Conversion {
/// Convert the string into a type conversion. The following
/// conversion names are supported:
///
/// * `"asis"`, `"bytes"`, or `"string"` => As-is (no conversion)
/// * `"int"` or `"integer"` => Signed integer
/// * `"float"` => Floating point number
/// * `"bool"` or `"boolean"` => Boolean
/// * `"timestamp"` => Timestamp, guessed using a set of formats
/// * `"timestamp|FORMAT"` => Timestamp using the given format
///
/// # Errors
///
/// Returns an error if the conversion name is unknown.
pub fn parse(s: impl AsRef<str>, tz: TimeZone) -> Result<Self, ConversionError> {
let s = s.as_ref();
let mut split = s.splitn(2, '|').map(str::trim);
match (split.next(), split.next()) {
(Some("asis" | "bytes" | "string"), None) => Ok(Self::Bytes),
(Some("integer" | "int"), None) => Ok(Self::Integer),
(Some("float"), None) => Ok(Self::Float),
(Some("bool" | "boolean"), None) => Ok(Self::Boolean),
(Some("timestamp"), None) => Ok(Self::Timestamp(tz)),
(Some("timestamp"), Some(fmt)) => {
// DateTime<Utc> can only convert timestamps without
// time zones, and DateTime<FixedOffset> can only
// convert with tone zones, so this has to distinguish
// between the two types of formats.
if format_has_zone(fmt) {
Ok(Self::TimestampTzFmt(fmt.into()))
} else {
Ok(Self::TimestampFmt(fmt.into(), tz))
}
}
_ => Err(ConversionError::UnknownConversion { name: s.into() }),
}
}
/// Use this `Conversion` variant to turn the given `bytes` into a new `T`.
///
/// # Errors
///
/// Returns errors from the underlying conversion functions. See `enum Error`.
#[allow(clippy::trait_duplication_in_bounds)] // appears to be a false positive
pub fn convert<T>(&self, bytes: Bytes) -> Result<T, Error>
where
T: From<Bytes> + From<i64> + From<NotNan<f64>> + From<bool> + From<DateTime<Utc>>,
{
Ok(match self {
Self::Bytes => bytes.into(),
Self::Integer => {
let s = String::from_utf8_lossy(&bytes);
s.parse::<i64>()
.with_context(|_| IntParseSnafu { s })?
.into()
}
Self::Float => {
let s = String::from_utf8_lossy(&bytes);
let parsed = s
.parse::<f64>()
.with_context(|_| FloatParseSnafu { s: s.clone() })?;
if !parsed.is_normal() {
return Err(Error::NanFloat {
s: format!("Invalid float \"{s}\": not a normal f64 number"),
});
}
let f = NotNan::new(parsed).map_err(|_| Error::NanFloat { s: s.to_string() })?;
f.into()
}
Self::Boolean => parse_bool(&String::from_utf8_lossy(&bytes))?.into(),
Self::Timestamp(tz) => parse_timestamp(*tz, &String::from_utf8_lossy(&bytes))?.into(),
Self::TimestampFmt(format, tz) => {
let s = String::from_utf8_lossy(&bytes);
let dt = tz
.datetime_from_str(&s, format)
.context(TimestampParseSnafu { s })?;
datetime_to_utc(&dt).into()
}
Self::TimestampTzFmt(format) => {
let s = String::from_utf8_lossy(&bytes);
let dt = DateTime::parse_from_str(&s, format)
.with_context(|_| TimestampParseSnafu { s })?;
datetime_to_utc(&dt).into()
}
})
}
}
/// Parse a string into a native `bool`. The built in `bool::from_str`
/// only handles two cases, `"true"` and `"false"`. We want to be able
/// to convert from a more diverse set of strings. In particular, the
/// following set of source strings are allowed:
///
/// * `"true"`, `"t"`, `"yes"`, `"y"` (all case-insensitive), and
/// non-zero integers all convert to `true`.
///
/// * `"false"`, `"f"`, `"no"`, `"n"` (all case-insensitive), and `"0"`
/// all convert to `false`.
///
/// # Errors
///
/// Any input value besides those described above result in a parse error.
fn parse_bool(s: &str) -> Result<bool, Error> {
match s {
"true" | "t" | "yes" | "y" => Ok(true),
"false" | "f" | "no" | "n" | "0" => Ok(false),
_ => {
if let Ok(n) = s.parse::<isize>() {
Ok(n != 0)
} else {
// Do the case conversion only if simple matches fail,
// since this operation can be expensive.
match s.to_lowercase().as_str() {
"true" | "t" | "yes" | "y" => Ok(true),
"false" | "f" | "no" | "n" => Ok(false),
_ => Err(Error::BoolParse { s: s.into() }),
}
}
}
}
}
/// Does the format specifier have a time zone option?
fn format_has_zone(fmt: &str) -> bool {
fmt.contains("%Z")
|| fmt.contains("%z")
|| fmt.contains("%:z")
|| fmt.contains("%#z")
|| fmt.contains("%+")
}
/// The list of allowed "automatic" timestamp formats with assumed local time zone
const TIMESTAMP_LOCAL_FORMATS: &[&str] = &[
"%F %T", // YYYY-MM-DD HH:MM:SS
"%v %T", // DD-Mmm-YYYY HH:MM:SS
"%FT%T", // ISO 8601 / RFC 3339 without TZ
"%m/%d/%Y:%T", // ???
"%a, %d %b %Y %T", // RFC 822/2822 without TZ
"%a %d %b %T %Y", // `date` command output without TZ
"%A %d %B %T %Y", // `date` command output without TZ, long names
"%a %b %e %T %Y", // ctime format
];
/// The list of allowed "automatic" timestamp formats with time zones
const TIMESTAMP_TZ_FORMATS: &[&str] = &[
"%+", // ISO 8601 / RFC 3339
"%a %d %b %T %Z %Y", // `date` command output
"%a %d %b %T %z %Y", // `date` command output, numeric TZ
"%a %d %b %T %#z %Y", // `date` command output, numeric TZ
"%d/%b/%Y:%T %z", // Common Log
];
fn parse_unix_timestamp(timestamp_str: &str) -> LocalResult<DateTime<Utc>> {
if let Ok(seconds_since_epoch) = timestamp_str.parse::<i64>() {
Utc.timestamp_opt(seconds_since_epoch, 0)
} else {
LocalResult::None
}
}
/// Parse a string into a timestamp using one of a set of formats
///
/// # Errors
///
/// Returns an error if the string could not be matched by one of the
/// predefined timestamp formats.
fn parse_timestamp(tz: TimeZone, s: &str) -> Result<DateTime<Utc>, Error> {
for format in TIMESTAMP_LOCAL_FORMATS {
if let Ok(result) = tz.datetime_from_str(s, format) {
return Ok(result);
}
}
// This is equivalent to the "%s" format.
if let LocalResult::Single(result) = parse_unix_timestamp(s) {
return Ok(result);
}
// This also handles "%FT%TZ" formats.
if let Ok(result) = DateTime::parse_from_rfc3339(s) {
return Ok(datetime_to_utc(&result));
}
if let Ok(result) = DateTime::parse_from_rfc2822(s) {
return Ok(datetime_to_utc(&result));
}
for format in TIMESTAMP_TZ_FORMATS {
if let Ok(result) = DateTime::parse_from_str(s, format) {
return Ok(datetime_to_utc(&result));
}
}
Err(Error::AutoTimestampParse { s: s.into() })
}