@@ -8,6 +8,7 @@ use color_eyre::eyre::{Context, Result};
8
8
use regex:: { Regex , RegexBuilder } ;
9
9
10
10
use crate :: module:: ContentType ;
11
+ use crate :: REGEX_ERROR ;
11
12
12
13
#[ derive( Clone , Copy , Debug ) ]
13
14
struct IssueDefinition {
@@ -43,7 +44,7 @@ impl IssueDefinition {
43
44
let regex = RegexBuilder :: new ( self . pattern )
44
45
. multi_line ( true )
45
46
. build ( )
46
- . unwrap ( ) ;
47
+ . expect ( REGEX_ERROR ) ;
47
48
let findings = regex. find_iter ( content) ;
48
49
49
50
findings
@@ -55,7 +56,7 @@ impl IssueDefinition {
55
56
. collect ( )
56
57
// If single-line:
57
58
} else {
58
- let regex = Regex :: new ( self . pattern ) . unwrap ( ) ;
59
+ let regex = Regex :: new ( self . pattern ) . expect ( REGEX_ERROR ) ;
59
60
let findings = content
60
61
. lines ( )
61
62
. enumerate ( )
@@ -200,13 +201,7 @@ fn check_common(content: &str) -> Vec<IssueReport> {
200
201
201
202
// This section groups all title requirements
202
203
mod title {
203
- use crate :: validation:: find_first_occurrence;
204
- use crate :: validation:: find_mod_id;
205
- use crate :: validation:: perform_simple_tests;
206
- use crate :: validation:: IssueDefinition ;
207
- use crate :: validation:: IssueReport ;
208
- use crate :: validation:: IssueSeverity ;
209
- use regex:: Regex ;
204
+ use super :: * ;
210
205
211
206
const SIMPLE_TITLE_TESTS : [ IssueDefinition ; 1 ] = [
212
207
// Test that there are no inline anchors in the title
@@ -237,14 +232,14 @@ mod title {
237
232
/// Find the first occurence of any heading in the file.
238
233
/// Returns the line number of the occurence and the line.
239
234
fn find_first_heading ( content : & str ) -> Option < ( usize , & str ) > {
240
- let any_heading_regex = Regex :: new ( r"^(\.|=+\s+)\S+.*" ) . unwrap ( ) ;
235
+ let any_heading_regex = Regex :: new ( r"^(\.|=+\s+)\S+.*" ) . expect ( REGEX_ERROR ) ;
241
236
242
237
find_first_occurrence ( content, & any_heading_regex)
243
238
}
244
239
245
240
/// Check that the first heading found in the file is a title: a level-1, numbered heading
246
241
fn check_title_level ( content : & str ) -> Option < IssueReport > {
247
- let title_regex = Regex :: new ( r"^=\s+\S+.*" ) . unwrap ( ) ;
242
+ let title_regex = Regex :: new ( r"^=\s+\S+.*" ) . expect ( REGEX_ERROR ) ;
248
243
249
244
if let Some ( ( line_no, heading) ) = find_first_heading ( content) {
250
245
if let Some ( _title) = title_regex. find ( heading) {
@@ -282,7 +277,7 @@ mod title {
282
277
}
283
278
} ;
284
279
285
- let attribute_regex = Regex :: new ( r"\{((?:[[:alnum:]]|[-_])+)\}" ) . unwrap ( ) ;
280
+ let attribute_regex = Regex :: new ( r"\{((?:[[:alnum:]]|[-_])+)\}" ) . expect ( REGEX_ERROR ) ;
286
281
let attribute = attribute_regex. captures ( mod_id) ?;
287
282
288
283
if attribute. get ( 1 ) . unwrap ( ) . as_str ( ) == "context" {
@@ -300,13 +295,7 @@ mod title {
300
295
301
296
// This section groups all content requirements
302
297
mod content {
303
- use crate :: validation:: find_first_occurrence;
304
- use crate :: validation:: find_mod_id;
305
- use crate :: validation:: perform_simple_tests;
306
- use crate :: validation:: IssueDefinition ;
307
- use crate :: validation:: IssueReport ;
308
- use crate :: validation:: IssueSeverity ;
309
- use regex:: Regex ;
298
+ use super :: * ;
310
299
311
300
const SIMPLE_CONTENT_TESTS : [ IssueDefinition ; 2 ] = [
312
301
IssueDefinition {
@@ -343,7 +332,7 @@ mod content {
343
332
fn check_metadata_variable ( content : & str ) -> Vec < IssueReport > {
344
333
let metadata_var_pattern =
345
334
r":_content-type:\s*(?:ASSEMBLY|PROCEDURE|CONCEPT|REFERENCE|SNIPPET)" ;
346
- let metadata_var_regex = Regex :: new ( metadata_var_pattern) . unwrap ( ) ;
335
+ let metadata_var_regex = Regex :: new ( metadata_var_pattern) . expect ( REGEX_ERROR ) ;
347
336
let metadata_var = find_first_occurrence ( content, & metadata_var_regex) ;
348
337
349
338
let mod_id = find_mod_id ( content) ;
@@ -380,7 +369,7 @@ mod content {
380
369
/// Check that the abstract flag is followed by a paragraph,
381
370
/// if it exists at all. The abstract flag is not required.
382
371
fn check_abstract_flag ( content : & str ) -> Option < IssueReport > {
383
- let abstract_regex = Regex :: new ( r#"^\[role="_abstract"\]"# ) . unwrap ( ) ;
372
+ let abstract_regex = Regex :: new ( r#"^\[role="_abstract"\]"# ) . expect ( REGEX_ERROR ) ;
384
373
let abstract_flag = find_first_occurrence ( content, & abstract_regex) ;
385
374
386
375
// If the file contains an abstract flag, test for the following paragraph
@@ -400,7 +389,7 @@ mod content {
400
389
// [systemitem]`firewalld` can be used to (...)
401
390
// Let's just check that the line starts with a non-whitespace character
402
391
// and that a letter appears at least somewhere.
403
- let paragraph_regex = Regex :: new ( r"^\S+[[:alpha:]].*" ) . unwrap ( ) ;
392
+ let paragraph_regex = Regex :: new ( r"^\S+[[:alpha:]].*" ) . expect ( REGEX_ERROR ) ;
404
393
// If a line follows the flag but it doesn't appear as a paragraph, report the issue
405
394
if paragraph_regex. find ( next_line) . is_none ( ) {
406
395
log:: debug!( "The non-paragraph-line: {:?}" , next_line) ;
@@ -423,12 +412,7 @@ mod content {
423
412
// This section groups all module requirements;
424
413
// they depend on title and content, and additional resources requirements
425
414
mod module {
426
- use crate :: validation:: check_common;
427
- use crate :: validation:: perform_simple_tests;
428
- use crate :: validation:: IssueDefinition ;
429
- use crate :: validation:: IssueReport ;
430
- use crate :: validation:: IssueSeverity ;
431
- use regex:: Regex ;
415
+ use super :: * ;
432
416
433
417
const SIMPLE_MODULE_TESTS : [ IssueDefinition ; 2 ] = [
434
418
// Ensure the correct syntax for Additional resources
@@ -460,10 +444,10 @@ mod module {
460
444
/// Test that modules include no other modules, except for snippets
461
445
fn check_include_except_snip ( content : & str ) -> Vec < IssueReport > {
462
446
let any_include_pattern = r"^include::.*\.adoc" ;
463
- let any_include_regex = Regex :: new ( any_include_pattern) . unwrap ( ) ;
447
+ let any_include_regex = Regex :: new ( any_include_pattern) . expect ( REGEX_ERROR ) ;
464
448
465
449
let snip_include_pattern = r"^include::((snip|.*/snip)[_-]|common-content/).*\.adoc" ;
466
- let snip_include_regex = Regex :: new ( snip_include_pattern) . unwrap ( ) ;
450
+ let snip_include_regex = Regex :: new ( snip_include_pattern) . expect ( REGEX_ERROR ) ;
467
451
468
452
let mut reports: Vec < IssueReport > = Vec :: new ( ) ;
469
453
@@ -496,12 +480,7 @@ mod module {
496
480
// This section groups all assembly requirements;
497
481
// they depend on title and content, and additional resources requirements
498
482
mod assembly {
499
- use crate :: validation:: check_common;
500
- use crate :: validation:: perform_simple_tests;
501
- use crate :: validation:: IssueDefinition ;
502
- use crate :: validation:: IssueReport ;
503
- use crate :: validation:: IssueSeverity ;
504
- use regex:: Regex ;
483
+ use super :: * ;
505
484
506
485
const SIMPLE_ASSEMBLY_TESTS : [ IssueDefinition ; 3 ] = [
507
486
// Test that an assembly includes no other assemblies
@@ -547,10 +526,10 @@ mod assembly {
547
526
/// * == Additional resources
548
527
/// In addition, let's also assume that the legacy 'Related information' heading is fine. (TODO: Make sure.)
549
528
fn check_headings_in_assembly ( content : & str ) -> Vec < IssueReport > {
550
- let heading_regex = Regex :: new ( r"^={2,}\s+\S.*" ) . unwrap ( ) ;
529
+ let heading_regex = Regex :: new ( r"^={2,}\s+\S.*" ) . expect ( REGEX_ERROR ) ;
551
530
let standard_headings_pattern =
552
531
r"^==\s+(?:Prerequisites|Additional resources|Related information)" ;
553
- let standard_headings_regex = Regex :: new ( standard_headings_pattern) . unwrap ( ) ;
532
+ let standard_headings_regex = Regex :: new ( standard_headings_pattern) . expect ( REGEX_ERROR ) ;
554
533
555
534
let mut lines_with_heading: Vec < usize > = Vec :: new ( ) ;
556
535
@@ -580,12 +559,7 @@ mod assembly {
580
559
}
581
560
582
561
mod additional_resources {
583
- use crate :: validation:: find_first_occurrence;
584
- use crate :: validation:: perform_simple_tests;
585
- use crate :: validation:: IssueDefinition ;
586
- use crate :: validation:: IssueReport ;
587
- use crate :: validation:: IssueSeverity ;
588
- use regex:: Regex ;
562
+ use super :: * ;
589
563
590
564
const SIMPLE_ADDITIONAL_RESOURCES_TESTS : [ IssueDefinition ; 0 ] = [
591
565
// No simple tests at this point.
@@ -623,7 +597,7 @@ mod additional_resources {
623
597
let add_res_regex = Regex :: new (
624
598
r"^(?:==\s+|\.)(?:Additional resources|Related information|Additional information)\s*$" ,
625
599
)
626
- . unwrap ( ) ;
600
+ . expect ( REGEX_ERROR ) ;
627
601
628
602
find_first_occurrence ( content, & add_res_regex)
629
603
}
@@ -649,11 +623,13 @@ mod additional_resources {
649
623
/// Check that the additional resources section is composed of list items, possibly with some ifdefs.
650
624
fn check_paragraphs_in_add_res ( lines : & [ & str ] , heading_index : usize ) -> Vec < IssueReport > {
651
625
// This regex matches either a plain list item, or one that's embedded in an inline ifdef.
652
- let bullet_point_regex = Regex :: new ( r"(?:^\*+\s+\S+|^ifdef::\S+\[\*+\s+\S+.*\])" ) . unwrap ( ) ;
626
+ let bullet_point_regex =
627
+ Regex :: new ( r"(?:^\*+\s+\S+|^ifdef::\S+\[\*+\s+\S+.*\])" ) . expect ( REGEX_ERROR ) ;
653
628
// A paragraph that isn't a list item is allowed if it's an ifdef or a comment.
654
- let allowed_paragraph = Regex :: new ( r"^(?:ifdef::\S+\[.*]|endif::\[\]|//)" ) . unwrap ( ) ;
629
+ let allowed_paragraph =
630
+ Regex :: new ( r"^(?:ifdef::\S+\[.*]|endif::\[\]|//)" ) . expect ( REGEX_ERROR ) ;
655
631
// Let's try to use a loose definition of an empty paragraph as a whitespace paragraph.
656
- let empty_line_regex = Regex :: new ( r"^\s*$" ) . unwrap ( ) ;
632
+ let empty_line_regex = Regex :: new ( r"^\s*$" ) . expect ( REGEX_ERROR ) ;
657
633
658
634
let mut issues = Vec :: new ( ) ;
659
635
@@ -694,7 +670,7 @@ mod additional_resources {
694
670
/// Detect links with no labels after a certain point in the file,
695
671
/// specifically after the additional resources heading.
696
672
fn check_link_labels_in_add_res ( lines : & [ & str ] , heading_index : usize ) -> Vec < IssueReport > {
697
- let link_regex = Regex :: new ( r"link:\S+\[]" ) . unwrap ( ) ;
673
+ let link_regex = Regex :: new ( r"link:\S+\[]" ) . expect ( REGEX_ERROR ) ;
698
674
699
675
let mut issues = Vec :: new ( ) ;
700
676
@@ -716,7 +692,7 @@ mod additional_resources {
716
692
fn check_additional_resource_length ( lines : & [ & str ] , heading_index : usize ) -> Vec < IssueReport > {
717
693
// This regex features capture groups to extract the content of the list item.
718
694
let bullet_point_regex =
719
- Regex :: new ( r"^(?:\*+\s+(\S+.*)|ifdef::\S+\[\*+\s+(\S+.*)\])" ) . unwrap ( ) ;
695
+ Regex :: new ( r"^(?:\*+\s+(\S+.*)|ifdef::\S+\[\*+\s+(\S+.*)\])" ) . expect ( REGEX_ERROR ) ;
720
696
// This is the number of words you need to write:
721
697
// * The `program(1)` man page
722
698
// Let's use that as the approximate upper limit.
@@ -752,7 +728,7 @@ mod additional_resources {
752
728
/// Find the first occurence of an ID definition in the file.
753
729
/// Returns the line number of the occurence and the line.
754
730
fn find_mod_id ( content : & str ) -> Option < ( usize , & str ) > {
755
- let id_regex = Regex :: new ( r"^\[id=\S+\]" ) . unwrap ( ) ;
731
+ let id_regex = Regex :: new ( r"^\[id=\S+\]" ) . expect ( REGEX_ERROR ) ;
756
732
757
733
find_first_occurrence ( content, & id_regex)
758
734
}
0 commit comments