Support comma style option to preserves-tool

This commit is contained in:
Tony Garnock-Jones 2022-06-08 16:10:33 +02:00
parent 2e8d53c779
commit 7fdf50b963
2 changed files with 31 additions and 2 deletions

View File

@ -21,6 +21,8 @@ use preserves::value::Value;
use preserves::value::ViaCodec;
use preserves::value::Writer;
use preserves::value::text::writer::CommaStyle;
use std::iter::FromIterator;
use std::io;
use std::io::Read;
@ -46,6 +48,13 @@ enum OutputFormat {
Unquoted,
}
#[derive(ArgEnum, Clone, Debug)]
enum CommasFormat {
None,
Separating,
Terminating,
}
#[derive(ArgEnum, Clone, Debug)]
enum CompletionDialect {
Bash,
@ -69,6 +78,16 @@ impl From<Boolish> for bool {
}
}
impl From<CommasFormat> for CommaStyle {
fn from(commas: CommasFormat) -> Self {
match commas {
CommasFormat::None => CommaStyle::None,
CommasFormat::Separating => CommaStyle::Separating,
CommasFormat::Terminating => CommaStyle::Terminating,
}
}
}
#[derive(ArgEnum, Clone, Debug)]
enum SelectOutput {
Sequence,
@ -92,6 +111,9 @@ struct Convert {
#[clap(long)]
escape_spaces: bool,
#[clap(long, short, arg_enum, default_value = "none")]
commas: CommasFormat,
#[clap(long)]
limit: Option<usize>,
@ -381,7 +403,9 @@ fn convert(c: Convert) -> io::Result<()> {
let write_ann: bool = c.write_annotations.into();
let mut w: Box<dyn FnMut(&IOValue) -> io::Result<()>> = match c.output_format {
OutputFormat::Text => {
let mut t = TextWriter::new(io::stdout()).set_escape_spaces(c.escape_spaces);
let mut t = TextWriter::new(io::stdout())
.set_escape_spaces(c.escape_spaces)
.set_comma_style(c.commas.into());
if c.indent.into() {
t.indentation = 2;
}

View File

@ -12,7 +12,7 @@ use std::io;
use super::super::boundary as B;
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub enum CommaStyle {
None,
Separating,
@ -59,6 +59,11 @@ impl<W: io::Write> TextWriter<W> {
}
}
pub fn set_comma_style(mut self, v: CommaStyle) -> Self {
self.comma_style = v;
self
}
pub fn set_escape_spaces(mut self, v: bool) -> Self {
self.escape_spaces = v;
self