Skip to content

Commit

Permalink
Merge pull request #139 from patrickomatic/use-clippy
Browse files Browse the repository at this point in the history
Use Clippy
  • Loading branch information
MathNya authored Nov 6, 2023
2 parents 8ca2b9a + 844652a commit ef48211
Show file tree
Hide file tree
Showing 275 changed files with 5,657 additions and 7,622 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: umya-spreadsheet

on:
push:
branches: ["*"]
pull_request:
branches: ["*"]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Install Rust
run: rustup component add rustfmt clippy

- name: Build
run: cargo build

- name: Lint (clippy)
run: cargo clippy -- -D warnings

- name: Lint (rustfmt)
run: cargo fmt --all -- --check

- name: Tests
run: cargo test
48 changes: 24 additions & 24 deletions src/helper/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,32 @@ pub fn is_address<S: AsRef<str>>(input: S) -> bool {

#[test]
fn is_address_test() {
assert_eq!(is_address("A1"), true);
assert_eq!(is_address("$A1"), true);
assert_eq!(is_address("A$1"), true);
assert_eq!(is_address("$A$1"), true);
assert!(is_address("A1"));
assert!(is_address("$A1"));
assert!(is_address("A$1"));
assert!(is_address("$A$1"));

assert_eq!(is_address("A1:B2"), true);
assert_eq!(is_address("$A1:B2"), true);
assert_eq!(is_address("$A$1:B2"), true);
assert_eq!(is_address("$A$1:$B2"), true);
assert_eq!(is_address("$A$1:$B$2"), true);
assert!(is_address("A1:B2"));
assert!(is_address("$A1:B2"));
assert!(is_address("$A$1:B2"));
assert!(is_address("$A$1:$B2"));
assert!(is_address("$A$1:$B$2"));

assert_eq!(is_address("Sheet1!A1"), true);
assert_eq!(is_address("Sheet1!$A1"), true);
assert_eq!(is_address("Sheet1!A$1"), true);
assert_eq!(is_address("Sheet1!A$1"), true);
assert!(is_address("Sheet1!A1"));
assert!(is_address("Sheet1!$A1"));
assert!(is_address("Sheet1!A$1"));
assert!(is_address("Sheet1!A$1"));

assert_eq!(is_address("Sheet1!A1:B2"), true);
assert_eq!(is_address("Sheet1!$A1:B2"), true);
assert_eq!(is_address("Sheet1!$A$1:B2"), true);
assert_eq!(is_address("Sheet1!$A$1:$B2"), true);
assert_eq!(is_address("Sheet1!$A$1:$B$2"), true);
assert_eq!(is_address("New Sheet!$H$7:$H$10"), true);
assert!(is_address("Sheet1!A1:B2"));
assert!(is_address("Sheet1!$A1:B2"));
assert!(is_address("Sheet1!$A$1:B2"));
assert!(is_address("Sheet1!$A$1:$B2"));
assert!(is_address("Sheet1!$A$1:$B$2"));
assert!(is_address("New Sheet!$H$7:$H$10"));

assert_eq!(is_address("(Sheet1!A1:B2)"), false);
assert_eq!(is_address("Sheet1!A1:"), false);
assert_eq!(is_address("Sheet1!A1:B"), false);
assert_eq!(is_address("Sheet1!A:B2"), false);
assert_eq!(is_address("Sheet1"), false);
assert!(!is_address("(Sheet1!A1:B2)"));
assert!(!is_address("Sheet1!A1:"));
assert!(!is_address("Sheet1!A1:B"));
assert!(!is_address("Sheet1!A:B2"));
assert!(!is_address("Sheet1"));
}
1 change: 0 additions & 1 deletion src/helper/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ where
alpha
.as_ref()
.chars()
.into_iter()
.rev()
.enumerate()
.map(|(index, v)| {
Expand Down
51 changes: 28 additions & 23 deletions src/helper/crypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use hmac::{Hmac, Mac};
use quick_xml::events::{BytesDecl, Event};
use quick_xml::Writer;
use sha2::{Digest, Sha512};
use std::cmp::Ordering;
use std::io;
use std::io::Write;
use std::path::Path;
Expand Down Expand Up @@ -187,6 +188,7 @@ pub fn encrypt<P: AsRef<Path>>(filepath: &P, data: &Vec<u8>, password: &str) {
}

// Encrypt/decrypt the package
#[allow(clippy::too_many_arguments)]
fn crypt_package(
encrypt: &bool,
cipher_algorithm: &str,
Expand Down Expand Up @@ -271,12 +273,16 @@ fn create_iv(
// Create the initialization vector by hashing the salt with the block key.
// Truncate or pad as needed to meet the block size.
let mut iv = hash(hash_algorithm, vec![salt_value, block_key]).unwrap();
if &iv.len() < block_size {
let mut tmp = buffer_alloc(0x36, *block_size);
buffer_copy(&mut tmp, &iv);
iv = tmp;
} else if &iv.len() > block_size {
iv = buffer_slice(&iv, 0, *block_size);
match iv.len().cmp(block_size) {
Ordering::Less => {
let mut tmp = buffer_alloc(0x36, *block_size);
buffer_copy(&mut tmp, &iv);
iv = tmp;
}
Ordering::Greater => {
iv = buffer_slice(&iv, 0, *block_size);
}
_ => {}
}
iv
}
Expand All @@ -287,7 +293,7 @@ fn crypt(
_cipher_algorithm: &str,
_cipher_chaining: &str,
key: &Vec<u8>,
iv: &Vec<u8>,
iv: &[u8],
input: &Vec<u8>,
) -> Result<Vec<u8>, String> {
let mut buf = [0u8; 4096];
Expand All @@ -305,7 +311,7 @@ fn crypt(
Ok(ct.to_vec())
}

fn hmac(algorithm: &str, key: &Vec<u8>, buffers: Vec<&Vec<u8>>) -> Result<Vec<u8>, String> {
fn hmac(algorithm: &str, key: &[u8], buffers: Vec<&Vec<u8>>) -> Result<Vec<u8>, String> {
let mut mac = match algorithm {
"SHA512" => {
type HmacSha512 = Hmac<Sha512>;
Expand Down Expand Up @@ -352,15 +358,15 @@ fn convert_password_to_key(

// Truncate or pad as needed to get to length of keyBits
let key_bytes = key_bits / 8;
if key.len() < key_bytes {
let mut tmp = buffer_alloc(0x36, key_bytes);
buffer_copy(&mut tmp, &key);
key = tmp;
} else if key.len() > key_bytes {
key = buffer_slice(&key, 0, key_bytes);
match key.len().cmp(&key_bytes) {
Ordering::Less => {
let mut tmp = buffer_alloc(0x36, key_bytes);
buffer_copy(&mut tmp, &key);
tmp
}
Ordering::Greater => buffer_slice(&key, 0, key_bytes),
_ => key,
}

key
}

// Calculate a hash of the concatenated buffers with the given algorithm.
Expand Down Expand Up @@ -401,6 +407,7 @@ fn create_uint32_le_buffer(value: &u32, buffer_size: Option<&usize>) -> Vec<u8>
buffer
}

#[allow(clippy::too_many_arguments)]
fn build_encryption_info(
package_salt_value: &Vec<u8>,
package_block_size: &usize,
Expand Down Expand Up @@ -540,7 +547,7 @@ fn build_encryption_info(
buffer_concat(vec![&ENCRYPTION_INFO_PREFIX.to_vec(), &result])
}

fn buffer_slice(buffer: &Vec<u8>, start: usize, end: usize) -> Vec<u8> {
fn buffer_slice(buffer: &[u8], start: usize, end: usize) -> Vec<u8> {
buffer[start..end].to_vec()
}

Expand All @@ -555,19 +562,17 @@ fn buffer_concat(buffers: Vec<&Vec<u8>>) -> Vec<u8> {
}
result
}
fn buffer_copy(buffer1: &mut Vec<u8>, buffer2: &Vec<u8>) {
let mut i = 0;
for byte in buffer2 {
fn buffer_copy(buffer1: &mut [u8], buffer2: &[u8]) {
for (i, byte) in buffer2.iter().enumerate() {
let _ = std::mem::replace(&mut buffer1[i], *byte);
i += 1;
}
}

fn buffer_read_u_int32_le(buffer: &Vec<u8>, _cnt: &usize) -> u32 {
fn buffer_read_u_int32_le(buffer: &[u8], _cnt: &usize) -> u32 {
LittleEndian::read_u32(buffer)
}

fn buffer_write_u_int32_le(buffer: &mut Vec<u8>, value: &u32, _cnt: &usize) {
fn buffer_write_u_int32_le(buffer: &mut [u8], value: &u32, _cnt: &usize) {
LittleEndian::write_u32(buffer, *value);
}

Expand Down
13 changes: 4 additions & 9 deletions src/helper/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ pub fn excel_to_date_time_object(
// Unix timestamp base date
NaiveDateTime::parse_from_str("1970-01-01 00:00:00", "%Y-%m-%d %T").unwrap()
} else {
// MS Excel calendar base dates
if CALENDAR_WINDOWS_1900 == CALENDAR_WINDOWS_1900 {
// Allow adjustment for 1900 Leap Year in MS Excel
if excel_timestamp < &60f64 {
NaiveDateTime::parse_from_str("1899-12-31 00:00:00", "%Y-%m-%d %T").unwrap()
} else {
NaiveDateTime::parse_from_str("1899-12-30 00:00:00", "%Y-%m-%d %T").unwrap()
}
// Allow adjustment for 1900 Leap Year in MS Excel
if excel_timestamp < &60f64 {
NaiveDateTime::parse_from_str("1899-12-31 00:00:00", "%Y-%m-%d %T").unwrap()
} else {
NaiveDateTime::parse_from_str("1904-01-01 00:00:00", "%Y-%m-%d %T").unwrap()
NaiveDateTime::parse_from_str("1899-12-30 00:00:00", "%Y-%m-%d %T").unwrap()
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/helper/formula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn adjustment_insert_formula_coordinate(
worksheet_name: &str,
self_worksheet_name: &str,
) -> String {
let re = Regex::new(r#"[^\(]*!*[A-Z]+[0-9]+\:[A-Z]+[0-9]+"#).unwrap();
let re = Regex::new(r"[^\(]*!*[A-Z]+[0-9]+\:[A-Z]+[0-9]+").unwrap();
let result = re.replace_all(formula, |caps: &Captures| {
let caps_string = caps.get(0).unwrap().as_str().to_string();
let split_str: Vec<&str> = caps_string.split('!').collect();
Expand All @@ -28,7 +28,7 @@ pub fn adjustment_insert_formula_coordinate(
range = split_str.first().unwrap().to_string();
}

if &wksheet != &worksheet_name {
if wksheet != worksheet_name {
return caps_string;
}

Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn adjustment_remove_formula_coordinate(
worksheet_name: &str,
self_worksheet_name: &str,
) -> String {
let re = Regex::new(r#"[^\(]*!*[A-Z]+[0-9]+\:[A-Z]+[0-9]+"#).unwrap();
let re = Regex::new(r"[^\(]*!*[A-Z]+[0-9]+\:[A-Z]+[0-9]+").unwrap();
let result = re.replace_all(formula, |caps: &Captures| {
let caps_string = caps.get(0).unwrap().as_str().to_string();
let split_str: Vec<&str> = caps_string.split('!').collect();
Expand All @@ -95,7 +95,7 @@ pub fn adjustment_remove_formula_coordinate(
range = split_str.first().unwrap().to_string();
}

if &wksheet != &worksheet_name {
if wksheet != worksheet_name {
return caps_string;
}

Expand Down
22 changes: 10 additions & 12 deletions src/helper/number_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::borrow::Cow;
use fancy_regex::Captures;
use fancy_regex::Matches;
use fancy_regex::Regex;
//use fancy_regex::Regex;
use helper::date::*;
use structs::Color;
use structs::NumberingFormat;
Expand All @@ -13,6 +12,7 @@ pub struct Split<'r, 't> {
finder: Matches<'r, 't>,
last: usize,
}

pub fn split<'r, 't>(regex: &'r Regex, text: &'t str) -> Split<'r, 't> {
Split {
finder: regex.find_iter(text),
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn to_formatted_string<S: AsRef<str>, P: AsRef<str>>(value: S, format: P) ->

// Convert any other escaped characters to quoted strings, e.g. (\T to "T")

let mut format = ESCAPE_REGEX.replace_all(&format, r#""$0""#).to_owned();
let mut format = ESCAPE_REGEX.replace_all(&format, r#""$0""#);

// Get the sections, there can be up to four sections, separated with a semi-colon (but only if not a quoted literal)

Expand Down Expand Up @@ -187,7 +187,7 @@ fn split_format(sections: Vec<&str>, value: &f64) -> (String, String, String) {
// 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
let cnt: usize = sections.len();
let color_regex: String = format!("{}{}{}", "\\[(", Color::NAMED_COLORS.join("|"), ")\\]");
let cond_regex = r#"\[(>|>=|<|<=|=|<>)([+-]?\d+([.]\d+)?)\]"#;
let cond_regex = r"\[(>|>=|<|<=|=|<>)([+-]?\d+([.]\d+)?)\]";
let color_re = Regex::new(&color_regex).unwrap();
let cond_re = Regex::new(cond_regex).unwrap();

Expand Down Expand Up @@ -284,14 +284,13 @@ fn split_format_compare(value: &f64, cond: &str, val: &f64, dfcond: &str, dfval:
}

fn format_as_date<'input>(value: &f64, format: &'input str) -> Cow<'input, str> {
let value = value;
let format = Cow::Borrowed(format);

// strip off first part containing e.g. [$-F800] or [$USD-409]
// general syntax: [$<Currency string>-<language info>]
// language info is in hexadecimal
// strip off chinese part like [DBNum1][$-804]
let re = Regex::new(r#"^(\[[0-9A-Za-z]*\])*(\[\$[A-Z]*-[0-9A-F]*\])"#).unwrap();
let re = Regex::new(r"^(\[[0-9A-Za-z]*\])*(\[\$[A-Z]*-[0-9A-F]*\])").unwrap();
let format = re.replace_all(&format, r#""#);

// OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case;
Expand Down Expand Up @@ -351,9 +350,9 @@ fn format_as_number<'input>(value: &f64, format: &'input str) -> Cow<'input, str
static ref THOUSANDS_SEP_REGEX: Regex = Regex::new(r#"(#,#|0,0)"#).unwrap();
static ref SCALE_REGEX: Regex = Regex::new(r#"(#|0)(,+)"#).unwrap();
static ref TRAILING_COMMA_REGEX: Regex = Regex::new("(#|0),+").unwrap();
static ref FRACTION_REGEX: Regex = Regex::new(r#"#?.*\?{1,2}\/\?{1,2}"#).unwrap();
static ref SQUARE_BRACKET_REGEX: Regex = Regex::new(r#"\[[^\]]+\]"#).unwrap();
static ref NUMBER_REGEX: Regex = Regex::new(r#"(0+)(\.?)(0*)"#).unwrap();
static ref FRACTION_REGEX: Regex = Regex::new(r"#?.*\?{1,2}\/\?{1,2}").unwrap();
static ref SQUARE_BRACKET_REGEX: Regex = Regex::new(r"\[[^\]]+\]").unwrap();
static ref NUMBER_REGEX: Regex = Regex::new(r"(0+)(\.?)(0*)").unwrap();
}

let mut value = value.to_string();
Expand Down Expand Up @@ -427,12 +426,12 @@ fn format_as_number<'input>(value: &f64, format: &'input str) -> Cow<'input, str
&format,
&item,
&use_thousands,
r#"(0+)(\.?)(0*)"#,
r"(0+)(\.?)(0*)",
);
}
}

let re = Regex::new(r#"\$[^0-9]*"#).unwrap();
let re = Regex::new(r"\$[^0-9]*").unwrap();
if re.find(&format).ok().flatten().is_some() {
let mut item: Vec<String> = Vec::new();
for ite in re.captures(&format).ok().flatten().unwrap().iter() {
Expand Down Expand Up @@ -545,8 +544,7 @@ fn format_straight_numeric_value(
let pow = 10i32.pow(right.len() as u32);
right_value = format!("{}", right_value.parse::<i32>().unwrap() * pow);
} else {
let mut right_value_conv: String =
right_value.chars().skip(0).take(right.len()).collect();
let mut right_value_conv: String = right_value.chars().take(right.len()).collect();
let ajst_str: String = right_value.chars().skip(right.len()).take(1).collect();
let ajst_int = ajst_str.parse::<i32>().unwrap();
if ajst_int > 4 {
Expand Down
2 changes: 1 addition & 1 deletion src/helper/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ pub fn get_start_and_end_point(range_str: &str) -> (u32, u32, u32, u32) {
}
}
}
return (row_start, row_end, col_start, col_end);
(row_start, row_end, col_start, col_end)
}
Loading

0 comments on commit ef48211

Please sign in to comment.