Permalink
Browse files

Use type-erased serde_json Formatter

  • Loading branch information...
sevagh committed Aug 30, 2017
1 parent 51f1e46 commit 5228d7f76d05c831217f6f3865555ad4319144d2
Showing with 74 additions and 48 deletions.
  1. +23 −12 Cargo.lock
  2. +7 −6 Cargo.toml
  3. +7 −11 src/decode.rs
  4. +0 −2 src/discovery.rs
  5. +29 −14 src/{newline_pretty_formatter.rs → formatter.rs}
  6. +6 −1 src/main.rs
  7. +2 −2 stream_delimit/Cargo.toml
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,6 +1,6 @@
[package]
name = "pq"
version = "0.9.0"
version = "0.9.2"
authors = ["Sevag Hanssian <sevag.hanssian@gmail.com>"]
description = "jq for protobuf"
repository = "https://github.com/sevagh/pq"
@@ -10,14 +10,15 @@ license = "MIT"
keywords = ["protobuf", "serde"]
[dependencies]
serde = "1.0.2"
serde = "1.0.10"
serde-value = "0.5.0"
erased_serde_json = { git = "https://github.com/sevagh/erased-serde-json" }
serde_json = "1.0.2"
serde-protobuf = "0.6.0"
protobuf = "1.2.1"
libc = "0.2.22"
stream_delimit = { path = "stream_delimit", version = "0.4.0" }
clap = "2.24.2"
protobuf = "1.4.1"
libc = "0.2.26"
stream_delimit = { path = "stream_delimit", version = "0.4.1" }
clap = "2.26.0"
error-chain = "0.10.0"
[features]
View
@@ -6,7 +6,7 @@ use serde_protobuf::de::Deserializer;
use serde_protobuf::descriptor::Descriptors;
use serde_value::Value;
use protobuf::CodedInputStream;
use newline_pretty_formatter::NewlineFormatter;
use formatter::CustomFormatter;
use errors::*;
pub struct PqrsDecoder<'a> {
@@ -40,15 +40,11 @@ impl<'a> PqrsDecoder<'a> {
let value = Value::deserialize(&mut deserializer).chain_err(
|| "Deser error",
)?;
if is_tty {
let formatter = NewlineFormatter::default();
Ok(value
.serialize(&mut Serializer::with_formatter(out, formatter))
.chain_err(|| "Ser error")?)
} else {
Ok(value.serialize(&mut Serializer::new(out)).chain_err(
|| "Serr error",
)?)
}
Ok(value
.serialize(&mut Serializer::with_formatter(
out,
CustomFormatter::new(is_tty),
))
.chain_err(|| "Ser error")?)
}
}
View
@@ -1,5 +1,3 @@
extern crate protobuf;
use std::env;
use std::fs::{File, read_dir};
use std::path::PathBuf;
@@ -1,31 +1,46 @@
use serde_json::ser::{Formatter, PrettyFormatter};
use serde_json::ser::{Formatter, PrettyFormatter, CompactFormatter};
use erased_serde_json::Formatter as ErasedFormatter;
use std::io::{self, Write};
use std::boxed::Box;
#[derive(Default)]
pub struct NewlineFormatter {
pretty: PrettyFormatter<'static>,
pub struct CustomFormatter {
formatter: Box<ErasedFormatter>,
depth: usize,
}
impl Formatter for NewlineFormatter {
impl CustomFormatter {
pub fn new(is_tty: bool) -> Self {
let f: Box<ErasedFormatter> = if is_tty {
Box::new(PrettyFormatter::default())
} else {
Box::new(CompactFormatter)
};
CustomFormatter {
formatter: f,
depth: 0,
}
}
}
impl Formatter for CustomFormatter {
fn begin_array<W: ?Sized + Write>(&mut self, w: &mut W) -> io::Result<()> {
self.pretty.begin_array(w)
self.formatter.begin_array(w)
}
fn end_array<W: ?Sized + Write>(&mut self, w: &mut W) -> io::Result<()> {
self.pretty.end_array(w)
self.formatter.end_array(w)
}
fn begin_array_value<W: ?Sized + Write>(&mut self, w: &mut W, first: bool) -> io::Result<()> {
self.pretty.begin_array_value(w, first)
self.formatter.begin_array_value(w, first)
}
fn end_array_value<W: ?Sized + Write>(&mut self, w: &mut W) -> io::Result<()> {
self.pretty.end_array_value(w)
self.formatter.end_array_value(w)
}
fn begin_object<W: ?Sized + Write>(&mut self, w: &mut W) -> io::Result<()> {
self.depth += 1;
self.pretty.begin_object(w)
self.formatter.begin_object(w)
}
fn end_object<W: ?Sized + Write>(&mut self, w: &mut W) -> io::Result<()> {
self.pretty.end_object(w).and_then(|()| {
self.formatter.end_object(w).and_then(|()| {
self.depth -= 1;
if self.depth == 0 {
w.write_all(b"\n")
@@ -35,12 +50,12 @@ impl Formatter for NewlineFormatter {
})
}
fn begin_object_key<W: ?Sized + Write>(&mut self, w: &mut W, first: bool) -> io::Result<()> {
self.pretty.begin_object_key(w, first)
self.formatter.begin_object_key(w, first)
}
fn begin_object_value<W: ?Sized + Write>(&mut self, w: &mut W) -> io::Result<()> {
self.pretty.begin_object_value(w)
self.formatter.begin_object_value(w)
}
fn end_object_value<W: ?Sized + Write>(&mut self, w: &mut W) -> io::Result<()> {
self.pretty.end_object_value(w)
self.formatter.end_object_value(w)
}
}
View
@@ -5,6 +5,7 @@ extern crate clap;
extern crate libc;
extern crate protobuf;
extern crate serde;
extern crate erased_serde_json;
extern crate serde_protobuf;
extern crate serde_value;
extern crate serde_json;
@@ -13,12 +14,16 @@ extern crate stream_delimit;
extern crate error_chain;
mod discovery;
mod newline_pretty_formatter;
mod formatter;
mod decode;
mod commands;
mod errors {
error_chain!{
foreign_links {
Io(::std::io::Error);
}
links {
StreamDelimit(::stream_delimit::error::Error, ::stream_delimit::error::ErrorKind);
}
@@ -1,6 +1,6 @@
[package]
name = "stream_delimit"
version = "0.4.0"
version = "0.4.1"
authors = ["Sevag Hanssian <sevag.hanssian@gmail.com>"]
description = "length delimited protobuf stream separator"
repository = "https://github.com/sevagh/pq/tree/master/stream_delimit"
@@ -9,7 +9,7 @@ license = "MIT"
keywords = ["protobuf", "serde"]
[dependencies]
libc = "0.2.22"
libc = "0.2.26"
kafka = { version = "0.6.2", optional = true }
error-chain = "0.10.0"

0 comments on commit 5228d7f

Please sign in to comment.