@@ -10,10 +10,11 @@ use std::sync::Mutex;
10
10
use clap:: Args ;
11
11
use contracts:: requires;
12
12
use kdl:: { KdlDocument , KdlNode } ;
13
- use miette:: { IntoDiagnostic , SourceSpan } ;
13
+ use miette:: { IntoDiagnostic , LabeledSpan , NamedSource , SourceOffset , SourceSpan } ;
14
14
use strum:: EnumIs ;
15
15
use thiserror:: Error ;
16
16
17
+ use usage:: parse:: config:: SpecConfig ;
17
18
use usage:: { SchemaCmd , Spec } ;
18
19
use xx:: { context, file} ;
19
20
@@ -52,10 +53,7 @@ impl Markdown {
52
53
fn inject_file ( & self , inject : & Path ) -> miette:: Result < ( ) > {
53
54
let raw = file:: read_to_string ( inject) . into_diagnostic ( ) ?;
54
55
context:: set_load_root ( inject. parent ( ) . unwrap ( ) . to_path_buf ( ) ) ;
55
- let out = raw
56
- . lines ( )
57
- . map ( |line| line. parse ( ) )
58
- . collect :: < miette:: Result < Vec < UsageMdDirective > > > ( ) ?
56
+ let out = parse_readme_directives ( inject, & raw ) ?
59
57
. into_iter ( )
60
58
. try_fold ( UsageMdContext :: new ( ) , |ctx, d| d. run ( ctx) ) ?
61
59
. out
@@ -159,6 +157,7 @@ enum UsageMdDirective {
159
157
GlobalFlags ,
160
158
CommandIndex ,
161
159
Commands ,
160
+ Config ,
162
161
EndToken ,
163
162
Plain ( String ) ,
164
163
}
@@ -175,6 +174,7 @@ impl Display for UsageMdDirective {
175
174
UsageMdDirective :: GlobalFlags => write ! ( f, "<!-- [USAGE] global_flags -->" ) ,
176
175
UsageMdDirective :: CommandIndex => write ! ( f, "<!-- [USAGE] command_index -->" ) ,
177
176
UsageMdDirective :: Commands => write ! ( f, "<!-- [USAGE] commands -->" ) ,
177
+ UsageMdDirective :: Config => write ! ( f, "<!-- [USAGE] config -->" ) ,
178
178
UsageMdDirective :: EndToken => write ! ( f, "<!-- [USAGE] -->" ) ,
179
179
UsageMdDirective :: Plain ( line) => write ! ( f, "{}" , line) ,
180
180
}
@@ -291,6 +291,13 @@ impl UsageMdDirective {
291
291
print_commands ( & ctx, & [ & spec. cmd ] ) ?;
292
292
ctx. push ( format ! ( "<!-- [USAGE] -->" ) ) ;
293
293
}
294
+ UsageMdDirective :: Config => {
295
+ ctx. plain = false ;
296
+ let spec = ctx. spec . as_ref ( ) . unwrap ( ) ;
297
+ ctx. push ( self . to_string ( ) ) ;
298
+ print_config ( & ctx, & spec. config ) ?;
299
+ ctx. push ( format ! ( "<!-- [USAGE] -->" ) ) ;
300
+ }
294
301
UsageMdDirective :: EndToken => {
295
302
ctx. plain = true ;
296
303
}
@@ -354,6 +361,26 @@ fn print_commands(ctx: &UsageMdContext, cmds: &[&SchemaCmd]) -> miette::Result<(
354
361
Ok ( ( ) )
355
362
}
356
363
364
+ fn print_config ( ctx : & UsageMdContext , config : & SpecConfig ) -> miette:: Result < ( ) > {
365
+ for ( key, prop) in & config. props {
366
+ ctx. push ( format ! ( "### `{key}`" , key = key) ) ;
367
+ if let Some ( env) = & prop. env {
368
+ ctx. push ( format ! ( "env: `{env}`" , env = env) ) ;
369
+ }
370
+ if !prop. default . is_null ( ) {
371
+ ctx. push ( format ! ( "default: `{default}`" , default = prop. default ) ) ;
372
+ }
373
+ if let Some ( help) = & prop. help {
374
+ ctx. push ( format ! ( "{help}" , help = help) ) ;
375
+ }
376
+ if let Some ( long_help) = & prop. long_help {
377
+ ctx. push ( format ! ( "{long_help}" , long_help = long_help) ) ;
378
+ }
379
+ ctx. push ( "Used by commnds: global|*" . to_string ( ) ) ;
380
+ }
381
+ Ok ( ( ) )
382
+ }
383
+
357
384
#[ derive( Error , Diagnostic , Debug ) ]
358
385
#[ error( "Error parsing markdown directive" ) ]
359
386
#[ diagnostic( ) ]
@@ -367,11 +394,12 @@ struct MarkdownError {
367
394
err_span : SourceSpan ,
368
395
}
369
396
370
- impl FromStr for UsageMdDirective {
371
- type Err = miette :: Error ;
372
- fn from_str ( line : & str ) -> Result < Self , Self :: Err > {
397
+ fn parse_readme_directives ( path : & Path , full : & str ) -> miette :: Result < Vec < UsageMdDirective > > {
398
+ let mut directives = vec ! [ ] ;
399
+ for ( line_num , line ) in full . lines ( ) . enumerate ( ) {
373
400
if line == "<!-- [USAGE] -->" {
374
- return Ok ( UsageMdDirective :: EndToken ) ;
401
+ directives. push ( UsageMdDirective :: EndToken ) ;
402
+ continue ;
375
403
}
376
404
let directive = if let Some ( x) = regex ! ( r#"<!-- \[USAGE\] (.*) -->"# ) . captures ( line) {
377
405
let doc: KdlDocument = x. get ( 1 ) . unwrap ( ) . as_str ( ) . parse ( ) ?;
@@ -400,13 +428,28 @@ impl FromStr for UsageMdDirective {
400
428
"usage_overview" => UsageMdDirective :: UsageOverview ,
401
429
"global_args" => UsageMdDirective :: GlobalArgs ,
402
430
"global_flags" => UsageMdDirective :: GlobalFlags ,
431
+ //"config" => UsageMdDirective::Config,
403
432
"command_index" => UsageMdDirective :: CommandIndex ,
404
433
"commands" => UsageMdDirective :: Commands ,
405
- _ => Err ( err ( "unknown directive type" . into ( ) , * node. name ( ) . span ( ) ) ) ?,
434
+ // k => Err(err("unknown directive type".into(), *node.name().span()))?,
435
+ // k => diagnostic!(source_code=doc.to_string(),
436
+ // source_code= "unknown directive type".into(),
437
+ // // err_span= *node.name().span()
438
+ // }),
439
+ k => Err ( miette ! (
440
+ labels = vec![ LabeledSpan :: new(
441
+ Some ( format!( "unknown directive type: {k}" ) ) ,
442
+ SourceOffset :: from_location( full, line_num + 1 , 14 ) . offset( ) ,
443
+ node. name( ) . span( ) . len( ) ,
444
+ ) ] ,
445
+ "Error parsing markdown directive" ,
446
+ )
447
+ . with_source_code ( NamedSource :: new ( path. to_string_lossy ( ) , full. to_string ( ) ) ) ) ?,
406
448
}
407
449
} else {
408
450
UsageMdDirective :: Plain ( line. into ( ) )
409
451
} ;
410
- Ok ( directive)
452
+ directives . push ( directive) ;
411
453
}
454
+ Ok ( directives)
412
455
}
0 commit comments