Do not allocate when parsing CSV headers

This commit is contained in:
Kerollmops 2022-06-14 10:13:48 +02:00
parent ce90fc628a
commit 048e174efb
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -108,7 +108,7 @@ impl<W: io::Write + io::Seek> DocumentBatchBuilder<W> {
.headers()? .headers()?
.into_iter() .into_iter()
.map(parse_csv_header) .map(parse_csv_header)
.map(|(k, t)| (this.index.insert(&k), t)) .map(|(k, t)| (this.index.insert(k), t))
.collect::<BTreeMap<_, _>>(); .collect::<BTreeMap<_, _>>();
for (i, record) in records.into_records().enumerate() { for (i, record) in records.into_records().enumerate() {
@ -161,16 +161,16 @@ enum AllowedType {
Number, Number,
} }
fn parse_csv_header(header: &str) -> (String, AllowedType) { fn parse_csv_header(header: &str) -> (&str, AllowedType) {
// if there are several separators we only split on the last one. // if there are several separators we only split on the last one.
match header.rsplit_once(':') { match header.rsplit_once(':') {
Some((field_name, field_type)) => match field_type { Some((field_name, field_type)) => match field_type {
"string" => (field_name.to_string(), AllowedType::String), "string" => (field_name, AllowedType::String),
"number" => (field_name.to_string(), AllowedType::Number), "number" => (field_name, AllowedType::Number),
// if the pattern isn't reconized, we keep the whole field. // if the pattern isn't reconized, we keep the whole field.
_otherwise => (header.to_string(), AllowedType::String), _otherwise => (header, AllowedType::String),
}, },
None => (header.to_string(), AllowedType::String), None => (header, AllowedType::String),
} }
} }