Skip to content

Commit

Permalink
Fix some rustc and clippy warnings (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Lauzier authored Nov 7, 2021
1 parent d932da5 commit 2ccf386
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: -- -D warnings
args: --all --tests -- -D warnings

test-windows:
runs-on: windows-latest
Expand Down
100 changes: 51 additions & 49 deletions src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use crate::error::*;
use crate::opt::Opt;
use crate::table::Table;
use crate::types::PartitionTypeGUID;
use crate::uuid::{convert_str_to_array, generate_random_uuid, UUID};
use crate::uuid::{convert_str_to_array, generate_random_uuid, Uuid};
#[cfg(target_os = "linux")]
use gptman::linux::reread_partition_table;
use gptman::{GPTPartitionEntry, GPT};
use std::fs;
use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;
use std::path::{Path, PathBuf};

const BYTE_UNITS: &[&str] = &["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];

Expand Down Expand Up @@ -57,10 +57,10 @@ where
let disk_order = command == "P";

if args.is_empty() {
print(&opt, &opt.device, gpt, len, disk_order)?;
print(opt, &opt.device, gpt, len, disk_order)?;
} else {
for path in args {
match open_and_print(&opt, &path.into(), disk_order) {
match open_and_print(opt, path.as_ref(), disk_order) {
Ok(()) => {}
Err(err) => println!("could not open {:?}: {}", path, err),
}
Expand All @@ -71,7 +71,7 @@ where
"d" => delete_partition(gpt, ask)?,
"f" => fix_partitions_order(gpt),
"w" => {
write(gpt, &opt)?;
write(gpt, opt)?;
return Ok(true);
}
"t" => change_type(gpt, ask)?,
Expand Down Expand Up @@ -123,7 +123,7 @@ fn help() {
println!();
}

fn open_and_print(opt: &Opt, path: &PathBuf, disk_order: bool) -> Result<()> {
fn open_and_print(opt: &Opt, path: &Path, disk_order: bool) -> Result<()> {
let mut f = fs::File::open(path)?;
let len = f.seek(SeekFrom::End(0))?;
let gpt = GPT::find_from(&mut f)?;
Expand Down Expand Up @@ -241,7 +241,7 @@ where
}

fn parse_lba(gpt: &GPT, value: &str, min: u64, max: u64) -> Result<u64> {
let n = u64::from_str_radix(value.trim_end_matches(char::is_alphabetic), 10)?;
let n = value.trim_end_matches(char::is_alphabetic).parse::<u64>()?;
let unit = (*value)
.to_uppercase()
.as_str()
Expand All @@ -264,7 +264,7 @@ fn parse_lba(gpt: &GPT, value: &str, min: u64, max: u64) -> Result<u64> {
"EB" => (n * 1000_u64.pow(6) - 1) / gpt.sector_size + 1,
"ZB" => (n * 1000_u64.pow(7) - 1) / gpt.sector_size + 1,
"YB" => (n * 1000_u64.pow(8) - 1) / gpt.sector_size + 1,
"" => u64::from_str_radix(value, 10)?,
"" => value.parse::<u64>()?,
x => return Err(Error::new(&format!("Invalid unit: {}", x))),
};
let aligned_up = ((result - 1) / gpt.align + 1) * gpt.align;
Expand Down Expand Up @@ -378,7 +378,7 @@ where
Ok(())
}

pub fn print(opt: &Opt, path: &PathBuf, gpt: &GPT, len: u64, disk_order: bool) -> Result<()> {
pub fn print(opt: &Opt, path: &Path, gpt: &GPT, len: u64, disk_order: bool) -> Result<()> {
use crate::opt::Column;

let usable = gpt.header.last_usable_lba - gpt.header.first_usable_lba + 1;
Expand Down Expand Up @@ -565,47 +565,49 @@ where
match ask("Partition type GUID (type L to list all types):")?.as_ref() {
"" => {}
"q" => break,
"L" => loop {
println!("Category:");
for (i, cat) in categories.iter().enumerate() {
println!("{:2} => {}", i + 1, cat);
}
"L" => {
loop {
println!("Category:");
for (i, cat) in categories.iter().enumerate() {
println!("{:2} => {}", i + 1, cat);
}

match ask("Choose category (q to go back):")?.as_ref() {
"" => {}
"q" => break,
i => loop {
if let Some(types_map) = usize::from_str_radix(i, 10)
.ok()
.and_then(|x| categories.get(x - 1))
.and_then(|x| TYPE_MAP.get(*x))
{
let mut types: Vec<_> = types_map.iter().collect();
types.sort_by(|a, b| a.1.cmp(b.1));
let types: Vec<(usize, &(&[u8; 16], &&str))> =
types.iter().enumerate().collect();

println!("Partition types:");
for (i, (guid, name)) in types.iter() {
println!("{:2} => {}: {}", i + 1, guid.display_uuid(), name);
}
match ask("Choose category (q to go back):")?.as_ref() {
"" => {}
"q" => break,
i => loop {
if let Some(types_map) = i
.parse::<usize>()
.ok()
.and_then(|x| categories.get(x - 1))
.and_then(|x| TYPE_MAP.get(*x))
{
let mut types: Vec<_> = types_map.iter().collect();
types.sort_by(|a, b| a.1.cmp(b.1));
let types: Vec<(usize, &(&[u8; 16], &&str))> =
types.iter().enumerate().collect();

println!("Partition types:");
for (i, (guid, name)) in types.iter() {
println!("{:2} => {}: {}", i + 1, guid.display_uuid(), name);
}

match ask("Choose partition type (q to go back):")?.as_ref() {
"" => {}
"q" => break,
i => {
if let Some(arr) = usize::from_str_radix(i, 10)
.ok()
.and_then(|x| types.get(x - 1).map(|(_, (arr, _))| **arr))
{
return Ok(arr);
match ask("Choose partition type (q to go back):")?.as_ref() {
"" => {}
"q" => break,
i => {
if let Some(arr) = i.parse::<usize>().ok().and_then(|x| {
types.get(x - 1).map(|(_, (arr, _))| **arr)
}) {
return Ok(arr);
}
}
}
}
}
},
},
}
}
},
}
x => match convert_str_to_array(x) {
Ok(arr) => return Ok(arr),
Err(err) => {
Expand Down Expand Up @@ -772,13 +774,13 @@ where
Ok(())
}

fn copy_partition<F>(dst_gpt: &mut GPT, dst_path: &PathBuf, ask: &F) -> Result<()>
fn copy_partition<F>(dst_gpt: &mut GPT, dst_path: &Path, ask: &F) -> Result<()>
where
F: Fn(&str) -> Result<String>,
{
let src_path: PathBuf =
match ask(&format!("From disk (default {}):", dst_path.display()))?.as_str() {
"" => dst_path.clone(),
"" => dst_path.to_path_buf(),
x => x.into(),
};
let src_gpt = GPT::find_from(&mut fs::File::open(src_path)?)?;
Expand All @@ -804,7 +806,7 @@ where
Ok(())
}

fn print_raw_data(gpt: &GPT, path: &PathBuf) -> Result<()> {
fn print_raw_data(gpt: &GPT, path: &Path) -> Result<()> {
let mut f = fs::File::open(path)?;

print_table(&mut f, "First sector", 0, gpt.sector_size as u32)?;
Expand Down Expand Up @@ -872,13 +874,13 @@ where
Ok(())
}

fn copy_all_partitions<F>(dst_gpt: &mut GPT, dst_path: &PathBuf, ask: &F) -> Result<()>
fn copy_all_partitions<F>(dst_gpt: &mut GPT, dst_path: &Path, ask: &F) -> Result<()>
where
F: Fn(&str) -> Result<String>,
{
let src_path: PathBuf =
match ask(&format!("From disk (default {}):", dst_path.display()))?.as_str() {
"" => dst_path.clone(),
"" => dst_path.to_path_buf(),
x => x.into(),
};
let src_gpt = GPT::find_from(&mut fs::File::open(src_path)?)?;
Expand Down
2 changes: 2 additions & 0 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::from_str_radix_10)]

mod attribute_bits;
mod commands;
mod error;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::uuid::{convert_str_to_array, UUID};
use crate::uuid::{convert_str_to_array, Uuid};
use lazy_static::lazy_static;
use std::collections::HashMap;

Expand Down
5 changes: 3 additions & 2 deletions src/cli/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ impl fmt::Display for Error {
}
}

pub trait UUID {
pub trait Uuid {
fn display_uuid(&self) -> String;
}

impl UUID for [u8; 16] {
impl Uuid for [u8; 16] {
fn display_uuid(&self) -> String {
let mut digits: Vec<_> = self.iter().collect();
let mut uuid: Vec<String> = Vec::new();
Expand Down Expand Up @@ -68,6 +68,7 @@ pub fn convert_str_to_array(uuid: &str) -> Result<[u8; 16], Error> {
reordered.extend(digits.drain(..2).rev());
reordered.extend(digits.drain(..2).rev());
reordered.extend(digits.drain(..2));
#[allow(clippy::extend_with_drain)]
reordered.extend(digits.drain(..));

for (e, v) in arr.iter_mut().zip(reordered.iter()) {
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,7 @@ impl GPT {
/// ```
pub fn find_free_sectors(&self) -> Vec<(u64, u64)> {
assert!(self.align > 0, "align must be greater than 0");
let mut positions = Vec::new();
positions.push(self.header.first_usable_lba - 1);
let mut positions = vec![self.header.first_usable_lba - 1];
for partition in self.partitions.iter().filter(|x| x.is_used()) {
positions.push(partition.starting_lba);
positions.push(partition.ending_lba);
Expand Down Expand Up @@ -1517,7 +1516,7 @@ mod test {
assert!(gpt.remove(1).is_ok());
gpt.write_into(&mut cur).unwrap();
let maybe_gpt = GPT::read_from(&mut cur, ss);
assert!(maybe_gpt.is_ok(), format!("{:?}", maybe_gpt.err()));
assert!(maybe_gpt.is_ok(), "{:?}", maybe_gpt.err());

gpt.header.crc32_checksum = 1;
cur.seek(SeekFrom::Start(ss)).unwrap();
Expand All @@ -1534,6 +1533,7 @@ mod test {
}

#[test]
#[allow(clippy::manual_swap)]
fn write_invalid_boundaries() {
fn test(path: &str, ss: u64) {
let mut cur = io::Cursor::new(fs::read(path).unwrap());
Expand Down Expand Up @@ -1718,7 +1718,7 @@ mod test {
assert_eq!(data[511], 0xaa);
assert_eq!(data[446 + 4], 0xee);
for (i, x) in data.iter().enumerate() {
if i < 446 || i >= 512 {
if !(446..512).contains(&i) {
assert_eq!(*x, 2);
}
}
Expand Down

0 comments on commit 2ccf386

Please sign in to comment.