Skip to content

Commit bf944cb

Browse files
committed
Rename :_module-type: to :_content-type: in validation
1 parent 6876036 commit bf944cb

File tree

1 file changed

+45
-47
lines changed

1 file changed

+45
-47
lines changed

src/validation.rs

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,11 @@ enum ModTypeOrReport {
159159

160160
/// Try to determine the module type of a file, using the file name and the file content.
161161
fn determine_mod_type(base_name: &str, content: &str) -> ModTypeOrReport {
162-
if base_name.starts_with("assembly_") || base_name.starts_with("assembly-") {
163-
return ModTypeOrReport::Type(ModuleType::Assembly);
164-
}
165162
let mod_patterns = [
166-
("con", ":_module-type: CONCEPT", ModuleType::Concept),
167-
("proc", ":_module-type: PROCEDURE", ModuleType::Procedure),
168-
("ref", ":_module-type: REFERENCE", ModuleType::Reference),
163+
("assembly", ":_content-type: ASSEMBLY", ModuleType::Assembly),
164+
("con", ":_content-type: CONCEPT", ModuleType::Concept),
165+
("proc", ":_content-type: PROCEDURE", ModuleType::Procedure),
166+
("ref", ":_content-type: REFERENCE", ModuleType::Reference),
169167
];
170168
for pattern in mod_patterns.iter() {
171169
if base_name.starts_with(pattern.0) || content.contains(pattern.1) {
@@ -311,6 +309,7 @@ mod title {
311309
// This section groups all content requirements
312310
mod content {
313311
use crate::validation::find_first_occurrence;
312+
use crate::validation::find_mod_id;
314313
use crate::validation::perform_simple_tests;
315314
use crate::validation::IssueDefinition;
316315
use crate::validation::IssueReport;
@@ -347,10 +346,50 @@ mod content {
347346
}
348347

349348
reports.append(check_path_xrefs(content).as_mut());
349+
reports.append(check_metadata_variable(content).as_mut());
350350

351351
reports
352352
}
353353

354+
/// Check that the module type variable is located before the module ID, as required.
355+
/// As a side effect, this function also checks that both the varible and the ID are present.
356+
fn check_metadata_variable(content: &str) -> Vec<IssueReport> {
357+
let metadata_var_pattern =
358+
r":_content-type:\s*(?:ASSEMBLY|PROCEDURE|CONCEPT|REFERENCE|SNIPPET)";
359+
let metadata_var_regex = Regex::new(metadata_var_pattern).unwrap();
360+
let metadata_var = find_first_occurrence(content, metadata_var_regex);
361+
362+
let mod_id = find_mod_id(content);
363+
364+
// Prepare to store the reports about the module
365+
let mut results: Vec<IssueReport> = Vec::new();
366+
367+
// Report if the metadata variable is completely missing.
368+
// A missing ID is already reported elsewhere.
369+
if metadata_var.is_none() {
370+
let report = IssueReport {
371+
line_number: None,
372+
description: "The module is missing the _content-type attribute.",
373+
severity: IssueSeverity::Warning,
374+
};
375+
results.push(report);
376+
}
377+
378+
// If both elements are present, ensure their proper position in relation to each other
379+
if let (Some(mod_id), Some(metadata_var)) = (mod_id, metadata_var) {
380+
if mod_id.0 < metadata_var.0 {
381+
let report = IssueReport {
382+
line_number: Some(metadata_var.0),
383+
description: "The _content-type attribute is located after the module ID.",
384+
severity: IssueSeverity::Error,
385+
};
386+
results.push(report);
387+
}
388+
}
389+
390+
results
391+
}
392+
354393
/// Check that the abstract flag exists in the file and that it's followed by a paragraph.
355394
fn check_abstract_flag(content: &str) -> Option<IssueReport> {
356395
let abstract_regex = Regex::new(r#"^\[role="_abstract"\]"#).unwrap();
@@ -458,8 +497,6 @@ mod content {
458497
// they depend on title and content, and additional resources requirements
459498
mod module {
460499
use crate::validation::check_common;
461-
use crate::validation::find_first_occurrence;
462-
use crate::validation::find_mod_id;
463500
use crate::validation::perform_simple_tests;
464501
use crate::validation::IssueDefinition;
465502
use crate::validation::IssueReport;
@@ -488,50 +525,11 @@ mod module {
488525

489526
reports.append(check_common(content).as_mut());
490527
reports.append(perform_simple_tests(content, &SIMPLE_MODULE_TESTS).as_mut());
491-
reports.append(check_metadata_variable(content).as_mut());
492528
reports.append(check_include_except_snip(content).as_mut());
493529

494530
reports
495531
}
496532

497-
/// Check that the module type variable is located before the module ID, as required.
498-
/// As a side effect, this function also checks that both the varible and the ID are present.
499-
fn check_metadata_variable(content: &str) -> Vec<IssueReport> {
500-
let metadata_var_pattern = r":_module-type:\s*(?:PROCEDURE|CONCEPT|REFERENCE)";
501-
let metadata_var_regex = Regex::new(metadata_var_pattern).unwrap();
502-
let metadata_var = find_first_occurrence(content, metadata_var_regex);
503-
504-
let mod_id = find_mod_id(content);
505-
506-
// Prepare to store the reports about the module
507-
let mut results: Vec<IssueReport> = Vec::new();
508-
509-
// Report if the metadata variable is completely missing.
510-
// A missing ID is already reported elsewhere.
511-
if metadata_var.is_none() {
512-
let report = IssueReport {
513-
line_number: None,
514-
description: "The module is missing the module type variable.",
515-
severity: IssueSeverity::Warning,
516-
};
517-
results.push(report);
518-
}
519-
520-
// If both elements are present, ensure their proper position in relation to each other
521-
if let (Some(mod_id), Some(metadata_var)) = (mod_id, metadata_var) {
522-
if mod_id.0 < metadata_var.0 {
523-
let report = IssueReport {
524-
line_number: Some(metadata_var.0),
525-
description: "The module type variable is located after the module ID.",
526-
severity: IssueSeverity::Error,
527-
};
528-
results.push(report);
529-
}
530-
}
531-
532-
results
533-
}
534-
535533
/// Test that modules include no other modules, except for snippets
536534
fn check_include_except_snip(content: &str) -> Vec<IssueReport> {
537535
let any_include_pattern = r"^include::.*\.adoc";

0 commit comments

Comments
 (0)