@@ -43,6 +43,10 @@ use std::path::PathBuf;
43
43
#[ derive( Debug , Parser ) ]
44
44
#[ command( author, version, about) ]
45
45
struct Options {
46
+ /// Prefix for the category (e.g. 'lib' or 'utils').
47
+ #[ arg( short, long, default_value_t = String :: from( "lib" ) ) ]
48
+ prefix : String ,
49
+
46
50
/// Name of the function category (e.g. 'strings', 'attrsets').
47
51
#[ arg( short, long) ]
48
52
category : String ,
@@ -80,8 +84,9 @@ struct DocItem {
80
84
}
81
85
82
86
impl DocItem {
83
- fn into_entry ( self , category : & str ) -> ManualEntry {
87
+ fn into_entry ( self , prefix : & str , category : & str ) -> ManualEntry {
84
88
ManualEntry {
89
+ prefix : prefix. to_string ( ) ,
85
90
category : category. to_string ( ) ,
86
91
name : self . name ,
87
92
description : self
@@ -329,6 +334,7 @@ fn collect_entry_information(entry: AttrpathValue) -> Option<DocItem> {
329
334
// - as inherits
330
335
fn collect_bindings (
331
336
node : & SyntaxNode ,
337
+ prefix : & str ,
332
338
category : & str ,
333
339
scope : HashMap < String , ManualEntry > ,
334
340
) -> Vec < ManualEntry > {
@@ -339,7 +345,8 @@ fn collect_bindings(
339
345
for child in n. children ( ) {
340
346
if let Some ( apv) = AttrpathValue :: cast ( child. clone ( ) ) {
341
347
entries. extend (
342
- collect_entry_information ( apv) . map ( |di| di. into_entry ( category) ) ,
348
+ collect_entry_information ( apv)
349
+ . map ( |di| di. into_entry ( prefix, category) ) ,
343
350
) ;
344
351
} else if let Some ( inh) = Inherit :: cast ( child) {
345
352
// `inherit (x) ...` needs much more handling than we can
@@ -366,7 +373,7 @@ fn collect_bindings(
366
373
367
374
// Main entrypoint for collection
368
375
// TODO: document
369
- fn collect_entries ( root : rnix:: Root , category : & str ) -> Vec < ManualEntry > {
376
+ fn collect_entries ( root : rnix:: Root , prefix : & str , category : & str ) -> Vec < ManualEntry > {
370
377
// we will look into the top-level let and its body for function docs.
371
378
// we only need a single level of scope for this.
372
379
// since only the body can export a function we don't need to implement
@@ -376,16 +383,17 @@ fn collect_entries(root: rnix::Root, category: &str) -> Vec<ManualEntry> {
376
383
WalkEvent :: Enter ( n) if n. kind ( ) == SyntaxKind :: NODE_LET_IN => {
377
384
return collect_bindings (
378
385
LetIn :: cast ( n. clone ( ) ) . unwrap ( ) . body ( ) . unwrap ( ) . syntax ( ) ,
386
+ prefix,
379
387
category,
380
388
n. children ( )
381
389
. filter_map ( AttrpathValue :: cast)
382
390
. filter_map ( collect_entry_information)
383
- . map ( |di| ( di. name . to_string ( ) , di. into_entry ( category) ) )
391
+ . map ( |di| ( di. name . to_string ( ) , di. into_entry ( prefix , category) ) )
384
392
. collect ( ) ,
385
393
) ;
386
394
}
387
395
WalkEvent :: Enter ( n) if n. kind ( ) == SyntaxKind :: NODE_ATTR_SET => {
388
- return collect_bindings ( & n, category, Default :: default ( ) ) ;
396
+ return collect_bindings ( & n, prefix , category, Default :: default ( ) ) ;
389
397
}
390
398
_ => ( ) ,
391
399
}
@@ -424,7 +432,7 @@ fn main() {
424
432
// TODO: move this to commonmark.rs
425
433
writeln ! ( output, "{}" , description) . expect ( "Failed to write header" ) ;
426
434
427
- for entry in collect_entries ( nix, & opts. category ) {
435
+ for entry in collect_entries ( nix, & opts. prefix , & opts . category ) {
428
436
entry
429
437
. write_section ( & locs, & mut output)
430
438
. expect ( "Failed to write section" )
@@ -438,6 +446,7 @@ fn test_main() {
438
446
let locs = serde_json:: from_str ( & fs:: read_to_string ( "test/strings.json" ) . unwrap ( ) ) . unwrap ( ) ;
439
447
let nix = rnix:: Root :: parse ( & src) . ok ( ) . expect ( "failed to parse input" ) ;
440
448
let desc = "string manipulation functions" ;
449
+ let prefix = "lib" ;
441
450
let category = "strings" ;
442
451
443
452
// TODO: move this to commonmark.rs
@@ -448,7 +457,7 @@ fn test_main() {
448
457
)
449
458
. expect ( "Failed to write header" ) ;
450
459
451
- for entry in collect_entries ( nix, category) {
460
+ for entry in collect_entries ( nix, prefix , category) {
452
461
entry
453
462
. write_section ( & locs, & mut output)
454
463
. expect ( "Failed to write section" )
@@ -464,11 +473,12 @@ fn test_description_of_lib_debug() {
464
473
let mut output = Vec :: new ( ) ;
465
474
let src = fs:: read_to_string ( "test/lib-debug.nix" ) . unwrap ( ) ;
466
475
let nix = rnix:: Root :: parse ( & src) . ok ( ) . expect ( "failed to parse input" ) ;
476
+ let prefix = "lib" ;
467
477
let category = "debug" ;
468
478
let desc = retrieve_description ( & nix, & "Debug" , category) ;
469
479
writeln ! ( output, "{}" , desc) . expect ( "Failed to write header" ) ;
470
480
471
- for entry in collect_entries ( nix, category) {
481
+ for entry in collect_entries ( nix, prefix , category) {
472
482
entry
473
483
. write_section ( & Default :: default ( ) , & mut output)
474
484
. expect ( "Failed to write section" )
@@ -484,9 +494,10 @@ fn test_arg_formatting() {
484
494
let mut output = Vec :: new ( ) ;
485
495
let src = fs:: read_to_string ( "test/arg-formatting.nix" ) . unwrap ( ) ;
486
496
let nix = rnix:: Root :: parse ( & src) . ok ( ) . expect ( "failed to parse input" ) ;
497
+ let prefix = "lib" ;
487
498
let category = "options" ;
488
499
489
- for entry in collect_entries ( nix, category) {
500
+ for entry in collect_entries ( nix, prefix , category) {
490
501
entry
491
502
. write_section ( & Default :: default ( ) , & mut output)
492
503
. expect ( "Failed to write section" )
@@ -502,9 +513,10 @@ fn test_inherited_exports() {
502
513
let mut output = Vec :: new ( ) ;
503
514
let src = fs:: read_to_string ( "test/inherited-exports.nix" ) . unwrap ( ) ;
504
515
let nix = rnix:: Root :: parse ( & src) . ok ( ) . expect ( "failed to parse input" ) ;
516
+ let prefix = "lib" ;
505
517
let category = "let" ;
506
518
507
- for entry in collect_entries ( nix, category) {
519
+ for entry in collect_entries ( nix, prefix , category) {
508
520
entry
509
521
. write_section ( & Default :: default ( ) , & mut output)
510
522
. expect ( "Failed to write section" )
@@ -520,9 +532,10 @@ fn test_line_comments() {
520
532
let mut output = Vec :: new ( ) ;
521
533
let src = fs:: read_to_string ( "test/line-comments.nix" ) . unwrap ( ) ;
522
534
let nix = rnix:: Root :: parse ( & src) . ok ( ) . expect ( "failed to parse input" ) ;
535
+ let prefix = "lib" ;
523
536
let category = "let" ;
524
537
525
- for entry in collect_entries ( nix, category) {
538
+ for entry in collect_entries ( nix, prefix , category) {
526
539
entry
527
540
. write_section ( & Default :: default ( ) , & mut output)
528
541
. expect ( "Failed to write section" )
@@ -538,9 +551,10 @@ fn test_multi_line() {
538
551
let mut output = Vec :: new ( ) ;
539
552
let src = fs:: read_to_string ( "test/multi-line.nix" ) . unwrap ( ) ;
540
553
let nix = rnix:: Root :: parse ( & src) . ok ( ) . expect ( "failed to parse input" ) ;
554
+ let prefix = "lib" ;
541
555
let category = "let" ;
542
556
543
- for entry in collect_entries ( nix, category) {
557
+ for entry in collect_entries ( nix, prefix , category) {
544
558
entry
545
559
. write_section ( & Default :: default ( ) , & mut output)
546
560
. expect ( "Failed to write section" )
0 commit comments