11extern crate failure;
22#[ macro_use]
3+ extern crate structopt;
4+ #[ macro_use]
5+ extern crate serde_derive;
6+ #[ macro_use]
37extern crate convey;
48
9+ use structopt:: StructOpt ;
510use convey:: {
611 components:: { newline, text} ,
712 human, json,
813} ;
914
15+ /// Demo stuff
16+ #[ derive( StructOpt ) ]
17+ struct Cli {
18+ /// Output JSON instead of human readable messages
19+ #[ structopt( long = "json" ) ]
20+ json : bool ,
21+ }
22+
1023fn main ( ) -> Result < ( ) , failure:: Error > {
11- let mut out = convey:: new ( )
12- . add_target ( json:: file ( "target/foo.log" ) ?)
13- . add_target ( human:: stdout ( ) ?) ;
24+ let args = Cli :: from_args ( ) ;
25+ let mut out = if args. json {
26+ convey:: new ( ) . add_target ( json:: stdout ( ) ?)
27+ } else {
28+ convey:: new ( ) . add_target ( human:: stdout ( ) ?)
29+ } ;
1430
1531 let x = 42 ;
1632
@@ -25,5 +41,30 @@ fn main() -> Result<(), failure::Error> {
2541 span!( intense = true , [ "intense text" , ] ) ,
2642 ] ) ) ?;
2743
44+ #[ derive( Serialize ) ]
45+ struct ErrorMessage {
46+ code : i32 ,
47+ name : String ,
48+ message : String ,
49+ }
50+
51+ impl convey:: Render for ErrorMessage {
52+ render_for_humans ! ( self -> [
53+ span!( fg = "white" , bg = "black" , [ text( self . code. to_string( ) ) , text( " " ) , ] ) ,
54+ span!( fg = "red" , bg = "black" , [ text( & self . name) , ] ) ,
55+ newline( ) ,
56+ text( "> " ) ,
57+ text( & self . message) ,
58+ ] ) ;
59+
60+ render_json ! ( ) ;
61+ }
62+
63+ out. print ( & ErrorMessage {
64+ code : 42 ,
65+ name : String :: from ( "error" ) ,
66+ message : String :: from ( "Oh god no" ) ,
67+ } ) ?;
68+
2869 Ok ( ( ) )
2970}
0 commit comments