11extern crate failure;
2+ extern crate structopt;
3+ #[ macro_use]
4+ extern crate serde_derive;
25#[ macro_use]
36extern crate convey;
47
58use 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
1022fn 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}
0 commit comments