2022-01-01 02:39:58 +01:00
|
|
|
use nu_protocol::Value;
|
2022-01-24 05:52:19 +01:00
|
|
|
use std::collections::HashSet;
|
2022-01-01 02:39:58 +01:00
|
|
|
|
2023-04-26 20:56:10 +02:00
|
|
|
pub fn get_columns(input: &[Value]) -> Vec<String> {
|
2022-01-01 02:39:58 +01:00
|
|
|
let mut columns = vec![];
|
|
|
|
for item in input {
|
2023-04-14 20:51:38 +02:00
|
|
|
let Value::Record { cols, .. } = item else {
|
2022-03-07 14:39:02 +01:00
|
|
|
return vec![];
|
2023-04-14 20:51:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for col in cols {
|
|
|
|
if !columns.contains(col) {
|
|
|
|
columns.push(col.to_string());
|
|
|
|
}
|
2022-01-01 02:39:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
columns
|
|
|
|
}
|
2022-01-24 05:52:19 +01:00
|
|
|
|
2022-11-19 18:35:55 +01:00
|
|
|
// If a column doesn't exist in the input, return it.
|
|
|
|
pub fn nonexistent_column(inputs: Vec<String>, columns: Vec<String>) -> Option<String> {
|
|
|
|
let set: HashSet<String> = HashSet::from_iter(columns.iter().cloned());
|
2022-01-24 05:52:19 +01:00
|
|
|
|
|
|
|
for input in &inputs {
|
|
|
|
if set.contains(input) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-19 18:35:55 +01:00
|
|
|
return Some(input.clone());
|
2022-01-24 05:52:19 +01:00
|
|
|
}
|
2022-11-19 18:35:55 +01:00
|
|
|
None
|
2022-01-24 05:52:19 +01:00
|
|
|
}
|