[from|to]csv additions/refactoring.

Introduced flag to tell `from-to` / `to-csv` whether we want headers parsed and/or written.
This commit is contained in:
Andrés N. Robalino
2019-08-25 07:59:46 -05:00
parent de930daf33
commit 0e14ba86ae
7 changed files with 303 additions and 143 deletions

View File

@ -79,6 +79,7 @@ macro_rules! nu_error {
pub enum Stub<'a> {
FileWithContent(&'a str, &'a str),
FileWithContentToBeTrimmed(&'a str, &'a str),
EmptyFile(&'a str),
}
@ -124,14 +125,25 @@ impl Playground {
}
pub fn with_files(&mut self, files: Vec<Stub>) -> &mut Self {
let endl = line_ending();
files
.iter()
.map(|f| {
let mut path = PathBuf::from(&self.cwd);
let (file_name, contents) = match *f {
Stub::EmptyFile(name) => (name, "fake data"),
Stub::FileWithContent(name, content) => (name, content),
Stub::EmptyFile(name) => (name, "fake data".to_string()),
Stub::FileWithContent(name, content) => (name, content.to_string()),
Stub::FileWithContentToBeTrimmed(name, content) => (
name,
content
.lines()
.skip(1)
.map(|line| line.trim())
.collect::<Vec<&str>>()
.join(&endl),
),
};
path.push(file_name);
@ -176,6 +188,18 @@ pub fn file_contents(full_path: &str) -> String {
contents
}
pub fn line_ending() -> String {
#[cfg(windows)]
{
String::from("\r\n")
}
#[cfg(not(windows))]
{
String::from("\n")
}
}
pub fn normalize_string(input: &str) -> String {
#[cfg(windows)]
{