Skip to content

Commit

Permalink
clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
trou committed Aug 4, 2024
1 parent 7b27b84 commit 9290168
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 45 deletions.
18 changes: 9 additions & 9 deletions src/crcapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ impl Applet for CRCApplet {
}

impl CRCApplet {
fn do_crc(&self, alg_name: &str, val: &Vec<u8>) -> Result<String> {
let alg_size: u8 = (*alg_name.split("_").collect::<Vec<&str>>().get(1).unwrap())
fn do_crc(&self, alg_name: &str, val: &[u8]) -> Result<String> {
let alg_size: u8 = (*alg_name.split('_').collect::<Vec<&str>>().get(1).unwrap())
.parse()
.unwrap();
return match alg_size {
match alg_size {
0..=8 => {
let crc8 = algs!(alg_name; u8;
CRC_3_GSM, CRC_3_ROHC, CRC_4_G_704, CRC_4_INTERLAKEN, CRC_5_EPC_C1G2, CRC_5_G_704, CRC_5_USB,
Expand All @@ -245,7 +245,7 @@ impl CRCApplet {
CRC_8_GSM_B, CRC_8_HITAG, CRC_8_I_432_1, CRC_8_I_CODE, CRC_8_LTE, CRC_8_MAXIM_DOW,
CRC_8_MIFARE_MAD, CRC_8_NRSC_5, CRC_8_OPENSAFETY, CRC_8_ROHC, CRC_8_SAE_J1850, CRC_8_SMBUS,
CRC_8_TECH_3250, CRC_8_WCDMA);
Ok(format!("{:02x}", crc8.checksum(&val)))
Ok(format!("{:02x}", crc8.checksum(val)))
}
9..=16 => {
let crc16 = algs!(alg_name; u16;
Expand All @@ -260,28 +260,28 @@ impl CRCApplet {
CRC_16_OPENSAFETY_B, CRC_16_PROFIBUS, CRC_16_RIELLO,
CRC_16_SPI_FUJITSU, CRC_16_T10_DIF, CRC_16_TELEDISK, CRC_16_TMS37157,
CRC_16_UMTS, CRC_16_USB, CRC_16_XMODEM);
Ok(format!("{:04x}", crc16.checksum(&val)))
Ok(format!("{:04x}", crc16.checksum(val)))
}
17..=32 => {
let crc32 = algs!(alg_name; u32;
CRC_17_CAN_FD, CRC_21_CAN_FD, CRC_24_BLE, CRC_24_FLEXRAY_A, CRC_24_FLEXRAY_B, CRC_24_INTERLAKEN,
CRC_24_LTE_A, CRC_24_LTE_B, CRC_24_OPENPGP, CRC_24_OS_9, CRC_30_CDMA, CRC_31_PHILIPS, CRC_32_AIXM,
CRC_32_AUTOSAR, CRC_32_BASE91_D, CRC_32_BZIP2, CRC_32_CD_ROM_EDC, CRC_32_CKSUM, CRC_32_ISCSI,
CRC_32_ISO_HDLC, CRC_32_JAMCRC, CRC_32_MEF, CRC_32_MPEG_2, CRC_32_XFER);
Ok(format!("{:08x}", crc32.checksum(&val)))
Ok(format!("{:08x}", crc32.checksum(val)))
}
33..=64 => {
let crc64 = algs!(alg_name; u64; CRC_40_GSM, CRC_64_ECMA_182, CRC_64_GO_ISO, CRC_64_MS, CRC_64_REDIS, CRC_64_WE, CRC_64_XZ);
Ok(format!("{:016x}", crc64.checksum(&val)))
Ok(format!("{:016x}", crc64.checksum(val)))
}
65..=128 => {
let crc128 = algs!(alg_name; u128; CRC_82_DARC);
Ok(format!("{:032x}", crc128.checksum(&val)))
Ok(format!("{:032x}", crc128.checksum(val)))
}
_ => {
bail!("Unknown CRC algorithm");
}
};
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/sliceapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pub struct SliceApplet {
* return value: value, plus_prefix, minus_prefix
*/
fn parse_value_with_prefix(s: &String) -> Result<Position> {
if s.len() < 1 {
if s.is_empty() {
bail!("Invalid length for value");
}

let first = s.chars().nth(0).unwrap();
let first = s.chars().next().unwrap();

let str_stripped = &s[1..].to_string();

Expand All @@ -36,7 +36,7 @@ fn parse_value_with_prefix(s: &String) -> Result<Position> {
} else {
(false, false, s)
};
let offset = u64::from_str_with_radix(&str_strip).with_context(|| "Invalid offset value")?;
let offset = u64::from_str_with_radix(str_strip).with_context(|| "Invalid offset value")?;
Ok(Position {
offset,
relative,
Expand Down
62 changes: 29 additions & 33 deletions src/urlapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,39 @@ pub struct UrlEncApplet {
}

// Encoding table according to RFC 3986
fn build_url_table(excluded: &String, table: &mut [bool; 256]) -> () {
fn build_url_table(excluded: &str, table: &mut [bool; 256]) -> () {
for i in 0..255 {
let c = char::from_u32(i).unwrap();
if !c.is_ascii_graphic() {
table[i as usize] = true;
} else {
if matches!(
c,
'!' | '#'
| '$'
| '%'
| '&'
| '\''
| '('
| ')'
| '*'
| '+'
| ','
| '/'
| ':'
| ';'
| '='
| '?'
| '@'
| '['
| ']'
) && !excluded.contains(c)
{
table[i as usize] = true;
} else {
table[i as usize] = false;
}
} else if matches!(
c,
'!' | '#'
| '$'
| '%'
| '&'
| '\''
| '('
| ')'
| '*'
| '+'
| ','
| '/'
| ':'
| ';'
| '='
| '?'
| '@'
| '['
| ']'
) && !excluded.contains(c)
{
table[i as usize] = true;
}
}
}

fn build_custom_table(excluded: &String, custom: &String, table: &mut [bool; 256]) -> () {
fn build_custom_table(excluded: &str, custom: &String, table: &mut [bool; 256]) -> () {
for i in 0..255 {
let c = char::from_u32(i).unwrap();
if custom.contains(c) {
Expand All @@ -60,7 +56,7 @@ fn build_custom_table(excluded: &String, custom: &String, table: &mut [bool; 256
}

// Default is to encode non alpha-numeric (ASCII) chars
fn build_default_table(excluded: &String, table: &mut [bool; 256]) -> () {
fn build_default_table(excluded: &str, table: &mut [bool; 256]) -> () {
for i in 0..255 {
let c = char::from_u32(i).unwrap();
if c.is_ascii_alphanumeric() {
Expand Down Expand Up @@ -111,12 +107,12 @@ impl Applet for UrlEncApplet {
};
let mut table = [false; 256];
if args.get_flag("rfc3986") {
build_url_table(&excluded, &mut table);
build_url_table(excluded, &mut table);
} else if args.contains_id("custom") {
let custom = args.get_one::<String>("custom").unwrap();
build_custom_table(&excluded, &custom, &mut table);
build_custom_table(excluded, custom, &mut table);
} else {
build_default_table(&excluded, &mut table);
build_default_table(excluded, &mut table);
};
Ok(Box::new(Self { table: table }))
}
Expand Down

0 comments on commit 9290168

Please sign in to comment.