Skip to content

Commit 1626331

Browse files
committed
me + part: fix Clippy issues
Signed-off-by: Daniel Maslowski <info@orangecms.org>
1 parent e4a3022 commit 1626331

File tree

6 files changed

+68
-96
lines changed

6 files changed

+68
-96
lines changed

src/me.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl FPTArea {
8080
Gen2Partition::Dir(d) => Some(d.clone()),
8181
_ => None,
8282
})
83-
.collect::<Vec<DirPartition>>();
83+
.collect::<Vec<Box<DirPartition>>>();
8484
dirs.iter()
8585
.map(|d| (d.entry.name(), d.check_signature()))
8686
.collect()
@@ -104,14 +104,14 @@ impl FPTArea {
104104
pub fn check_ftpr_presence(&self) -> Result<(), String> {
105105
match &self.partitions {
106106
Partitions::Gen2(parts) => {
107-
if parts.iter().find(|p| p.entry().name() == FTPR).is_some() {
107+
if parts.iter().any(|p| p.entry().name() == FTPR) {
108108
Ok(())
109109
} else {
110110
Err("not found".into())
111111
}
112112
}
113113
Partitions::Gen3(parts) => {
114-
if parts.iter().find(|p| p.entry().name() == FTPR).is_some() {
114+
if parts.iter().any(|p| p.entry().name() == FTPR) {
115115
Ok(())
116116
} else {
117117
Err("not found".into())
@@ -190,10 +190,7 @@ impl FPTArea {
190190
println!("Recreate ME region from components");
191191
}
192192

193-
let mut res = match self.partitions.to_vec() {
194-
Ok(r) => r,
195-
Err(e) => return Err(e),
196-
};
193+
let mut res = self.partitions.to_vec()?;
197194
if debug {
198195
println!(" Minimum size: {:08x}", res.len());
199196
}

src/part/fpt.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use zerocopy::{AlignmentError, ConvertError, FromBytes, IntoBytes, Ref, SizeErro
2424
use zerocopy_derive::{FromBytes, Immutable, IntoBytes};
2525

2626
use crate::{
27-
part::part::{retain, ClearOptions},
28-
ver::Version,
2927
EMPTY,
28+
part::part::{ClearOptions, retain},
29+
ver::Version,
3030
};
3131

3232
pub const FTPR: &str = "FTPR";
@@ -142,16 +142,13 @@ impl Display for FPTHeader {
142142
}
143143
}
144144

145+
type EntryConvertError<'a> =
146+
ConvertError<AlignmentError<&'a [u8], [FPTEntry]>, SizeError<&'a [u8], [FPTEntry]>, Infallible>;
147+
145148
#[derive(Debug)]
146149
pub enum FptError<'a> {
147150
ParseHeaderError(SizeError<&'a [u8], FPTHeader>),
148-
ParseEntryError(
149-
ConvertError<
150-
AlignmentError<&'a [u8], [FPTEntry]>,
151-
SizeError<&'a [u8], [FPTEntry]>,
152-
Infallible,
153-
>,
154-
),
151+
ParseEntryError(EntryConvertError<'a>),
155152
}
156153

157154
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
@@ -293,22 +290,20 @@ const POSSIBLE_OFFSET: usize = 16;
293290
fn determine_offset(data: &[u8]) -> Option<usize> {
294291
let m = &data[..FPT_MAGIC_BYTES.len()];
295292
if m.eq(FPT_MAGIC_BYTES) {
296-
return Some(0);
293+
Some(0)
297294
} else {
298295
let m = &data[POSSIBLE_OFFSET..POSSIBLE_OFFSET + FPT_MAGIC_BYTES.len()];
299296
if m.eq(FPT_MAGIC_BYTES) {
300-
return Some(POSSIBLE_OFFSET);
297+
Some(POSSIBLE_OFFSET)
301298
} else {
302-
return None;
299+
None
303300
}
304301
}
305302
}
306303

307304
impl<'a> FPT {
308305
pub fn parse(data: &'a [u8]) -> Option<Result<Self, FptError<'a>>> {
309-
let Some(offset) = determine_offset(data) else {
310-
return None;
311-
};
306+
let offset = determine_offset(data)?;
312307
// Save for checksum recalculation
313308
let pre_header = &data[..offset];
314309
let d = &data[offset..];
@@ -353,7 +348,7 @@ impl<'a> FPT {
353348

354349
/// Two's complement of the sum of the bytes
355350
pub fn header_checksum(&self) -> u8 {
356-
let mut c = self.header.clone();
351+
let mut c = self.header;
357352
// Initial checksum field itself must be 0.
358353
c.checksum = 0;
359354
let d = [self.pre_header.as_bytes(), c.as_bytes()].concat();
@@ -373,12 +368,12 @@ impl<'a> FPT {
373368
let f = e.flags;
374369
retain(e.name(), options) && e.size() > 0 && f.kind() != PartitionKind::NVRAM
375370
})
376-
.map(|e| *e)
371+
.copied()
377372
.collect();
378373
self.header.entries = self.entries.len() as u32;
379374
// clear EFFS presence flag if applicable
380375
if (!options.parts_force_retention.contains(&EFFS.into())
381-
&& options.parts_force_deletion.len() == 0)
376+
&& options.parts_force_deletion.is_empty())
382377
|| options.parts_force_deletion.contains(&EFFS.into())
383378
{
384379
// TODO: define bitfield, parameterize via API

src/part/gen2.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ pub struct DirPartition {
2121
impl DirPartition {
2222
pub fn check_signature(&self) -> Result<(), String> {
2323
if self.dir.manifest.verify() {
24-
return Ok(());
24+
Ok(())
2525
} else {
26-
return Err("hash mismatch".into());
26+
Err("hash mismatch".into())
2727
}
2828
}
2929
}
3030

3131
#[derive(Serialize, Deserialize, Clone, Debug)]
3232
pub enum Gen2Partition {
33-
Dir(DirPartition),
33+
Dir(Box<DirPartition>),
3434
Data(DataPartition),
3535
MalformedOrUnknown(UnknownOrMalformedPartition),
3636
}
@@ -71,7 +71,8 @@ impl Gen2Partition {
7171
let o = entry.offset();
7272
let data = data.to_vec();
7373
if let Ok(dir) = Directory::new(&data, o) {
74-
Gen2Partition::Dir(DirPartition { dir, entry, data })
74+
let p = DirPartition { dir, entry, data };
75+
Gen2Partition::Dir(Box::new(p))
7576
} else {
7677
if debug {
7778
let n = entry.name();
@@ -90,9 +91,7 @@ impl Gen2Partition {
9091
// rebase Huffman chunks
9192
let offset_diff = old_offset - offset;
9293
println!("Adjust Huffman LUT, diff: {offset_diff:08x}");
93-
if let Err(e) = p.dir.rebase_huffman_chunks(offset_diff) {
94-
return Err(e);
95-
}
94+
p.dir.rebase_huffman_chunks(offset_diff)?;
9695
if let Some((mod_offset, huffman_mod)) = p.dir.get_huffman_mod() {
9796
let o = mod_offset;
9897
let l = LUT_HEADER_SIZE;
@@ -110,8 +109,7 @@ impl Gen2Partition {
110109
}
111110

112111
pub fn parse(fpt: &FPT, data: &[u8], debug: bool) -> Vec<Gen2Partition> {
113-
let parts = fpt
114-
.entries
112+
fpt.entries
115113
.iter()
116114
.map(|e| {
117115
let offset = e.offset();
@@ -131,13 +129,12 @@ pub fn parse(fpt: &FPT, data: &[u8], debug: bool) -> Vec<Gen2Partition> {
131129
Gen2Partition::parse(&data[offset..end], *e, debug)
132130
}
133131
})
134-
.collect();
135-
parts
132+
.collect()
136133
}
137134

138-
pub fn clean(parts: &Vec<Gen2Partition>, options: &ClearOptions) -> Vec<Gen2Partition> {
135+
pub fn clean(parts: &[Gen2Partition], options: &ClearOptions) -> Vec<Gen2Partition> {
139136
use log::info;
140-
let res = parts
137+
parts
141138
.iter()
142139
.filter(|p| {
143140
let e = p.entry();
@@ -158,16 +155,12 @@ pub fn clean(parts: &Vec<Gen2Partition>, options: &ClearOptions) -> Vec<Gen2Part
158155
// TODO: Extend with user-provided list
159156
let retention_list = strs_to_strings(ALWAYS_RETAIN);
160157
let mut cleaned = p.data().clone();
161-
match &p {
162-
Gen2Partition::Dir(dir) => {
163-
dir_clean(&dir.dir, &retention_list, &mut cleaned);
164-
}
165-
_ => {}
166-
};
158+
if let Gen2Partition::Dir(dir) = &p {
159+
dir_clean(&dir.dir, &retention_list, &mut cleaned);
160+
}
167161
p.set_data(cleaned);
168162
}
169163
p
170164
})
171-
.collect();
172-
res
165+
.collect()
173166
}

src/part/gen3.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ impl CPDPartition {
2323
pub fn check_signature(&self) -> Result<(), String> {
2424
if let Ok(m) = &self.cpd.manifest {
2525
if m.verify() {
26-
return Ok(());
26+
Ok(())
2727
} else {
28-
return Err("hash mismatch".into());
28+
Err("hash mismatch".into())
2929
}
3030
} else {
3131
Err("no manifest found".into())
@@ -139,8 +139,7 @@ impl Gen3Partition {
139139
}
140140

141141
pub fn parse(fpt: &FPT, data: &[u8], debug: bool) -> Vec<Gen3Partition> {
142-
let parts = fpt
143-
.entries
142+
fpt.entries
144143
.iter()
145144
.map(|e| {
146145
let offset = e.offset();
@@ -160,11 +159,10 @@ pub fn parse(fpt: &FPT, data: &[u8], debug: bool) -> Vec<Gen3Partition> {
160159
Gen3Partition::parse(&data[offset..end], *e, debug)
161160
}
162161
})
163-
.collect();
164-
parts
162+
.collect()
165163
}
166164

167-
pub fn clean(parts: &Vec<Gen3Partition>, options: &ClearOptions) -> Vec<Gen3Partition> {
165+
pub fn clean(parts: &[Gen3Partition], options: &ClearOptions) -> Vec<Gen3Partition> {
168166
use log::info;
169167
// Step 1: Reduce down to the partitions to be kept, i.e., non-removable
170168
// ones.
@@ -181,7 +179,7 @@ pub fn clean(parts: &Vec<Gen3Partition>, options: &ClearOptions) -> Vec<Gen3Part
181179
false
182180
}
183181
})
184-
.map(|p| p.clone())
182+
.cloned()
185183
.collect::<Vec<Gen3Partition>>();
186184
if options.keep_modules {
187185
return reduced;
@@ -193,12 +191,9 @@ pub fn clean(parts: &Vec<Gen3Partition>, options: &ClearOptions) -> Vec<Gen3Part
193191
// TODO: Extend with user-provided list
194192
let retention_list = strs_to_strings(ALWAYS_RETAIN);
195193
let mut cleaned = p.data().clone();
196-
match &p {
197-
Gen3Partition::Dir(dir) => {
198-
dir_clean(&dir.cpd, &retention_list, &mut cleaned);
199-
}
200-
_ => {}
201-
};
194+
if let Gen3Partition::Dir(dir) = &p {
195+
dir_clean(&dir.cpd, &retention_list, &mut cleaned);
196+
}
202197
p.set_data(cleaned);
203198
}
204199
// Step 3: Profit.

src/part/part.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ pub struct ClearOptions {
7272
pub fn retain(part_name: String, options: &ClearOptions) -> bool {
7373
part_name == FTPR
7474
|| options.parts_force_retention.contains(&part_name)
75-
|| (options.parts_force_deletion.len() > 0
75+
|| (!options.parts_force_deletion.is_empty()
7676
&& !options.parts_force_deletion.contains(&part_name))
7777
}
7878

7979
/// Clear out removable ranges in the FTPR directory
80-
pub fn dir_clean(dir: &dyn Removables, retention_list: &Vec<String>, data: &mut Vec<u8>) {
80+
pub fn dir_clean(dir: &dyn Removables, retention_list: &[String], data: &mut [u8]) {
8181
use log::info;
82-
for r in dir.removables(&retention_list) {
82+
for r in dir.removables(retention_list) {
8383
let offset = r.start;
8484
let size = r.end - r.start;
8585
info!("Freeing {size:8} bytes @ {offset:08x}");

0 commit comments

Comments
 (0)