decode_typed argument handling, README, output formats.
This commit is contained in:
parent
2b0ee72ceb
commit
b63b880959
4 changed files with 250 additions and 27 deletions
|
|
@ -17,9 +17,11 @@ repository = "fluffysquirrels/framed-rs"
|
|||
branch = "master"
|
||||
|
||||
[dependencies]
|
||||
clap = "^2.29.0"
|
||||
clap = { version = "^2.29.0", features = ["wrap_help"] }
|
||||
csv = "^1.0.0-beta.5"
|
||||
derive-error = "^0.0.4"
|
||||
framed = { path = "../framed" }
|
||||
serde_json = "^1.0.0"
|
||||
|
||||
[features]
|
||||
default = ["typed"]
|
||||
|
|
|
|||
|
|
@ -1,10 +1,84 @@
|
|||
# `framed_decode`
|
||||
|
||||
TODO.
|
||||
A command line utility to decode data encoded with the `framed` crate.
|
||||
|
||||
## framed-rs/bin/decode_typed
|
||||
|
||||
TODO.
|
||||
This command decodes a serialized struct encoded with the
|
||||
[`framed::typed`][typed] module. It requires the struct to implement
|
||||
the `serde::Deserialize` and `std::fmt::Debug` traits.
|
||||
|
||||
[typed]: https://docs.rs/framed/*/framed/typed/index.html
|
||||
|
||||
Here's an example. The main binary in crate `framed_test_type` writes
|
||||
a serialized and encoded `framed_test_type::Test` struct to stdout;
|
||||
first save that to a file:
|
||||
|
||||
```text
|
||||
~/framed-rs % cargo run -p framed_test_type > test_data
|
||||
|
||||
# The Test value is printed to stderr:
|
||||
test_type/main.rs: Sending sample value: Test {
|
||||
a: 1,
|
||||
b: 2
|
||||
}
|
||||
```
|
||||
|
||||
Set environment variables to configure the type to deserialize and the
|
||||
crate it's in, then pipe the test data to decode_typed. Note that the
|
||||
serialized data is not self describing, so if you configure the wrong
|
||||
type you will get garbage data out.
|
||||
|
||||
```text
|
||||
~/framed-rs % export DECODE_TYPE_CRATE_DIR=test_type;
|
||||
~/framed-rs % export DECODE_TYPE_NAME=framed_test_type::Test;
|
||||
~/framed-rs % < test_data bin/decode_typed
|
||||
# Some build output:
|
||||
Compiling framed_test_type v0.1.0 (file:///home/user/framed-rs/test_type)
|
||||
Compiling framed_decode v0.1.0 (file:///home/user/framed-rs/decode)
|
||||
|
||||
# decode logs each received value to stdout:
|
||||
decode/main.rs: received value of type framed_test_type::Test: Test {
|
||||
a: 1,
|
||||
b: 2
|
||||
}
|
||||
```
|
||||
|
||||
`decode_typed` first builds the type crate with `cargo rustc` then
|
||||
builds and runs `framed-rs/decode` linked to it. To configure the type
|
||||
crate build pass cargo arguments in the environment variable
|
||||
`DECODE_TYPE_CARGO_FLAGS` and rustc arguments in
|
||||
`DECODE_TYPE_RUSTC_FLAGS`. To configure the decode tool build pass
|
||||
cargo arguments in `DECODE_BIN_CARGO_FLAGS` and rustc arguments in
|
||||
`DECODE_BIN_RUSTC_FLAGS`.
|
||||
|
||||
`decode_typed` passes command line arguments to the `framed_decode`
|
||||
crate's binary; run `decode_typed --help` to see all the possible arguments:
|
||||
|
||||
```text
|
||||
~/framed-rs % bin/decode_typed --help
|
||||
|
||||
framed_decode 0.1.0
|
||||
Alex Helfet <alex.helfet@gmail.com>
|
||||
`framed` sends and receives data over lossy streams of bytes.
|
||||
|
||||
`framed_decode` is a command line tool to decode data encoded by the `framed` library.
|
||||
|
||||
USAGE:
|
||||
decode-5aa7aaf9ea94963a [OPTIONS]
|
||||
|
||||
FLAGS:
|
||||
-h, --help Prints help information
|
||||
-V, --version Prints version information
|
||||
|
||||
OPTIONS:
|
||||
--out-format <out-format>
|
||||
Output format type used to write data to stdout. [default: Debug] [values:
|
||||
Csv, Debug, Json]
|
||||
```
|
||||
|
||||
Note: you must install [`jq`][jq] and have it on your path.
|
||||
|
||||
On Ubuntu you can install it with: `sudo apt install jq`.
|
||||
|
||||
[jq]: https://stedolan.github.io/jq/
|
||||
|
|
|
|||
|
|
@ -1,11 +1,23 @@
|
|||
use clap;
|
||||
use csv;
|
||||
use framed;
|
||||
use serde_json;
|
||||
use std;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
/// Error in library `clap`.
|
||||
Clap(clap::Error),
|
||||
|
||||
/// Error in library `csv`.
|
||||
Csv(csv::Error),
|
||||
|
||||
/// Error in library `framed`.
|
||||
#[error(non_std)]
|
||||
Framed(framed::Error),
|
||||
|
||||
/// Error in library `serde_json`.
|
||||
SerdeJson(serde_json::Error),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
|
|
|||
|
|
@ -2,18 +2,29 @@
|
|||
|
||||
#[macro_use]
|
||||
extern crate clap;
|
||||
extern crate csv;
|
||||
#[macro_use]
|
||||
extern crate derive_error;
|
||||
extern crate framed;
|
||||
extern crate serde_json;
|
||||
|
||||
include!(env!("FRAMED_DECODE_DYNAMIC_RS"));
|
||||
|
||||
mod error;
|
||||
use error::{Error, Result};
|
||||
|
||||
// use clap::{App, Arg};
|
||||
use clap::Arg;
|
||||
use framed::typed::Receiver;
|
||||
use std::io::stdin;
|
||||
use std::io::{stdin, stdout};
|
||||
|
||||
arg_enum! {
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
enum OutputFormat {
|
||||
Csv,
|
||||
Debug,
|
||||
Json
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match try() {
|
||||
|
|
@ -24,16 +35,39 @@ fn main() {
|
|||
}
|
||||
|
||||
fn try() -> Result<()> {
|
||||
let app = app_from_crate!();
|
||||
let _matches = app.get_matches();
|
||||
let app = app_from_crate!()
|
||||
.arg(Arg::with_name("out-format")
|
||||
.long("out-format")
|
||||
.help("Output format type used to write data to stdout.")
|
||||
.takes_value(true)
|
||||
.empty_values(false)
|
||||
.possible_values(&OutputFormat::variants())
|
||||
.default_value("Debug")
|
||||
.case_insensitive(true));
|
||||
let matches = app.get_matches();
|
||||
let out_fmt = value_t!(matches, "out-format", OutputFormat)?;
|
||||
|
||||
let mut r = Receiver::<_, UserType>::new(stdin());
|
||||
|
||||
let mut csvw: Option<csv::Writer<_>> =
|
||||
match out_fmt {
|
||||
OutputFormat::Csv =>
|
||||
Some(csv::WriterBuilder::new().from_writer(stdout())),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
loop {
|
||||
let v = r.recv();
|
||||
match v {
|
||||
Ok(v) => {
|
||||
println!("decode/main.rs: received value of type {}: {:#?}",
|
||||
USER_TYPE_NAME, v);
|
||||
let res = r.recv();
|
||||
match res {
|
||||
Ok(v) => match out_fmt {
|
||||
OutputFormat::Csv => csvw.as_mut()
|
||||
.expect("Should've been initialized")
|
||||
.serialize(&v)?,
|
||||
OutputFormat::Debug => println!("{:#?}", v),
|
||||
OutputFormat::Json => {
|
||||
serde_json::to_writer(stdout(), &v)?;
|
||||
println!("");
|
||||
},
|
||||
},
|
||||
Err(framed::Error::EofBeforeFrame) => return Ok(()),
|
||||
Err(e) => return Err(Error::from(e)),
|
||||
|
|
|
|||
Loading…
Reference in a new issue