Skip to content

Commit 3a58cf9

Browse files
authored
some clippy fixes for cargo builder (#72)
1 parent aacb0ed commit 3a58cf9

File tree

1 file changed

+70
-32
lines changed
  • freertos-cargo-build/src

1 file changed

+70
-32
lines changed

freertos-cargo-build/src/lib.rs

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use cc::Build;
22
use std::ffi::OsStr;
33
use std::fmt::Display;
4-
use std::{fmt, env};
54
use std::path::{Path, PathBuf};
5+
use std::{env, fmt};
66
use walkdir::WalkDir;
77

88
/// The FREERTOS_SRC env variable must point to the FreeRTOS kernel code.
@@ -52,30 +52,33 @@ impl Display for Error {
5252
}
5353
}
5454

55-
56-
impl Builder {
57-
/// Construct a new instance of a blank set of configuration.
58-
///
59-
/// This builder is finished with the [`compile`] function.
60-
///
61-
/// [`compile`]: struct.Build.html#method.compile
62-
pub fn new() -> Builder {
55+
impl Default for Builder {
56+
fn default() -> Self {
6357
let freertos_path = env::var(ENV_KEY_FREERTOS_SRC).unwrap_or_default();
6458
let freertos_config_path = env::var(ENV_KEY_FREERTOS_CONFIG).unwrap_or_default();
6559
let freertos_shim = env::var(ENV_KEY_FREERTOS_SHIM).unwrap_or_default();
6660

67-
let b = Builder {
61+
Self {
6862
freertos_dir: PathBuf::from(freertos_path),
6963
freertos_config_dir: PathBuf::from(freertos_config_path),
7064
freertos_shim: PathBuf::from(freertos_shim),
7165
freertos_port: None,
7266
freertos_port_base: None,
7367
cc: cc::Build::new(),
7468
heap_c: PathBuf::from("heap_4.c"),
75-
};
76-
return b;
69+
}
7770
}
71+
}
7872

73+
impl Builder {
74+
/// Construct a new instance of a blank set of configuration.
75+
///
76+
/// This builder is finished with the [`compile`] function.
77+
///
78+
/// [`compile`]: struct.Build.html#method.compile
79+
pub fn new() -> Builder {
80+
Self::default()
81+
}
7982

8083
/// Set the path to freeRTOS source
8184
/// Default is loaded from ENV variable "FREERTOS_SRC"
@@ -107,8 +110,9 @@ impl Builder {
107110
if f_name.ends_with(".c") {
108111
return Some(entry.path().to_owned());
109112
}
110-
return None;
111-
}).collect();
113+
None
114+
})
115+
.collect();
112116
files
113117
}
114118

@@ -125,8 +129,9 @@ impl Builder {
125129
if f_name.ends_with(".c") {
126130
return Some(entry.path().to_owned());
127131
}
128-
return None;
129-
}).collect();
132+
None
133+
})
134+
.collect();
130135
files
131136
}
132137
fn freertos_port_files(&self) -> Vec<PathBuf> {
@@ -140,8 +145,9 @@ impl Builder {
140145
if f_name.ends_with(".c") {
141146
return Some(entry.path().to_owned());
142147
}
143-
return None;
144-
}).collect();
148+
None
149+
})
150+
.collect();
145151
files
146152
}
147153

@@ -177,10 +183,15 @@ impl Builder {
177183

178184
let target = env::var("TARGET").unwrap_or_default();
179185
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default(); // msvc, gnu, ...
180-
//let target_family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or_default(); // unix, windows
186+
//let target_family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or_default(); // unix, windows
181187
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default(); // x86_64
182188
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); // none, windows, linux, macos
183-
let port = match (target.as_str(), target_arch.as_str(), target_os.as_str(), target_env.as_str()) {
189+
let port = match (
190+
target.as_str(),
191+
target_arch.as_str(),
192+
target_os.as_str(),
193+
target_env.as_str(),
194+
) {
184195
(_, "x86_64", "windows", _) => "MSVC-MingW",
185196
(_, "x86_64", "linux", "gnu") => "GCC/Linux",
186197
("thumbv7m-none-eabi", _, _, _) => "GCC/ARM_CM3",
@@ -190,10 +201,13 @@ impl Builder {
190201
("thumbv8m.main-none-eabi", _, _, _) => "GCC/ARM_CM33_NTZ/non_secure",
191202
("thumbv8m.main-none-eabihf", _, _, _) => "GCC/ARM_CM33_NTZ/non_secure",
192203
_ => {
193-
panic!("Unknown target: '{}', from TARGET environment variable.", target);
204+
panic!(
205+
"Unknown target: '{}', from TARGET environment variable.",
206+
target
207+
);
194208
}
195209
};
196-
return base.join(port);
210+
base.join(port)
197211
}
198212

199213
pub fn freertos_port_base<P: AsRef<Path>>(&mut self, base_dir: P) {
@@ -209,7 +223,9 @@ impl Builder {
209223
}
210224

211225
fn heap_c_file(&self) -> PathBuf {
212-
self.freertos_dir.join("portable/MemMang").join(&self.heap_c)
226+
self.freertos_dir
227+
.join("portable/MemMang")
228+
.join(&self.heap_c)
213229
}
214230
fn shim_c_file(&self) -> PathBuf {
215231
self.freertos_shim.join("shim.c")
@@ -218,37 +234,58 @@ impl Builder {
218234
/// Check that all required files and paths exist
219235
fn verify_paths(&self) -> Result<(), Error> {
220236
if !self.freertos_dir.is_dir() {
221-
return Err(Error::new(&format!("Directory freertos_dir does not exist: {}", self.freertos_dir.to_str().unwrap())));
237+
return Err(Error::new(&format!(
238+
"Directory freertos_dir does not exist: {}",
239+
self.freertos_dir.to_str().unwrap()
240+
)));
222241
}
223242
let port_dir = self.get_freertos_port_dir();
224243
if !port_dir.is_dir() {
225-
return Err(Error::new(&format!("Directory freertos_port_dir does not exist: {}", port_dir.to_str().unwrap())));
244+
return Err(Error::new(&format!(
245+
"Directory freertos_port_dir does not exist: {}",
246+
port_dir.to_str().unwrap()
247+
)));
226248
}
227249

228250
let include_dir = self.freertos_include_dir();
229251
if !include_dir.is_dir() {
230-
return Err(Error::new(&format!("Directory freertos_include_dir does not exist: {}", include_dir.to_str().unwrap())));
252+
return Err(Error::new(&format!(
253+
"Directory freertos_include_dir does not exist: {}",
254+
include_dir.to_str().unwrap()
255+
)));
231256
}
232257

233258
// The heap implementation
234259
let heap_c = self.heap_c_file();
235260
if !heap_c.is_file() {
236-
return Err(Error::new(&format!("File heap_?.c does not exist: {}", heap_c.to_str().unwrap())));
261+
return Err(Error::new(&format!(
262+
"File heap_?.c does not exist: {}",
263+
heap_c.to_str().unwrap()
264+
)));
237265
}
238266

239267
// Allows to find the FreeRTOSConfig.h
240268
if !self.freertos_config_dir.is_dir() {
241-
return Err(Error::new(&format!("Directory freertos_config_dir does not exist: {}", self.freertos_config_dir.to_str().unwrap())));
269+
return Err(Error::new(&format!(
270+
"Directory freertos_config_dir does not exist: {}",
271+
self.freertos_config_dir.to_str().unwrap()
272+
)));
242273
}
243274
// Make sure FreeRTOSConfig.h exists in freertos_config_dir
244275
if !self.freertos_config_dir.join("FreeRTOSConfig.h").is_file() {
245-
return Err(Error::new(&format!("File FreeRTOSConfig.h does not exist in the freertos_config_dir directory: {}", self.freertos_config_dir.to_str().unwrap())));
276+
return Err(Error::new(&format!(
277+
"File FreeRTOSConfig.h does not exist in the freertos_config_dir directory: {}",
278+
self.freertos_config_dir.to_str().unwrap()
279+
)));
246280
}
247281

248282
// Add the freertos shim.c to support freertos-rust
249283
let shim_c = self.shim_c_file();
250284
if !shim_c.is_file() {
251-
return Err(Error::new(&format!("File freertos_shim '{}' does not exist, missing freertos-rust dependency?", shim_c.to_str().unwrap())));
285+
return Err(Error::new(&format!(
286+
"File freertos_shim '{}' does not exist, missing freertos-rust dependency?",
287+
shim_c.to_str().unwrap()
288+
)));
252289
}
253290

254291
Ok(())
@@ -272,7 +309,8 @@ impl Builder {
272309
println!("cargo:rerun-if-env-changed={ENV_KEY_FREERTOS_CONFIG}");
273310
println!("cargo:rerun-if-env-changed={ENV_KEY_FREERTOS_SHIM}");
274311

275-
b.try_compile("freertos").map_err(|e| Error::new(&format!("{}", e)))?;
312+
b.try_compile("freertos")
313+
.map_err(|e| Error::new(&format!("{}", e)))?;
276314

277315
Ok(())
278316
}
@@ -331,7 +369,7 @@ fn add_include_with_rerun<P: AsRef<Path>>(build: &mut Build, dir: P) {
331369
fn test_paths() {
332370
env::set_var("FREERTOS_SRC", "some/path");
333371
env::set_var("TARGET", "thumbv8m.main-none-eabihf");
334-
let mut b = Builder::new();
372+
let b = Builder::new();
335373
assert_eq!(b.freertos_dir.to_str().unwrap(), "some/path");
336374
}
337375
/*

0 commit comments

Comments
 (0)