Skip to content

Commit 01703c3

Browse files
author
Marek Suchánek
committed
Implement clippy suggestions
1 parent fd1357e commit 01703c3

File tree

7 files changed

+75
-59
lines changed

7 files changed

+75
-59
lines changed

src/cmd_line.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use clap::{command, Arg, ArgGroup, ArgMatches};
44

55
/// Define the command-line arguments and return them as the `clap::ArgMatches` struct.
6+
#[must_use]
67
pub fn get_args() -> ArgMatches {
78
// Define command-line options
89
let matches = command!()

src/lib.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod templating;
88
mod validation;
99
mod write;
1010

11-
pub use module::{Input, Module, ModuleType};
11+
pub use module::{ContentType, Input, Module};
1212

1313
/// This struct stores options based on the command-line arguments,
1414
/// and is passed to various functions across the program.
@@ -23,6 +23,7 @@ pub struct Options {
2323

2424
impl Options {
2525
/// Set current options based on the command-line options
26+
#[must_use]
2627
pub fn new(args: &ArgMatches) -> Self {
2728
// Determine the configured verbosity level.
2829
// The clap configuration ensures that verbose and quiet
@@ -60,7 +61,7 @@ pub enum Verbosity {
6061
Quiet,
6162
}
6263

63-
pub fn run(options: Options, cmdline_args: ArgMatches) -> Result<()> {
64+
pub fn run(options: &Options, cmdline_args: &ArgMatches) -> Result<()> {
6465
// Initialize the logging system based on the set verbosity
6566
logging::initialize_logger(options.verbosity)?;
6667

@@ -74,7 +75,7 @@ pub fn run(options: Options, cmdline_args: ArgMatches) -> Result<()> {
7475
for module_type_str in ["assembly", "concept", "procedure", "reference", "snippet"] {
7576
// Check if the given module type occurs on the command line
7677
if let Some(titles_iterator) = cmdline_args.values_of(module_type_str) {
77-
let mut modules = process_module_type(titles_iterator, module_type_str, &options);
78+
let mut modules = process_module_type(titles_iterator, module_type_str, options);
7879

7980
// Move all the newly created modules into the common Vec
8081
non_populated.append(&mut modules);
@@ -83,7 +84,7 @@ pub fn run(options: Options, cmdline_args: ArgMatches) -> Result<()> {
8384

8485
// Write all non-populated modules to the disk
8586
for module in &non_populated {
86-
module.write_file(&options)?;
87+
module.write_file(options)?;
8788
}
8889

8990
// Treat the populated assembly module as a special case:
@@ -100,11 +101,11 @@ pub fn run(options: Options, cmdline_args: ArgMatches) -> Result<()> {
100101
assert!(!include_statements.is_empty());
101102

102103
// Generate the populated assembly module
103-
let populated: Module = Input::new(ModuleType::Assembly, title, &options)
104+
let populated: Module = Input::new(ContentType::Assembly, title, options)
104105
.include(include_statements)
105106
.into();
106107

107-
populated.write_file(&options)?;
108+
populated.write_file(options)?;
108109
}
109110

110111
// Validate all file names specified on the command line
@@ -125,11 +126,11 @@ fn process_module_type(
125126
options: &Options,
126127
) -> Vec<Module> {
127128
let module_type = match module_type_str {
128-
"assembly" | "include-in" => ModuleType::Assembly,
129-
"concept" => ModuleType::Concept,
130-
"procedure" => ModuleType::Procedure,
131-
"reference" => ModuleType::Reference,
132-
"snippet" => ModuleType::Snippet,
129+
"assembly" | "include-in" => ContentType::Assembly,
130+
"concept" => ContentType::Concept,
131+
"procedure" => ContentType::Procedure,
132+
"reference" => ContentType::Reference,
133+
"snippet" => ContentType::Snippet,
133134
_ => unimplemented!(),
134135
};
135136

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() -> Result<()> {
1010
let options = Options::new(&cmdline_args);
1111

1212
// Run the main functionality
13-
newdoc::run(options, cmdline_args)?;
13+
newdoc::run(&options, &cmdline_args)?;
1414

1515
Ok(())
1616
}

src/module.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::Options;
66

77
/// All possible types of the AsciiDoc module
88
#[derive(Debug, Clone, Copy, PartialEq)]
9-
pub enum ModuleType {
9+
pub enum ContentType {
1010
Assembly,
1111
Concept,
1212
Procedure,
@@ -15,7 +15,7 @@ pub enum ModuleType {
1515
}
1616

1717
// Implement human-readable string display for the module type
18-
impl fmt::Display for ModuleType {
18+
impl fmt::Display for ContentType {
1919
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2020
let name = match self {
2121
Self::Assembly => "assembly",
@@ -31,7 +31,7 @@ impl fmt::Display for ModuleType {
3131
/// An initial representation of the module with input data, used to construct the `Module` struct
3232
#[derive(Debug)]
3333
pub struct Input {
34-
pub mod_type: ModuleType,
34+
pub mod_type: ContentType,
3535
pub title: String,
3636
pub options: Options,
3737
pub includes: Option<Vec<String>>,
@@ -40,7 +40,7 @@ pub struct Input {
4040
/// A representation of the module with all its metadata and the generated AsciiDoc content
4141
#[derive(Debug, PartialEq)]
4242
pub struct Module {
43-
mod_type: ModuleType,
43+
mod_type: ContentType,
4444
title: String,
4545
id: String,
4646
pub file_name: String,
@@ -51,7 +51,8 @@ pub struct Module {
5151

5252
/// Construct a basic builder for `Module`, storing information from the user input.
5353
impl Input {
54-
pub fn new(mod_type: ModuleType, title: &str, options: &Options) -> Input {
54+
#[must_use]
55+
pub fn new(mod_type: ContentType, title: &str, options: &Options) -> Input {
5556
log::debug!("Processing title `{}` of type `{:?}`", title, mod_type);
5657

5758
let title = String::from(title);
@@ -66,6 +67,7 @@ impl Input {
6667
}
6768

6869
/// Set the optional include statements for files that this assembly includes
70+
#[must_use]
6971
pub fn include(mut self, include_statements: Vec<String>) -> Self {
7072
self.includes = Some(include_statements);
7173
self
@@ -76,6 +78,7 @@ impl Input {
7678
/// * An AsciiDoc section ID
7779
/// * A DocBook section ID
7880
/// * A file name
81+
#[must_use]
7982
pub fn id(&self) -> String {
8083
let title = &self.title;
8184
// The ID is all lower-case
@@ -161,6 +164,7 @@ impl Input {
161164
/// Prepare the file name for the generated file.
162165
///
163166
/// The file name is based on the module ID, with the `.adoc` extension.
167+
#[must_use]
164168
pub fn file_name(&self) -> String {
165169
let suffix = ".adoc";
166170

@@ -171,11 +175,11 @@ impl Input {
171175
if self.options.prefixes {
172176
// If prefixes are enabled, pick the right file prefix
173177
match self.mod_type {
174-
ModuleType::Assembly => "assembly_",
175-
ModuleType::Concept => "con_",
176-
ModuleType::Procedure => "proc_",
177-
ModuleType::Reference => "ref_",
178-
ModuleType::Snippet => "snip_",
178+
ContentType::Assembly => "assembly_",
179+
ContentType::Concept => "con_",
180+
ContentType::Procedure => "proc_",
181+
ContentType::Reference => "ref_",
182+
ContentType::Snippet => "snip_",
179183
}
180184
} else {
181185
// If prefixes are disabled, use an empty string for the prefix
@@ -206,8 +210,8 @@ impl Input {
206210
// The first directory in the include path is either `assemblies/` or `modules/`,
207211
// based on the module type, or `snippets/` for snippet files.
208212
let include_root = match &self.mod_type {
209-
ModuleType::Assembly => "assemblies",
210-
ModuleType::Snippet => "snippets",
213+
ContentType::Assembly => "assemblies",
214+
ContentType::Snippet => "snippets",
211215
_ => "modules",
212216
};
213217

@@ -277,7 +281,8 @@ impl From<Input> for Module {
277281
impl Module {
278282
/// The constructor for the Module struct. Creates a basic version of Module
279283
/// without any optional features.
280-
pub fn new(mod_type: ModuleType, title: &str, options: &Options) -> Module {
284+
#[must_use]
285+
pub fn new(mod_type: ContentType, title: &str, options: &Options) -> Module {
281286
let input = Input::new(mod_type, title, options);
282287
input.into()
283288
}
@@ -312,12 +317,12 @@ mod tests {
312317
fn check_basic_assembly_fields() {
313318
let options = basic_options();
314319
let assembly = Module::new(
315-
ModuleType::Assembly,
320+
ContentType::Assembly,
316321
"A testing assembly with /special-characters*",
317322
&options,
318323
);
319324

320-
assert_eq!(assembly.mod_type, ModuleType::Assembly);
325+
assert_eq!(assembly.mod_type, ContentType::Assembly);
321326
assert_eq!(
322327
assembly.title,
323328
"A testing assembly with /special-characters*"
@@ -338,12 +343,12 @@ mod tests {
338343
fn check_module_builder_and_new() {
339344
let options = basic_options();
340345
let from_new: Module = Module::new(
341-
ModuleType::Assembly,
346+
ContentType::Assembly,
342347
"A testing assembly with /special-characters*",
343348
&options,
344349
);
345350
let from_builder: Module = Input::new(
346-
ModuleType::Assembly,
351+
ContentType::Assembly,
347352
"A testing assembly with /special-characters*",
348353
&options,
349354
)
@@ -355,7 +360,11 @@ mod tests {
355360
fn check_detected_path() {
356361
let options = path_options();
357362

358-
let module = Module::new(ModuleType::Procedure, "Testing the detected path", &options);
363+
let module = Module::new(
364+
ContentType::Procedure,
365+
"Testing the detected path",
366+
&options,
367+
);
359368

360369
assert_eq!(
361370
module.include_statement,

src/templating.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use askama::Template;
22
use regex::{Regex, RegexBuilder};
33

4-
use crate::module::{Input, ModuleType};
4+
use crate::module::{ContentType, Input};
55

66
// A note on the structure of this file:
77
// This file repeats a lot of code when it configures the Askama templates.
@@ -78,34 +78,35 @@ impl Input {
7878

7979
/// Perform string replacements in the modular template that matches the `ModuleType`.
8080
/// Return the template text with all replacements.
81+
#[must_use]
8182
pub fn text(&self) -> String {
8283
let mut document = match self.mod_type {
83-
ModuleType::Assembly => AssemblyTemplate {
84+
ContentType::Assembly => AssemblyTemplate {
8485
module_id: &self.id(),
8586
module_title: &self.title,
8687
include_statements: &self.includes_block(),
8788
examples: self.options.examples,
8889
}
8990
.render(),
90-
ModuleType::Concept => ConceptTemplate {
91+
ContentType::Concept => ConceptTemplate {
9192
module_id: &self.id(),
9293
module_title: &self.title,
9394
examples: self.options.examples,
9495
}
9596
.render(),
96-
ModuleType::Procedure => ProcedureTemplate {
97+
ContentType::Procedure => ProcedureTemplate {
9798
module_id: &self.id(),
9899
module_title: &self.title,
99100
examples: self.options.examples,
100101
}
101102
.render(),
102-
ModuleType::Reference => ReferenceTemplate {
103+
ContentType::Reference => ReferenceTemplate {
103104
module_id: &self.id(),
104105
module_title: &self.title,
105106
examples: self.options.examples,
106107
}
107108
.render(),
108-
ModuleType::Snippet => SnippetTemplate {
109+
ContentType::Snippet => SnippetTemplate {
109110
module_title: &self.title,
110111
examples: self.options.examples,
111112
}

src/validation.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::Path;
77
use color_eyre::eyre::{Context, Result};
88
use regex::{Regex, RegexBuilder};
99

10-
use crate::module::ModuleType;
10+
use crate::module::ContentType;
1111

1212
#[derive(Clone, Copy, Debug)]
1313
struct IssueDefinition {
@@ -110,7 +110,7 @@ pub fn validate(file_name: &str) -> Result<()> {
110110

111111
let reports = match mod_type {
112112
// If the file is an assembly, test the assembly requirements
113-
ModTypeOrReport::Type(ModuleType::Assembly) => assembly::check(&content),
113+
ModTypeOrReport::Type(ContentType::Assembly) => assembly::check(&content),
114114
// If the module type is indeterminate, test the requirements that don't depend on the type
115115
ModTypeOrReport::Report(type_report) => {
116116
let mut reports = check_common(&content);
@@ -150,17 +150,21 @@ fn report_issues(mut issues: Vec<IssueReport>, file_path: &str) {
150150
/// This enum contains either the module type determined from a file, or an issue report saying
151151
/// that the module type could not be determined.
152152
enum ModTypeOrReport {
153-
Type(ModuleType),
153+
Type(ContentType),
154154
Report(IssueReport),
155155
}
156156

157157
/// Try to determine the module type of a file, using the file name and the file content.
158158
fn determine_mod_type(base_name: &str, content: &str) -> ModTypeOrReport {
159159
let mod_patterns = [
160-
("assembly", ":_content-type: ASSEMBLY", ModuleType::Assembly),
161-
("con", ":_content-type: CONCEPT", ModuleType::Concept),
162-
("proc", ":_content-type: PROCEDURE", ModuleType::Procedure),
163-
("ref", ":_content-type: REFERENCE", ModuleType::Reference),
160+
(
161+
"assembly",
162+
":_content-type: ASSEMBLY",
163+
ContentType::Assembly,
164+
),
165+
("con", ":_content-type: CONCEPT", ContentType::Concept),
166+
("proc", ":_content-type: PROCEDURE", ContentType::Procedure),
167+
("ref", ":_content-type: REFERENCE", ContentType::Reference),
164168
];
165169
for pattern in &mod_patterns {
166170
if base_name.starts_with(pattern.0) || content.contains(pattern.1) {
@@ -235,7 +239,7 @@ mod title {
235239
fn find_first_heading(content: &str) -> Option<(usize, &str)> {
236240
let any_heading_regex = Regex::new(r"^(\.|=+\s+)\S+.*").unwrap();
237241

238-
find_first_occurrence(content, any_heading_regex)
242+
find_first_occurrence(content, &any_heading_regex)
239243
}
240244

241245
/// Check that the first heading found in the file is a title: a level-1, numbered heading
@@ -340,7 +344,7 @@ mod content {
340344
let metadata_var_pattern =
341345
r":_content-type:\s*(?:ASSEMBLY|PROCEDURE|CONCEPT|REFERENCE|SNIPPET)";
342346
let metadata_var_regex = Regex::new(metadata_var_pattern).unwrap();
343-
let metadata_var = find_first_occurrence(content, metadata_var_regex);
347+
let metadata_var = find_first_occurrence(content, &metadata_var_regex);
344348

345349
let mod_id = find_mod_id(content);
346350

@@ -377,7 +381,7 @@ mod content {
377381
/// if it exists at all. The abstract flag is not required.
378382
fn check_abstract_flag(content: &str) -> Option<IssueReport> {
379383
let abstract_regex = Regex::new(r#"^\[role="_abstract"\]"#).unwrap();
380-
let abstract_flag = find_first_occurrence(content, abstract_regex);
384+
let abstract_flag = find_first_occurrence(content, &abstract_regex);
381385

382386
// If the file contains an abstract flag, test for the following paragraph
383387
if let Some((line_no, _line)) = abstract_flag {
@@ -621,7 +625,7 @@ mod additional_resources {
621625
)
622626
.unwrap();
623627

624-
find_first_occurrence(content, add_res_regex)
628+
find_first_occurrence(content, &add_res_regex)
625629
}
626630

627631
/// See if the additional resources heading is missing the additional resources flag,
@@ -750,11 +754,11 @@ mod additional_resources {
750754
fn find_mod_id(content: &str) -> Option<(usize, &str)> {
751755
let id_regex = Regex::new(r"^\[id=\S+\]").unwrap();
752756

753-
find_first_occurrence(content, id_regex)
757+
find_first_occurrence(content, &id_regex)
754758
}
755759

756760
/// Search for a predefined regex in a file. If found, return the line number and the line text.
757-
fn find_first_occurrence(content: &str, regex: Regex) -> Option<(usize, &str)> {
761+
fn find_first_occurrence<'a>(content: &'a str, regex: &Regex) -> Option<(usize, &'a str)> {
758762
for (index, line) in content.lines().enumerate() {
759763
if let Some(_occurrence) = regex.find(line) {
760764
return Some((index, line));

0 commit comments

Comments
 (0)