Skip to content

Commit 7c4f551

Browse files
committed
Use a map instead of an imperatively built list; some borrowing
1 parent be0e756 commit 7c4f551

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

src/main.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn main() {
9090
assert!(!include_statements.is_empty());
9191

9292
// Generate the populated assembly module
93-
let populated: Module = Input::new(ModuleType::Assembly, title, &options)
93+
let populated: Module = Input::new(&ModuleType::Assembly, title, &options)
9494
.include(include_statements)
9595
.into();
9696

@@ -105,26 +105,18 @@ fn process_module_type(
105105
module_type_str: &str,
106106
options: &Options,
107107
) -> Vec<Module> {
108-
let mut modules_from_type = Vec::new();
109-
110-
for title in titles {
111-
// Convert the string module type to an enum.
112-
// This must be done for each title separately so that the title can own the ModuleType.
113-
let module_type = match module_type_str {
114-
"assembly" => ModuleType::Assembly,
115-
"include-in" => ModuleType::Assembly,
116-
"concept" => ModuleType::Concept,
117-
"procedure" => ModuleType::Procedure,
118-
"reference" => ModuleType::Reference,
119-
_ => unimplemented!(),
120-
};
121-
122-
let module = Module::new(module_type, title, &options);
123-
124-
modules_from_type.push(module);
125-
}
108+
let module_type = match module_type_str {
109+
"assembly" => ModuleType::Assembly,
110+
"include-in" => ModuleType::Assembly,
111+
"concept" => ModuleType::Concept,
112+
"procedure" => ModuleType::Procedure,
113+
"reference" => ModuleType::Reference,
114+
_ => unimplemented!(),
115+
};
116+
117+
let modules_from_type = titles.map(|title| Module::new(&module_type, title, &options));
126118

127-
modules_from_type
119+
modules_from_type.collect()
128120
}
129121

130122
// These tests act as pseudo-integration tests. They let the top-level functions generate
@@ -149,7 +141,7 @@ mod tests {
149141
/// Test that we generate the assembly that we expect.
150142
#[test]
151143
fn test_assembly() {
152-
let mod_type = ModuleType::Assembly;
144+
let mod_type = &ModuleType::Assembly;
153145
let mod_title = "Testing that an assembly forms properly";
154146
let options = basic_options();
155147
let assembly = Module::new(mod_type, mod_title, &options);
@@ -163,7 +155,7 @@ mod tests {
163155
/// Test that we generate the concept module that we expect.
164156
#[test]
165157
fn test_concept_module() {
166-
let mod_type = ModuleType::Concept;
158+
let mod_type = &ModuleType::Concept;
167159
let mod_title = "A title that tests a concept";
168160
let options = basic_options();
169161
let concept = Module::new(mod_type, mod_title, &options);
@@ -176,7 +168,7 @@ mod tests {
176168
/// Test that we generate the procedure module that we expect.
177169
#[test]
178170
fn test_procedure_module() {
179-
let mod_type = ModuleType::Procedure;
171+
let mod_type = &ModuleType::Procedure;
180172
let mod_title = "Testing a procedure";
181173
let options = basic_options();
182174
let procedure = Module::new(mod_type, mod_title, &options);
@@ -189,7 +181,7 @@ mod tests {
189181
/// Test that we generate the reference module that we expect.
190182
#[test]
191183
fn test_reference_module() {
192-
let mod_type = ModuleType::Reference;
184+
let mod_type = &ModuleType::Reference;
193185
let mod_title = "The lines in a reference module";
194186
let options = basic_options();
195187
let reference = Module::new(mod_type, mod_title, &options);

src/module.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use regex::{Regex, RegexBuilder};
77
use crate::Options;
88

99
/// All possible types of the AsciiDoc module
10-
#[derive(Debug, Clone, PartialEq)]
10+
#[derive(Debug, Clone, Copy, PartialEq)]
1111
pub enum ModuleType {
1212
Assembly,
1313
Concept,
@@ -44,14 +44,14 @@ const REFERENCE_TEMPLATE: &str = include_str!("../data/templates/reference.adoc"
4444

4545
/// Construct a basic builder for `Module`, storing information from the user input.
4646
impl Input {
47-
pub fn new(mod_type: ModuleType, title: &str, options: &Options) -> Input {
47+
pub fn new(mod_type: &ModuleType, title: &str, options: &Options) -> Input {
4848
debug!("Processing title `{}` of type `{:?}`", title, mod_type);
4949

5050
let title = String::from(title);
5151
let options = options.clone();
5252

5353
Input {
54-
mod_type,
54+
mod_type: *mod_type,
5555
title,
5656
options,
5757
includes: None,
@@ -330,7 +330,7 @@ impl From<Input> for Module {
330330
/// Convert the `Input` builder struct into the finished `Module` struct.
331331
fn from(input: Input) -> Self {
332332
let module = Module {
333-
mod_type: input.mod_type.clone(),
333+
mod_type: input.mod_type,
334334
title: input.title.clone(),
335335
id: input.id(),
336336
file_name: input.file_name(),
@@ -360,7 +360,7 @@ impl From<Input> for Module {
360360
impl Module {
361361
/// The constructor for the Module struct. Creates a basic version of Module
362362
/// without any optional features.
363-
pub fn new(mod_type: ModuleType, title: &str, options: &Options) -> Module {
363+
pub fn new(mod_type: &ModuleType, title: &str, options: &Options) -> Module {
364364
let input = Input::new(mod_type, title, options);
365365
input.into()
366366
}
@@ -397,7 +397,7 @@ mod tests {
397397
fn check_basic_assembly_fields() {
398398
let options = basic_options();
399399
let assembly = Module::new(
400-
ModuleType::Assembly,
400+
&ModuleType::Assembly,
401401
"A testing assembly with /special-characters*",
402402
&options,
403403
);
@@ -423,12 +423,12 @@ mod tests {
423423
fn check_module_builder_and_new() {
424424
let options = basic_options();
425425
let from_new: Module = Module::new(
426-
ModuleType::Assembly,
426+
&ModuleType::Assembly,
427427
"A testing assembly with /special-characters*",
428428
&options,
429429
);
430430
let from_builder: Module = Input::new(
431-
ModuleType::Assembly,
431+
&ModuleType::Assembly,
432432
"A testing assembly with /special-characters*",
433433
&options,
434434
)
@@ -440,7 +440,7 @@ mod tests {
440440
fn check_detected_path() {
441441
let options = path_options();
442442

443-
let module = Module::new(ModuleType::Procedure, "Testing the detected path", &options);
443+
let module = Module::new(&ModuleType::Procedure, "Testing the detected path", &options);
444444

445445
assert_eq!(
446446
module.include_statement,

0 commit comments

Comments
 (0)