Add strip_annotations function and expose it in preserves-tool

This commit is contained in:
Tony Garnock-Jones 2021-09-30 12:41:29 +02:00
parent 29a882f953
commit 486a631e73
3 changed files with 49 additions and 9 deletions

View File

@ -56,11 +56,6 @@ This is the main tool. It can
-V, --version Print version information
OPTIONS:
--annotations <on/off>
[default: on] [possible values: disabled, no, n, off, 0, false,
enabled, yes, y, on, 1, true]
-i, --input-format <INPUT_FORMAT>
[default: auto-detect] [possible values: auto-detect, text, binary]
@ -74,12 +69,20 @@ This is the main tool. It can
-o, --output-format <OUTPUT_FORMAT>
[default: text] [possible values: text, binary, unquoted]
--read-annotations <on/off>
[default: on] [possible values: disabled, no, n, off, 0, false,
enabled, yes, y, on, 1, true]
--select <SELECT>
[default: *]
--select-output <SELECT_OUTPUT>
[default: sequence] [possible values: sequence, set]
--write-annotations <on/off>
[default: on] [possible values: disabled, no, n, off, 0, false,
enabled, yes, y, on, 1, true]
### `preserves-tool quote`
This subcommand reads chunks from standard input and outputs each one

View File

@ -108,7 +108,10 @@ struct Convert {
collect: bool,
#[clap(long, arg_enum, value_name = "on/off", default_value = "on")]
annotations: Boolish,
read_annotations: Boolish,
#[clap(long, arg_enum, value_name = "on/off", default_value = "on")]
write_annotations: Boolish,
}
#[derive(ArgEnum, Clone, Debug)]
@ -362,7 +365,8 @@ fn print_unquoted(v: &IOValue) {
}
fn convert(c: Convert) -> io::Result<()> {
let mut vs = ValueStream::new(c.input_format, c.annotations.into(), io::stdin());
let mut vs = ValueStream::new(c.input_format, c.read_annotations.into(), io::stdin());
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);
@ -370,14 +374,22 @@ fn convert(c: Convert) -> io::Result<()> {
t.indentation = 2;
}
Box::new(move |v| {
t.write(&mut IOValueDomainCodec, v)?;
if write_ann {
t.write(&mut IOValueDomainCodec, v)?;
} else {
t.write(&mut IOValueDomainCodec, &v.strip_annotations::<IOValue>())?;
}
println!();
Ok(())
})
}
OutputFormat::Binary => {
let mut p = PackedWriter::new(io::stdout());
Box::new(move |v| p.write(&mut IOValueDomainCodec, v))
Box::new(move |v| if write_ann {
p.write(&mut IOValueDomainCodec, v)
} else {
p.write(&mut IOValueDomainCodec, &v.strip_annotations::<IOValue>())
})
}
OutputFormat::Unquoted =>
Box::new(|v| Ok(print_unquoted(v))),

View File

@ -62,6 +62,10 @@ pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord {
self.value().fmt(f)
}
fn strip_annotations<M: NestedValue<Embedded = Self::Embedded>>(&self) -> M {
M::wrap(Annotations::empty(), self.value().strip_annotations())
}
fn copy_via<M: NestedValue, F, Err>(&self, f: &mut F) -> Result<M, Err>
where
F: FnMut(&Self::Embedded) -> Result<Value<M>, Err>
@ -917,6 +921,27 @@ impl<N: NestedValue> Value<N> {
self.as_embedded().ok_or_else(|| self.expected(ExpectedKind::Embedded))
}
pub fn strip_annotations<M: NestedValue<Embedded = N::Embedded>>(&self) -> Value<M> {
match self {
Value::Boolean(b) => Value::Boolean(*b),
Value::Float(f) => Value::Float(f.clone()),
Value::Double(d) => Value::Double(d.clone()),
Value::SignedInteger(n) => Value::SignedInteger(n.clone()),
Value::String(s) => Value::String(s.clone()),
Value::ByteString(v) => Value::ByteString(v.clone()),
Value::Symbol(v) => Value::Symbol(v.clone()),
Value::Record(r) => Value::Record(Record(r.fields_vec().iter().map(
|a| a.strip_annotations()).collect())),
Value::Sequence(v) => Value::Sequence(v.iter().map(
|a| a.strip_annotations()).collect()),
Value::Set(v) => Value::Set(v.iter().map(
|a| a.strip_annotations()).collect()),
Value::Dictionary(v) => Value::Dictionary(v.iter().map(
|(a,b)| (a.strip_annotations(), b.strip_annotations())).collect()),
Value::Embedded(d) => Value::Embedded(d.clone()),
}
}
pub fn copy_via<M: NestedValue, F, Err>(&self, f: &mut F) -> Result<Value<M>, Err>
where
F: FnMut(&N::Embedded) -> Result<Value<M>, Err>