Skip to content

Commit 1d85e47

Browse files
author
Marek Suchánek
committed
Implement clippy suggestions
1 parent 47e944c commit 1d85e47

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// This module defines the `Module` struct, its builder struct, and methods on both structs.
22
use std::fmt;
3-
use std::path::{Path, PathBuf};
3+
use std::path::{Component, Path, PathBuf};
44

55
use crate::Options;
66

@@ -228,7 +228,7 @@ impl Input {
228228
let component_vec: Vec<_> = target_path
229229
.as_path()
230230
.components()
231-
.map(|c| c.as_os_str())
231+
.map(Component::as_os_str)
232232
.collect();
233233

234234
// Find the position of the component that matches the root element,

src/validation.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/// This module provides functionality to validate (lint) existing module and assembly files,
22
/// to check if the files meet the template structure and other requirements.
3+
use std::ffi::OsStr;
34
use std::fmt;
45
use std::fs;
56
use std::path::Path;
67

7-
use color_eyre::eyre::{Context, Result};
8+
use color_eyre::eyre::{eyre, Context, Result};
89
use regex::{Regex, RegexBuilder};
910

1011
use crate::module::ContentType;
@@ -102,7 +103,9 @@ pub fn validate(file_name: &str) -> Result<()> {
102103
log::debug!("Validating file `{}`", file_name);
103104

104105
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))?;
106109

107110
let content =
108111
fs::read_to_string(path).context(format!("Error reading file `{}`.", file_name))?;
@@ -156,7 +159,7 @@ enum ModTypeOrReport {
156159
}
157160

158161
/// 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 {
160163
let mod_patterns = [
161164
(
162165
"assembly",
@@ -167,8 +170,14 @@ fn determine_mod_type(base_name: &str, content: &str) -> ModTypeOrReport {
167170
("proc", ":_content-type: PROCEDURE", ContentType::Procedure),
168171
("ref", ":_content-type: REFERENCE", ContentType::Reference),
169172
];
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+
170179
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) {
172181
return ModTypeOrReport::Type(pattern.2);
173182
}
174183
}
@@ -201,7 +210,10 @@ fn check_common(content: &str) -> Vec<IssueReport> {
201210

202211
// This section groups all title requirements
203212
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+
};
205217

206218
const SIMPLE_TITLE_TESTS: [IssueDefinition; 1] = [
207219
// Test that there are no inline anchors in the title
@@ -295,7 +307,10 @@ mod title {
295307

296308
// This section groups all content requirements
297309
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+
};
299314

300315
const SIMPLE_CONTENT_TESTS: [IssueDefinition; 2] = [
301316
IssueDefinition {
@@ -412,7 +427,10 @@ mod content {
412427
// This section groups all module requirements;
413428
// they depend on title and content, and additional resources requirements
414429
mod module {
415-
use super::*;
430+
use super::{
431+
check_common, perform_simple_tests, IssueDefinition, IssueReport, IssueSeverity, Regex,
432+
REGEX_ERROR,
433+
};
416434

417435
const SIMPLE_MODULE_TESTS: [IssueDefinition; 2] = [
418436
// Ensure the correct syntax for Additional resources
@@ -480,7 +498,10 @@ mod module {
480498
// This section groups all assembly requirements;
481499
// they depend on title and content, and additional resources requirements
482500
mod assembly {
483-
use super::*;
501+
use super::{
502+
check_common, perform_simple_tests, IssueDefinition, IssueReport, IssueSeverity, Regex,
503+
REGEX_ERROR,
504+
};
484505

485506
const SIMPLE_ASSEMBLY_TESTS: [IssueDefinition; 3] = [
486507
// Test that an assembly includes no other assemblies
@@ -559,7 +580,10 @@ mod assembly {
559580
}
560581

561582
mod additional_resources {
562-
use super::*;
583+
use super::{
584+
find_first_occurrence, perform_simple_tests, IssueDefinition, IssueReport, IssueSeverity,
585+
Regex, REGEX_ERROR,
586+
};
563587

564588
const SIMPLE_ADDITIONAL_RESOURCES_TESTS: [IssueDefinition; 0] = [
565589
// No simple tests at this point.

0 commit comments

Comments
 (0)