1
1
use std:: fmt:: Debug ;
2
2
use std:: { collections:: HashMap , fmt:: Display } ;
3
3
4
+ use crate :: error:: RoverError ;
4
5
use crate :: utils:: table:: { self , cell, row} ;
5
6
6
7
use ansi_term:: {
@@ -13,6 +14,7 @@ use rover_client::operations::graph::publish::GraphPublishResponse;
13
14
use rover_client:: operations:: subgraph:: list:: SubgraphListResponse ;
14
15
use rover_client:: operations:: subgraph:: publish:: SubgraphPublishResponse ;
15
16
use rover_client:: shared:: { CheckResponse , FetchResponse , GraphRef , SdlType } ;
17
+ use serde:: Serialize ;
16
18
use serde_json:: { json, Value } ;
17
19
use termimad:: MadSkin ;
18
20
@@ -192,7 +194,7 @@ impl RoverOutput {
192
194
}
193
195
}
194
196
195
- pub fn get_internal_json ( & self ) -> Option < Value > {
197
+ pub ( crate ) fn get_internal_json ( & self ) -> Value {
196
198
match self {
197
199
RoverOutput :: DocsList ( shortlinks) => {
198
200
let mut shortlink_vec = vec ! [ ] ;
@@ -201,32 +203,69 @@ impl RoverOutput {
201
203
json ! ( { "slug" : shortlink_slug, "description" : shortlink_description } ) ,
202
204
) ;
203
205
}
204
- Some ( json ! ( { "shortlinks" : shortlink_vec } ) )
206
+ json ! ( { "shortlinks" : shortlink_vec } )
205
207
}
206
- RoverOutput :: FetchResponse ( fetch_response) => Some ( json ! ( fetch_response) ) ,
207
- RoverOutput :: CoreSchema ( csdl) => Some ( json ! ( { "core_schema" : csdl } ) ) ,
208
+ RoverOutput :: FetchResponse ( fetch_response) => json ! ( fetch_response) ,
209
+ RoverOutput :: CoreSchema ( csdl) => json ! ( { "core_schema" : csdl } ) ,
208
210
RoverOutput :: GraphPublishResponse {
209
211
graph_ref : _,
210
212
publish_response,
211
- } => Some ( json ! ( publish_response) ) ,
213
+ } => json ! ( publish_response) ,
212
214
RoverOutput :: SubgraphPublishResponse {
213
215
graph_ref : _,
214
216
subgraph : _,
215
217
publish_response,
216
- } => Some ( json ! ( publish_response) ) ,
217
- RoverOutput :: SubgraphList ( list_response) => Some ( json ! ( list_response) ) ,
218
- RoverOutput :: CheckResponse ( check_response) => Some ( json ! ( check_response) ) ,
219
- RoverOutput :: VariantList ( variants) => Some ( json ! ( { "variants" : variants } ) ) ,
220
- RoverOutput :: Profiles ( profiles) => Some ( json ! ( { "profiles" : profiles } ) ) ,
218
+ } => json ! ( publish_response) ,
219
+ RoverOutput :: SubgraphList ( list_response) => json ! ( list_response) ,
220
+ RoverOutput :: CheckResponse ( check_response) => json ! ( check_response) ,
221
+ RoverOutput :: VariantList ( variants) => json ! ( { "variants" : variants } ) ,
222
+ RoverOutput :: Profiles ( profiles) => json ! ( { "profiles" : profiles } ) ,
221
223
RoverOutput :: Introspection ( introspection_response) => {
222
- Some ( json ! ( { "introspection_response" : introspection_response } ) )
224
+ json ! ( { "introspection_response" : introspection_response } )
223
225
}
224
- RoverOutput :: Markdown ( markdown_string) => Some ( json ! ( { "markdown" : markdown_string } ) ) ,
225
- RoverOutput :: None => None ,
226
+ RoverOutput :: Markdown ( markdown_string) => json ! ( { "markdown" : markdown_string } ) ,
227
+ RoverOutput :: None => json ! ( null ) ,
226
228
}
227
229
}
228
230
}
229
231
232
+ #[ derive( Debug , Clone , Serialize ) ]
233
+ pub ( crate ) struct JsonOutput {
234
+ pub ( crate ) data : JsonData ,
235
+ pub ( crate ) error : Value ,
236
+ }
237
+
238
+ impl JsonOutput {
239
+ pub ( crate ) fn success ( output : RoverOutput ) -> Value {
240
+ let json_output = JsonOutput {
241
+ data : JsonData {
242
+ inner : output. get_internal_json ( ) ,
243
+ success : true ,
244
+ } ,
245
+ error : json ! ( null) ,
246
+ } ;
247
+ json ! ( json_output)
248
+ }
249
+
250
+ pub ( crate ) fn error ( error : RoverError ) -> Value {
251
+ let json_output = JsonOutput {
252
+ data : JsonData {
253
+ inner : json ! ( null) ,
254
+ success : false ,
255
+ } ,
256
+ error : json ! ( error) ,
257
+ } ;
258
+ json ! ( json_output)
259
+ }
260
+ }
261
+
262
+ #[ derive( Debug , Clone , Serialize ) ]
263
+ pub ( crate ) struct JsonData {
264
+ #[ serde( flatten) ]
265
+ pub ( crate ) inner : Value ,
266
+ pub ( crate ) success : bool ,
267
+ }
268
+
230
269
fn print_descriptor ( descriptor : impl Display ) {
231
270
if atty:: is ( Stream :: Stdout ) {
232
271
eprintln ! ( "{}: \n " , Style :: new( ) . bold( ) . paint( descriptor. to_string( ) ) ) ;
0 commit comments