Skip to content

Commit 54e4087

Browse files
committed
Support SMILE
1 parent fca64c6 commit 54e4087

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

Cargo.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ rmpv = "1.0.0"
4444
serde = "1.0.137"
4545
serde-hjson = "0.9.1"
4646
serde-protobuf = "0.8.1"
47+
serde-smile = "0.1.4"
4748
serde_cbor = "0.11.2"
4849
serde_json = "1.0.81"
4950
serde_yaml = "0.9.16"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ architectures.
4242
| TOML | ✔️ | ✔️ |
4343
| Raw (plain text) | ✔️ | ✔️ |
4444
| CSV | ✔️ | ✔️ |
45+
| SMILE | ✔️ | ✔️ |

src/bin/rq.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ pub struct Options {
6565
/// Input is a series of YAML documents.
6666
#[structopt(short = "y", long = "input-yaml")]
6767
pub flag_input_yaml: bool,
68+
/// Input is formatted as SMILE
69+
#[structopt(short = "s", long = "input-smile")]
70+
pub flag_input_smile: bool,
6871

6972
#[structopt(short = "A", long = "output-avro")]
7073
pub flag_output_avro: Option<String>,
@@ -84,6 +87,8 @@ pub struct Options {
8487
pub flag_output_toml: bool,
8588
#[structopt(short = "Y", long = "output-yaml")]
8689
pub flag_output_yaml: bool,
90+
#[structopt(short = "S", long = "output-smile")]
91+
pub flag_output_smile: bool,
8792

8893
#[structopt(short = "l", long = "log")]
8994
pub flag_log: Option<String>,
@@ -179,6 +184,9 @@ fn run(args: &Options) -> rq::error::Result<()> {
179184
} else if args.flag_input_yaml {
180185
let source = rq::value::yaml::source(&mut input);
181186
run_source(args, source)
187+
} else if args.flag_input_smile {
188+
let source = rq::value::smile::source(&mut input)?;
189+
run_source(args, source)
182190
} else if args.flag_input_raw {
183191
let source = rq::value::raw::source(&mut input);
184192
run_source(args, source)
@@ -277,6 +285,9 @@ where
277285
rq::value::yaml::sink,
278286
rq::value::yaml::sink
279287
)
288+
} else if args.flag_output_smile {
289+
let sink = rq::value::smile::sink(&mut output)?;
290+
run_source_sink(source, sink)
280291
} else if args.flag_output_raw {
281292
let sink = rq::value::raw::sink(&mut output);
282293
run_source_sink(source, sink)
@@ -593,6 +604,30 @@ mod test {
593604
assert!(a.flag_output_cbor);
594605
}
595606

607+
#[test]
608+
fn test_docopt_input_smile() {
609+
let a = parse_args(&["rq", "-s"]);
610+
assert!(a.flag_input_smile);
611+
}
612+
613+
#[test]
614+
fn test_docopt_input_smile_long() {
615+
let a = parse_args(&["rq", "--input-smile"]);
616+
assert!(a.flag_input_smile);
617+
}
618+
619+
#[test]
620+
fn test_docopt_output_smile() {
621+
let a = parse_args(&["rq", "-S"]);
622+
assert!(a.flag_output_smile);
623+
}
624+
625+
#[test]
626+
fn test_docopt_output_smile_long() {
627+
let a = parse_args(&["rq", "--output-smile"]);
628+
assert!(a.flag_output_smile);
629+
}
630+
596631
#[test]
597632
fn test_docopt_input_protobuf() {
598633
let a = parse_args(&["rq", "-p", ".foo.Bar"]);

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub enum Error {
4646
TomlDeserialize(#[cause] toml::de::Error),
4747
#[fail(display = "TOML serialize error")]
4848
TomlSerialize(#[cause] toml::ser::Error),
49+
#[fail(display = "SMILE error")]
50+
Smile(#[cause] serde_smile::Error),
4951
#[fail(display = "glob error")]
5052
Glob(#[cause] glob::GlobError),
5153
#[fail(display = "glob pattern error")]
@@ -142,6 +144,7 @@ gen_from!(serde_yaml::Error, Yaml);
142144
gen_from!(yaml_rust::ScanError, YamlScan);
143145
gen_from!(toml::de::Error, TomlDeserialize);
144146
gen_from!(toml::ser::Error, TomlSerialize);
147+
gen_from!(serde_smile::Error, Smile);
145148
gen_from!(glob::GlobError, Glob);
146149
gen_from!(glob::PatternError, GlobPattern);
147150
gen_from!(csv::Error, Csv);

src/value/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod json;
1313
pub mod messagepack;
1414
pub mod protobuf;
1515
pub mod raw;
16+
pub mod smile;
1617
pub mod toml;
1718
pub mod yaml;
1819

src/value/smile.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use crate::error;
2+
3+
use crate::value;
4+
use serde;
5+
use serde_smile;
6+
use std::fmt;
7+
use std::io;
8+
9+
pub struct Source<R>(
10+
serde_smile::de::StreamDeserializer<
11+
'static,
12+
serde_smile::de::IoRead<io::BufReader<R>>,
13+
value::Value,
14+
>,
15+
)
16+
where
17+
R: io::Read;
18+
19+
pub struct Sink<W>(serde_smile::ser::Serializer<W>)
20+
where
21+
W: io::Write;
22+
23+
#[inline]
24+
pub fn source<R>(r: R) -> error::Result<Source<R>>
25+
where
26+
R: io::Read,
27+
{
28+
Ok(Source(
29+
serde_smile::de::Deserializer::new(serde_smile::de::IoRead::new(io::BufReader::new(r)))?
30+
.into_iter(),
31+
))
32+
}
33+
34+
#[inline]
35+
pub fn sink<W>(w: W) -> error::Result<Sink<W>>
36+
where
37+
W: io::Write,
38+
{
39+
Ok(Sink(
40+
serde_smile::ser::Serializer::builder()
41+
.shared_strings(true)
42+
.build(w)?,
43+
))
44+
}
45+
46+
impl<R> value::Source for Source<R>
47+
where
48+
R: io::Read,
49+
{
50+
#[inline]
51+
fn read(&mut self) -> error::Result<Option<value::Value>> {
52+
Ok(self.0.next().transpose()?)
53+
}
54+
}
55+
56+
impl<W> value::Sink for Sink<W>
57+
where
58+
W: io::Write,
59+
{
60+
#[inline]
61+
fn write(&mut self, v: value::Value) -> error::Result<()> {
62+
Ok(serde::Serialize::serialize(&v, &mut self.0)?)
63+
}
64+
}
65+
66+
impl<R> fmt::Debug for Source<R>
67+
where
68+
R: io::Read,
69+
{
70+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71+
f.debug_struct("SmileSource").finish()
72+
}
73+
}
74+
75+
impl<W> fmt::Debug for Sink<W>
76+
where
77+
W: io::Write,
78+
{
79+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80+
f.debug_struct("SmileSink").finish()
81+
}
82+
}

0 commit comments

Comments
 (0)