@@ -6,7 +6,7 @@ const SYSTEM_SCALARS: &[&str] = &["Int", "Float", "String", "Boolean", "ID"];
6
6
const FEDERATION_SCALARS : & [ & str ] =& [ "Any" ] ;
7
7
8
8
/// Options for SDL export
9
- #[ derive( Debug , Copy , Clone , Default ) ]
9
+ #[ derive( Debug , Copy , Clone ) ]
10
10
pub struct SDLExportOptions {
11
11
sorted_fields : bool ,
12
12
sorted_arguments : bool ,
@@ -15,6 +15,24 @@ pub struct SDLExportOptions {
15
15
prefer_single_line_descriptions : bool ,
16
16
include_specified_by : bool ,
17
17
compose_directive : bool ,
18
+ use_space_ident : bool ,
19
+ indent_width : u8 ,
20
+ }
21
+
22
+ impl Default for SDLExportOptions {
23
+ fn default ( ) ->Self {
24
+ Self {
25
+ sorted_fields : false ,
26
+ sorted_arguments : false ,
27
+ sorted_enum_values : false ,
28
+ federation : false ,
29
+ prefer_single_line_descriptions : false ,
30
+ include_specified_by : false ,
31
+ compose_directive : false ,
32
+ use_space_ident : false ,
33
+ indent_width : 2 ,
34
+ }
35
+ }
18
36
}
19
37
20
38
impl SDLExportOptions {
@@ -89,6 +107,23 @@ impl SDLExportOptions {
89
107
..self
90
108
}
91
109
}
110
+
111
+ /// Use spaces for indentation instead of tabs
112
+ pub fn use_space_ident ( self ) ->Self {
113
+ Self {
114
+ use_space_ident : true ,
115
+ ..self
116
+ }
117
+ }
118
+
119
+ /// Set the number of spaces to use for each indentation level (default: 2).
120
+ /// Only applies when `use_space_indent` is true
121
+ pub fn indent_width ( self , width : u8 ) ->Self {
122
+ Self {
123
+ indent_width : width,
124
+ ..self
125
+ }
126
+ }
92
127
}
93
128
94
129
impl Registry {
@@ -108,7 +143,6 @@ impl Registry {
108
143
}
109
144
110
145
self . export_type ( ty, & mut sdl, & options) ;
111
- writeln ! ( sdl) . ok ( ) ;
112
146
}
113
147
114
148
self . directives . values ( ) . for_each ( |directive|{
@@ -157,8 +191,13 @@ impl Registry {
157
191
158
192
if options. federation {
159
193
writeln ! ( sdl, "extend schema @link(" ) . ok ( ) ;
160
- writeln ! ( sdl, "\t url:\" https://specs.apollo.dev/federation/v2.3\" ," ) . ok ( ) ;
161
- writeln ! ( sdl, "\t import: [\" @key\" ,\" @tag\" ,\" @shareable\" ,\" @inaccessible\" ,\" @override\" ,\" @external\" ,\" @provides\" ,\" @requires\" ,\" @composeDirective\" ,\" @interfaceObject\" ]" ) . ok ( ) ;
194
+ writeln ! (
195
+ sdl,
196
+ "{}url:\" https://specs.apollo.dev/federation/v2.3\" ," ,
197
+ tab( & options)
198
+ )
199
+ . ok ( ) ;
200
+ writeln ! ( sdl, "{}import: [\" @key\" ,\" @tag\" ,\" @shareable\" ,\" @inaccessible\" ,\" @override\" ,\" @external\" ,\" @provides\" ,\" @requires\" ,\" @composeDirective\" ,\" @interfaceObject\" ]" , tab( & options) ) . ok ( ) ;
162
201
writeln ! ( sdl, ")" ) . ok ( ) ;
163
202
164
203
if options. compose_directive {
@@ -176,23 +215,23 @@ impl Registry {
176
215
} ) ;
177
216
for ( url, directives) in compose_directives{
178
217
writeln ! ( sdl, "extend schema @link(" ) . ok ( ) ;
179
- writeln ! ( sdl, "\t url :\" {}\" " , url) . ok ( ) ;
180
- writeln ! ( sdl, "\t import : [{}]" , directives. join( "," ) ) . ok ( ) ;
218
+ writeln ! ( sdl, "{}url :\" {}\" " , tab ( & options ) , url) . ok ( ) ;
219
+ writeln ! ( sdl, "{}import : [{}]" , tab ( & options ) , directives. join( "," ) ) . ok ( ) ;
181
220
writeln ! ( sdl, ")" ) . ok ( ) ;
182
221
for namein directives{
183
- writeln ! ( sdl, "\t @composeDirective(name: {})" , name) . ok ( ) ;
222
+ writeln ! ( sdl, "{} @composeDirective(name: {})" , tab ( & options ) , name) . ok ( ) ;
184
223
}
185
224
writeln ! ( sdl) . ok ( ) ;
186
225
}
187
226
}
188
227
} else {
189
228
writeln ! ( sdl, "schema {{" ) . ok ( ) ;
190
- writeln ! ( sdl, "\t query : {}" , self . query_type) . ok ( ) ;
229
+ writeln ! ( sdl, "{}query : {}" , tab ( & options ) , self . query_type) . ok ( ) ;
191
230
if let Some ( mutation_type) =self . mutation_type . as_deref ( ) {
192
- writeln ! ( sdl, "\t mutation : {}" , mutation_type) . ok ( ) ;
231
+ writeln ! ( sdl, "{}mutation : {}" , tab ( & options ) , mutation_type) . ok ( ) ;
193
232
}
194
233
if let Some ( subscription_type) =self . subscription_type . as_deref ( ) {
195
- writeln ! ( sdl, "\t subscription : {}" , subscription_type) . ok ( ) ;
234
+ writeln ! ( sdl, "{}subscription : {}" , tab ( & options ) , subscription_type) . ok ( ) ;
196
235
}
197
236
writeln ! ( sdl, "}}" ) . ok ( ) ;
198
237
}
@@ -223,7 +262,7 @@ impl Registry {
223
262
}
224
263
225
264
if !field. args . is_empty ( ) {
226
- write ! ( sdl, "\t {}(" , field. name) . ok ( ) ;
265
+ write ! ( sdl, "{}{}(" , tab ( & options ) , field. name) . ok ( ) ;
227
266
228
267
let mut args = field. args . values ( ) . collect :: < Vec < _ > > ( ) ;
229
268
if options. sorted_arguments {
@@ -243,7 +282,7 @@ impl Registry {
243
282
}
244
283
245
284
if need_multiline{
246
- write ! ( sdl, "\t \t " ) . ok ( ) ;
285
+ write ! ( sdl, "{0}{0}" , tab ( options ) ) . ok ( ) ;
247
286
} else if i !=0 {
248
287
sdl. push ( ' ' ) ;
249
288
}
@@ -266,11 +305,11 @@ impl Registry {
266
305
}
267
306
268
307
if need_multiline{
269
- sdl . push_str ( "\n \t " ) ;
308
+ write ! ( sdl , "\n {}" , tab ( & options ) ) . ok ( ) ;
270
309
}
271
310
write ! ( sdl, "): {}" , field. ty) . ok ( ) ;
272
311
} else {
273
- write ! ( sdl, "\t {} : {}" , field. name, field. ty) . ok ( ) ;
312
+ write ! ( sdl, "{}{} : {}" , tab ( & options ) , field. name, field. ty) . ok ( ) ;
274
313
}
275
314
276
315
write_deprecated ( sdl, & field. deprecation ) ;
@@ -352,7 +391,7 @@ impl Registry {
352
391
write ! ( sdl, " {}" , directive. sdl( ) ) . ok ( ) ;
353
392
}
354
393
355
- writeln ! ( sdl) . ok ( ) ;
394
+ writeln ! ( sdl, " \n " ) . ok ( ) ;
356
395
}
357
396
}
358
397
MetaType :: Object {
@@ -437,7 +476,7 @@ impl Registry {
437
476
438
477
writeln ! ( sdl, " {{" ) . ok ( ) ;
439
478
Self :: export_fields ( sdl, fields. values ( ) , options) ;
440
- writeln ! ( sdl, "}}" ) . ok ( ) ;
479
+ writeln ! ( sdl, "}}\n " ) . ok ( ) ;
441
480
}
442
481
MetaType :: Interface {
443
482
name,
@@ -482,7 +521,7 @@ impl Registry {
482
521
483
522
writeln ! ( sdl, " {{" ) . ok ( ) ;
484
523
Self :: export_fields ( sdl, fields. values ( ) , options) ;
485
- writeln ! ( sdl, "}}" ) . ok ( ) ;
524
+ writeln ! ( sdl, "}}\n " ) . ok ( ) ;
486
525
}
487
526
MetaType :: Enum {
488
527
name,
@@ -522,7 +561,7 @@ impl Registry {
522
561
if let Some ( description) =& value. description {
523
562
write_description ( sdl, options, 1 , description) ;
524
563
}
525
- write ! ( sdl, "\t {}" , value. name) . ok ( ) ;
564
+ write ! ( sdl, "{}{}" , tab ( & options ) , value. name) . ok ( ) ;
526
565
write_deprecated ( sdl, & value. deprecation ) ;
527
566
528
567
if options. federation {
@@ -542,7 +581,7 @@ impl Registry {
542
581
writeln ! ( sdl) . ok ( ) ;
543
582
}
544
583
545
- writeln ! ( sdl, "}}" ) . ok ( ) ;
584
+ writeln ! ( sdl, "}}\n " ) . ok ( ) ;
546
585
}
547
586
MetaType :: InputObject {
548
587
name,
@@ -587,7 +626,7 @@ impl Registry {
587
626
if let Some ( ref description) =& field. description {
588
627
write_description ( sdl, options, 1 , description) ;
589
628
}
590
- sdl. push ( '\t' ) ;
629
+ write ! ( sdl, "{}" , tab ( options ) ) . ok ( ) ;
591
630
write_input_value ( sdl, field) ;
592
631
if options. federation {
593
632
if field. inaccessible {
@@ -603,7 +642,7 @@ impl Registry {
603
642
writeln ! ( sdl) . ok ( ) ;
604
643
}
605
644
606
- writeln ! ( sdl, "}}" ) . ok ( ) ;
645
+ writeln ! ( sdl, "}}\n " ) . ok ( ) ;
607
646
}
608
647
MetaType :: Union {
609
648
name,
@@ -641,7 +680,7 @@ impl Registry {
641
680
write ! ( sdl, " | {}" , ty) . ok ( ) ;
642
681
}
643
682
}
644
- writeln ! ( sdl) . ok ( ) ;
683
+ writeln ! ( sdl, " \n " ) . ok ( ) ;
645
684
}
646
685
}
647
686
}
@@ -670,7 +709,7 @@ fn write_description(
670
709
level : usize ,
671
710
description : & str ,
672
711
) {
673
- let tabs =" \t " . repeat ( level) ;
712
+ let tabs =tab ( options ) . repeat ( level) ;
674
713
675
714
if options. prefer_single_line_descriptions && !description. contains ( '\n' ) {
676
715
let description = description. replace ( '"' , r#"\""# ) ;
@@ -730,6 +769,14 @@ fn escape_string(s: &str) -> String {
730
769
res
731
770
}
732
771
772
+ fn tab ( options : & SDLExportOptions ) ->String {
773
+ if options. use_space_ident {
774
+ " " . repeat ( options. indent_width . into ( ) )
775
+ } else {
776
+ "\t " . to_string ( )
777
+ }
778
+ }
779
+
733
780
#[ cfg( test) ]
734
781
mod tests{
735
782
use super :: * ;