mirror of
https://github.com/nushell/nushell.git
synced 2025-04-26 14:18:19 +02:00
# Description As part of the refactor to split spans off of Value, this moves to using helper functions to create values, and using `.span()` instead of matching span out of Value directly. Hoping to get a few more helping hands to finish this, as there are a lot of commands to update :) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> Co-authored-by: WindSoilder <windsoilder@outlook.com>
115 lines
3.2 KiB
Rust
115 lines
3.2 KiB
Rust
use csv::{ReaderBuilder, Trim};
|
|
use nu_protocol::{IntoPipelineData, PipelineData, Record, ShellError, Span, Value};
|
|
|
|
fn from_delimited_string_to_value(
|
|
DelimitedReaderConfig {
|
|
separator,
|
|
comment,
|
|
quote,
|
|
escape,
|
|
noheaders,
|
|
flexible,
|
|
no_infer,
|
|
trim,
|
|
}: DelimitedReaderConfig,
|
|
s: String,
|
|
span: Span,
|
|
) -> Result<Value, csv::Error> {
|
|
let mut reader = ReaderBuilder::new()
|
|
.has_headers(!noheaders)
|
|
.flexible(flexible)
|
|
.delimiter(separator as u8)
|
|
.comment(comment.map(|c| c as u8))
|
|
.quote(quote as u8)
|
|
.escape(escape.map(|c| c as u8))
|
|
.trim(trim)
|
|
.from_reader(s.as_bytes());
|
|
|
|
let headers = if noheaders {
|
|
(1..=reader.headers()?.len())
|
|
.map(|i| format!("column{i}"))
|
|
.collect::<Vec<String>>()
|
|
} else {
|
|
reader.headers()?.iter().map(String::from).collect()
|
|
};
|
|
|
|
let mut rows = vec![];
|
|
for row in reader.records() {
|
|
let mut output_row = vec![];
|
|
for value in row?.iter() {
|
|
if no_infer {
|
|
output_row.push(Value::string(value.to_string(), span));
|
|
continue;
|
|
}
|
|
|
|
if let Ok(i) = value.parse::<i64>() {
|
|
output_row.push(Value::int(i, span));
|
|
} else if let Ok(f) = value.parse::<f64>() {
|
|
output_row.push(Value::float(f, span));
|
|
} else {
|
|
output_row.push(Value::string(value.to_string(), span));
|
|
}
|
|
}
|
|
rows.push(Value::record(
|
|
Record {
|
|
cols: headers.clone(),
|
|
vals: output_row,
|
|
},
|
|
span,
|
|
));
|
|
}
|
|
|
|
Ok(Value::list(rows, span))
|
|
}
|
|
|
|
pub(super) struct DelimitedReaderConfig {
|
|
pub separator: char,
|
|
pub comment: Option<char>,
|
|
pub quote: char,
|
|
pub escape: Option<char>,
|
|
pub noheaders: bool,
|
|
pub flexible: bool,
|
|
pub no_infer: bool,
|
|
pub trim: Trim,
|
|
}
|
|
|
|
pub(super) fn from_delimited_data(
|
|
config: DelimitedReaderConfig,
|
|
input: PipelineData,
|
|
name: Span,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let (concat_string, _span, metadata) = input.collect_string_strict(name)?;
|
|
|
|
Ok(from_delimited_string_to_value(config, concat_string, name)
|
|
.map_err(|x| ShellError::DelimiterError {
|
|
msg: x.to_string(),
|
|
span: name,
|
|
})?
|
|
.into_pipeline_data_with_metadata(metadata))
|
|
}
|
|
|
|
pub fn trim_from_str(trim: Option<Value>) -> Result<Trim, ShellError> {
|
|
match trim {
|
|
Some(v) => {
|
|
let span = v.span();
|
|
match v {
|
|
Value::String {val: item, ..} => match item.as_str() {
|
|
|
|
"all" => Ok(Trim::All),
|
|
"headers" => Ok(Trim::Headers),
|
|
"fields" => Ok(Trim::Fields),
|
|
"none" => Ok(Trim::None),
|
|
_ => Err(ShellError::TypeMismatch {
|
|
err_message:
|
|
"the only possible values for trim are 'all', 'headers', 'fields' and 'none'"
|
|
.into(),
|
|
span,
|
|
}),
|
|
}
|
|
_ => Ok(Trim::None),
|
|
}
|
|
}
|
|
_ => Ok(Trim::None),
|
|
}
|
|
}
|