Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.

Commit 1e356d4

Browse files
authored
Merge pull request killercup#29 from killercup/json-stdout
Json stdout and other updates
2 parents bf8bb49 + d790e30 commit 1e356d4

File tree

7 files changed

+58
-10
lines changed

7 files changed

+58
-10
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ rust:
66

77
matrix:
88
include:
9-
- rust: 1.29.1
9+
- rust: 1.30.1
1010
env: CLIPPY=YESPLEASE
1111
before_script: rustup component add clippy-preview
1212
script: cargo clippy --all-targets --all -- -D warnings
13-
- rust: 1.29.1
13+
- rust: 1.30.1
1414
env: RUSTFMT=YESPLEASE
1515
before_script: rustup component add rustfmt-preview
1616
script: cargo fmt --all -- --check

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ assert_fs = "0.9.0"
2727
predicates = "0.9.0"
2828
convey_derive = { version = "0.2", path = "convey_derive" }
2929
rand = "0.5.5"
30+
structopt = "0.2"

examples/cli.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
extern crate failure;
2+
extern crate structopt;
3+
#[macro_use]
4+
extern crate serde_derive;
25
#[macro_use]
36
extern crate convey;
47

58
use convey::{
69
components::{newline, text},
710
human, json,
811
};
12+
use structopt::StructOpt;
13+
14+
/// Demo stuff
15+
#[derive(StructOpt)]
16+
struct Cli {
17+
/// Output JSON instead of human readable messages
18+
#[structopt(long = "json")]
19+
json: bool,
20+
}
921

1022
fn main() -> Result<(), failure::Error> {
11-
let mut out = convey::new()
12-
.add_target(json::file("target/foo.log")?)
13-
.add_target(human::stdout()?);
23+
let args = Cli::from_args();
24+
let mut out = if args.json {
25+
convey::new().add_target(json::stdout()?)
26+
} else {
27+
convey::new().add_target(human::stdout()?)
28+
};
1429

1530
let x = 42;
1631

@@ -25,5 +40,30 @@ fn main() -> Result<(), failure::Error> {
2540
span!(intense = true, ["intense text",]),
2641
]))?;
2742

43+
#[derive(Serialize)]
44+
struct ErrorMessage {
45+
code: i32,
46+
name: String,
47+
message: String,
48+
}
49+
50+
impl convey::Render for ErrorMessage {
51+
render_for_humans!(self -> [
52+
span!(fg = "white", bg = "black", [text(self.code.to_string()), text(" "),]),
53+
span!(fg = "red", bg = "black", [text(&self.name),]),
54+
newline(),
55+
text("> "),
56+
text(&self.message),
57+
]);
58+
59+
render_json!();
60+
}
61+
62+
out.print(&ErrorMessage {
63+
code: 42,
64+
name: String::from("error"),
65+
message: String::from("Oh god no"),
66+
})?;
67+
2868
Ok(())
2969
}

src/components/span.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn span() -> Span {
1111
macro_rules! __inner_span {
1212
($span:ident, $attr:ident = $val:expr, $($token:tt)*) => {
1313
$span = $span.$attr($val)?;
14-
__inner_span!($span, $($token)*);
14+
$crate::__inner_span!($span, $($token)*);
1515
};
1616
($span:ident, [$($item:expr,)*]) => {
1717
$(
@@ -46,7 +46,7 @@ macro_rules! span {
4646
($($token:tt)*) => {
4747
{
4848
let mut the_span = $crate::components::span();
49-
__inner_span!(the_span, $($token)*);
49+
$crate::__inner_span!(the_span, $($token)*);
5050
the_span
5151
}
5252
};

src/human.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ macro_rules! render_for_humans {
205205
};
206206
($self:ident -> [$($item:expr,)*]) => {
207207
fn render_for_humans(&$self, fmt: &mut $crate::human::Formatter) -> Result<(), $crate::Error> {
208-
let span = span!([ $( $item, )* ]);
208+
let span = $crate::span!([ $( $item, )* ]);
209209
span.render_for_humans(fmt)?;
210210
Ok(())
211211
}

src/json.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ use std::path::Path;
77
use std::sync::Arc;
88
use {Error, Target};
99

10+
/// Construct a new JSON output target that writes to stdout
11+
pub fn stdout() -> Result<Target, Error> {
12+
use std::io::{stdout, BufWriter};
13+
14+
let formatter = Formatter::init_with(|| Ok(BufWriter::new(stdout())))?;
15+
Ok(Target::json(formatter))
16+
}
17+
1018
/// Create a new JSON output that writes to a file
1119
pub fn file<T: AsRef<Path>>(name: T) -> Result<Target, Error> {
1220
let path = name.as_ref().to_path_buf();

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
1515
#![warn(missing_docs)]
1616

17+
extern crate crossbeam_channel;
1718
extern crate failure;
18-
#[macro_use]
1919
extern crate failure_derive;
20-
extern crate crossbeam_channel;
2120
extern crate serde;
2221
extern crate termcolor;
2322
#[macro_use]

0 commit comments

Comments
 (0)