fix: Retain unknown columns when mangling headers on CSV import

This commit is contained in:
Emilia Allison 2024-08-03 17:37:41 -04:00
parent ad57482dea
commit 108a2677e3
Signed by: emilia
GPG Key ID: 7A3F8997BFE894E0
1 changed files with 12 additions and 5 deletions

View File

@ -5,6 +5,8 @@ pub fn mangle_headers(data: &str) -> String {
for field in fields { for field in fields {
if let Some(f) = detect_field(field) { if let Some(f) = detect_field(field) {
modified_headers.push(f.to_string()); modified_headers.push(f.to_string());
} else {
log::debug!("Unk field: {:?}", field)
} }
} }
@ -12,22 +14,25 @@ pub fn mangle_headers(data: &str) -> String {
} }
fn detect_field(field: &str) -> Option<Field> { fn detect_field(field: &str) -> Option<Field> {
// NOTE: Don't return none! Consider refactor to -> Field later on.
// Returning none means a field will be dropped, which requires the user to manually drop
// columns from their picklist.
match field.trim().to_lowercase() { match field.trim().to_lowercase() {
x if x.contains("source") || x.contains("src") => match x { x if x.contains("source") || x.contains("src") => match x {
_ if x.contains("plate") => Some(Field::SourcePlate), _ if x.contains("plate") || x.contains("barcode") => Some(Field::SourcePlate),
_ if x.contains("well") => Some(Field::SourceWell), _ if x.contains("well") => Some(Field::SourceWell),
_ if x.contains("format") || x.contains("fmt") => Some(Field::SourceFormat), _ if x.contains("format") || x.contains("fmt") => Some(Field::SourceFormat),
_ => None, _ => Some(Field::Other(x)), // Retain unknown fields
}, },
x if x.contains("destination") || x.contains("dest") => match x { x if x.contains("destination") || x.contains("dest") => match x {
_ if x.contains("plate") => Some(Field::DestinationPlate), _ if x.contains("plate") || x.contains("barcode") => Some(Field::DestinationPlate),
_ if x.contains("well") => Some(Field::DestinationWell), _ if x.contains("well") => Some(Field::DestinationWell),
_ if x.contains("format") || x.contains("fmt") => Some(Field::DestinationFormat), _ if x.contains("format") || x.contains("fmt") => Some(Field::DestinationFormat),
_ => None, _ => Some(Field::Other(x)), // Retain unknown fields
}, },
x if x.contains("volume") => Some(Field::Volume), x if x.contains("volume") => Some(Field::Volume),
x if x.contains("concentration") => Some(Field::Concentration), x if x.contains("concentration") => Some(Field::Concentration),
_ => None, x => Some(Field::Other(x)), // Retain unknown fields
} }
} }
@ -40,6 +45,7 @@ enum Field {
DestinationFormat, DestinationFormat,
Volume, Volume,
Concentration, Concentration,
Other(String),
} }
impl ToString for Field { impl ToString for Field {
@ -53,6 +59,7 @@ impl ToString for Field {
Field::DestinationFormat => "destinationformat".to_string(), Field::DestinationFormat => "destinationformat".to_string(),
Field::Volume => "volume".to_string(), Field::Volume => "volume".to_string(),
Field::Concentration => "concentration".to_string(), Field::Concentration => "concentration".to_string(),
Field::Other(x) => x.to_string(),
} }
} }
} }