Skip to content

Commit f155a7c

Browse files
committed
clang::Entry constructor simplified
1 parent 664c464 commit f155a7c

File tree

2 files changed

+74
-26
lines changed

2 files changed

+74
-26
lines changed

bear/src/semantic/clang/converter.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,21 @@ impl CommandConverter {
7777
.map(|source_file| {
7878
let command_args = Self::build_command_args_for_source(cmd, &source_file);
7979

80-
Entry::new(
81-
source_file,
82-
command_args,
83-
&cmd.working_dir,
84-
output_file.as_ref(),
85-
self.format.command_field_as_array,
86-
)
80+
if self.format.command_field_as_array {
81+
Entry::with_arguments(
82+
source_file,
83+
command_args,
84+
&cmd.working_dir,
85+
output_file.as_ref(),
86+
)
87+
} else {
88+
Entry::with_command(
89+
source_file,
90+
command_args,
91+
&cmd.working_dir,
92+
output_file.as_ref(),
93+
)
94+
}
8795
})
8896
.collect()
8997
}

bear/src/semantic/clang/mod.rs

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,35 @@ pub struct Entry {
5757
}
5858

5959
impl Entry {
60-
/// Create an Entry from arguments or as a command string.
61-
pub fn new(
60+
/// Create an Entry with the arguments field populated.
61+
pub fn with_arguments(
6262
file: impl Into<path::PathBuf>,
6363
arguments: Vec<String>,
6464
directory: impl Into<path::PathBuf>,
6565
output: Option<impl Into<path::PathBuf>>,
66-
as_array: bool,
6766
) -> Self {
68-
if as_array {
69-
Entry {
70-
file: file.into(),
71-
arguments,
72-
command: String::default(),
73-
directory: directory.into(),
74-
output: output.map(|o| o.into()),
75-
}
76-
} else {
77-
Entry {
78-
file: file.into(),
79-
arguments: Vec::default(),
80-
command: shell_words::join(&arguments),
81-
directory: directory.into(),
82-
output: output.map(|o| o.into()),
83-
}
67+
Entry {
68+
file: file.into(),
69+
arguments,
70+
command: String::default(),
71+
directory: directory.into(),
72+
output: output.map(|o| o.into()),
73+
}
74+
}
75+
76+
/// Create an Entry with the command field populated.
77+
pub fn with_command(
78+
file: impl Into<path::PathBuf>,
79+
arguments: Vec<String>,
80+
directory: impl Into<path::PathBuf>,
81+
output: Option<impl Into<path::PathBuf>>,
82+
) -> Self {
83+
Entry {
84+
file: file.into(),
85+
arguments: Vec::default(),
86+
command: shell_words::join(&arguments),
87+
directory: directory.into(),
88+
output: output.map(|o| o.into()),
8489
}
8590
}
8691

@@ -221,4 +226,39 @@ mod tests {
221226
assert_eq!(err, expected_error);
222227
}
223228
}
229+
230+
#[test]
231+
fn test_entry_with_arguments_constructor() {
232+
let entry = Entry::with_arguments(
233+
"main.cpp",
234+
vec!["clang".to_string(), "-c".to_string()],
235+
"/tmp",
236+
Some("main.o"),
237+
);
238+
239+
assert!(!entry.arguments.is_empty());
240+
assert!(entry.command.is_empty());
241+
assert_eq!(entry.file, std::path::PathBuf::from("main.cpp"));
242+
assert_eq!(entry.directory, std::path::PathBuf::from("/tmp"));
243+
assert_eq!(entry.output, Some(std::path::PathBuf::from("main.o")));
244+
assert!(entry.validate().is_ok());
245+
}
246+
247+
#[test]
248+
fn test_entry_with_command_constructor() {
249+
let entry = Entry::with_command(
250+
"main.cpp",
251+
vec!["clang".to_string(), "-c".to_string()],
252+
"/tmp",
253+
Some("main.o"),
254+
);
255+
256+
assert!(entry.arguments.is_empty());
257+
assert!(!entry.command.is_empty());
258+
assert_eq!(entry.command, "clang -c");
259+
assert_eq!(entry.file, std::path::PathBuf::from("main.cpp"));
260+
assert_eq!(entry.directory, std::path::PathBuf::from("/tmp"));
261+
assert_eq!(entry.output, Some(std::path::PathBuf::from("main.o")));
262+
assert!(entry.validate().is_ok());
263+
}
224264
}

0 commit comments

Comments
 (0)