1
1
/// This module provides functionality to validate (lint) existing module and assembly files,
2
2
/// to check if the files meet the template structure and other requirements.
3
+ use std:: ffi:: OsStr ;
3
4
use std:: fmt;
4
5
use std:: fs;
5
6
use std:: path:: Path ;
6
7
7
- use color_eyre:: eyre:: { Context , Result } ;
8
+ use color_eyre:: eyre:: { eyre , Context , Result } ;
8
9
use regex:: { Regex , RegexBuilder } ;
9
10
10
11
use crate :: module:: ContentType ;
@@ -102,7 +103,9 @@ pub fn validate(file_name: &str) -> Result<()> {
102
103
log:: debug!( "Validating file `{}`" , file_name) ;
103
104
104
105
let path = Path :: new ( file_name) ;
105
- let base_name = path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
106
+ let base_name: & OsStr = path
107
+ . file_name ( )
108
+ . ok_or_else ( || eyre ! ( "Invalid file name: {:?}" , path) ) ?;
106
109
107
110
let content =
108
111
fs:: read_to_string ( path) . context ( format ! ( "Error reading file `{}`." , file_name) ) ?;
@@ -156,7 +159,7 @@ enum ModTypeOrReport {
156
159
}
157
160
158
161
/// Try to determine the module type of a file, using the file name and the file content.
159
- fn determine_mod_type ( base_name : & str , content : & str ) -> ModTypeOrReport {
162
+ fn determine_mod_type ( base_name : & OsStr , content : & str ) -> ModTypeOrReport {
160
163
let mod_patterns = [
161
164
(
162
165
"assembly" ,
@@ -167,8 +170,14 @@ fn determine_mod_type(base_name: &str, content: &str) -> ModTypeOrReport {
167
170
( "proc" , ":_content-type: PROCEDURE" , ContentType :: Procedure ) ,
168
171
( "ref" , ":_content-type: REFERENCE" , ContentType :: Reference ) ,
169
172
] ;
173
+
174
+ // Convert the OS file name to a string, replacing characters that aren't valid Unicode
175
+ // with `�` placeholders.
176
+ // OsStr itself is missing the `starts_with` method that we need here.
177
+ let lossy_name = base_name. to_string_lossy ( ) ;
178
+
170
179
for pattern in & mod_patterns {
171
- if base_name . starts_with ( pattern. 0 ) || content. contains ( pattern. 1 ) {
180
+ if lossy_name . starts_with ( pattern. 0 ) || content. contains ( pattern. 1 ) {
172
181
return ModTypeOrReport :: Type ( pattern. 2 ) ;
173
182
}
174
183
}
@@ -201,7 +210,10 @@ fn check_common(content: &str) -> Vec<IssueReport> {
201
210
202
211
// This section groups all title requirements
203
212
mod title {
204
- use super :: * ;
213
+ use super :: {
214
+ find_first_occurrence, find_mod_id, perform_simple_tests, IssueDefinition , IssueReport ,
215
+ IssueSeverity , Regex , REGEX_ERROR ,
216
+ } ;
205
217
206
218
const SIMPLE_TITLE_TESTS : [ IssueDefinition ; 1 ] = [
207
219
// Test that there are no inline anchors in the title
@@ -295,7 +307,10 @@ mod title {
295
307
296
308
// This section groups all content requirements
297
309
mod content {
298
- use super :: * ;
310
+ use super :: {
311
+ find_first_occurrence, find_mod_id, perform_simple_tests, IssueDefinition , IssueReport ,
312
+ IssueSeverity , Regex , REGEX_ERROR ,
313
+ } ;
299
314
300
315
const SIMPLE_CONTENT_TESTS : [ IssueDefinition ; 2 ] = [
301
316
IssueDefinition {
@@ -412,7 +427,10 @@ mod content {
412
427
// This section groups all module requirements;
413
428
// they depend on title and content, and additional resources requirements
414
429
mod module {
415
- use super :: * ;
430
+ use super :: {
431
+ check_common, perform_simple_tests, IssueDefinition , IssueReport , IssueSeverity , Regex ,
432
+ REGEX_ERROR ,
433
+ } ;
416
434
417
435
const SIMPLE_MODULE_TESTS : [ IssueDefinition ; 2 ] = [
418
436
// Ensure the correct syntax for Additional resources
@@ -480,7 +498,10 @@ mod module {
480
498
// This section groups all assembly requirements;
481
499
// they depend on title and content, and additional resources requirements
482
500
mod assembly {
483
- use super :: * ;
501
+ use super :: {
502
+ check_common, perform_simple_tests, IssueDefinition , IssueReport , IssueSeverity , Regex ,
503
+ REGEX_ERROR ,
504
+ } ;
484
505
485
506
const SIMPLE_ASSEMBLY_TESTS : [ IssueDefinition ; 3 ] = [
486
507
// Test that an assembly includes no other assemblies
@@ -559,7 +580,10 @@ mod assembly {
559
580
}
560
581
561
582
mod additional_resources {
562
- use super :: * ;
583
+ use super :: {
584
+ find_first_occurrence, perform_simple_tests, IssueDefinition , IssueReport , IssueSeverity ,
585
+ Regex , REGEX_ERROR ,
586
+ } ;
563
587
564
588
const SIMPLE_ADDITIONAL_RESOURCES_TESTS : [ IssueDefinition ; 0 ] = [
565
589
// No simple tests at this point.
0 commit comments