Skip to content

Commit

Permalink
style: enable more lints
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsrm committed Dec 21, 2024
1 parent 1f81738 commit 48b864b
Show file tree
Hide file tree
Showing 449 changed files with 2,883 additions and 1,563 deletions.
31 changes: 31 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,34 @@ doctest = false
[features]
js = ["getrandom/js"]
default = ["image"]

[lints]
workspace = true

[workspace.lints.rust]
explicit_outlives_requirements = "deny"
let-underscore-drop = "deny"
meta-variable-misuse = "deny"
non_ascii_idents = "deny"
non-local-definitions = "deny"
redundant-imports = "deny"
redundant-lifetimes = "deny"
single-use-lifetimes = "deny"
trivial-casts = "deny"
trivial_numeric_casts = "deny"
unit-bindings = "deny"
unsafe-code = "deny" # Will only be allowed in platform-specific modules
unused-import-braces = "deny"
unused-lifetimes = "deny"
unused-macro-rules = "deny"
unused_qualifications = "deny"
variant_size_differences = "deny"

[workspace.lints.clippy]
correctness = { level = "deny", priority = -1 }
style = { level = "warn", priority = -1 }
complexity = { level = "warn", priority = -1 }
perf = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
cargo = { level = "warn", priority = -1 }
suspicious = { level = "warn", priority = -1 }
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ let mut book = umya_spreadsheet::new_file();
### Write file
```rust
let path = std::path::Path::new("./tests/result_files/bbb.xlsx");
let _ = umya_spreadsheet::writer::xlsx::write(&book, path);
let _unused = umya_spreadsheet::writer::xlsx::write(&book, path);
```
### Write file with password
```rust
let path = std::path::Path::new("./tests/result_files/bbb.xlsx");
let _ = umya_spreadsheet::writer::xlsx::write_with_password(&book, path, "password");
let _unused = umya_spreadsheet::writer::xlsx::write_with_password(&book, path, "password");
```
```rust
let from_path = std::path::Path::new("./tests/test_files/aaa.xlsx");
let to_path = std::path::Path::new("./tests/result_files/bbb.xlsx");
let _ = umya_spreadsheet::writer::xlsx::set_password(&from_path, &to_path, "password");
let _unused = umya_spreadsheet::writer::xlsx::set_password(&from_path, &to_path, "password");
```
### Read Value
```rust
Expand Down Expand Up @@ -147,7 +147,7 @@ Pass the book as a ```Spreadsheet``` to modify it in other functions.
```rust

let mut book = umya_spreadsheet::new_file();
let _ = book.new_sheet("Sheet2");
let _unused = book.new_sheet("Sheet2");
update_excel(&mut book);

fn update_excel(book: &mut Spreadsheet) {
Expand Down
9 changes: 6 additions & 3 deletions src/helper/address.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use fancy_regex::Regex;

#[must_use]
pub fn split_address(address: &str) -> (&str, &str) {
address
.rsplit_once('!')
.map(|(sheet_name, range)| (sheet_name.trim_matches(&['\'', '"'][..]), range))
.unwrap_or(("", address))
.map_or(("", address), |(sheet_name, range)| {
(sheet_name.trim_matches(&['\'', '"'][..]), range)
})
}

#[must_use]
pub fn join_address(sheet_name: &str, address: &str) -> String {
if sheet_name.is_empty() {
return address.to_string();
}
format!("{}!{}", sheet_name, address)
format!("{sheet_name}!{address}")
}

#[test]
Expand Down
6 changes: 4 additions & 2 deletions src/helper/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io::BufReader;
use std::io::Read;

#[inline]
#[must_use]
pub fn get_binary_data(path: &str) -> Vec<u8> {
let path = std::path::Path::new(path);
let mut buf = Vec::new();
Expand All @@ -14,11 +15,12 @@ pub fn get_binary_data(path: &str) -> Vec<u8> {
}

#[inline]
#[must_use]
pub fn make_media_object(path: &str) -> MediaObject {
let name = path.split("/").last().unwrap();
let name = path.split('/').last().unwrap();
let mut obj = MediaObject::default();
obj.set_image_data(get_binary_data(path));
obj.set_image_name(name);
obj.set_image_title(name.split(".").next().unwrap_or(""));
obj.set_image_title(name.split('.').next().unwrap_or(""));
obj
}
33 changes: 21 additions & 12 deletions src/helper/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* https://ciintelligence.blogspot.com/2012/02/converting-excel-theme-color-and-tint.html
* <https://ciintelligence.blogspot.com/2012/02/converting-excel-theme-color-and-tint.html>
*/

#[derive(Default, Debug, Clone, PartialEq, PartialOrd)]
Expand All @@ -19,12 +19,14 @@ pub struct MsHlsColor {
const RGBMAX: f64 = 255.0;
const HLSMAX: f64 = 255.0;

#[must_use]
pub fn calc_tint(rgb: &str, tint: f64) -> String {
let mut ms_hls = convert_rgb_to_ms_hls(rgb);
ms_hls.l = calculate_final_lum_value(tint, ms_hls.l as f64);
ms_hls.l = calculate_final_lum_value(tint, f64::from(ms_hls.l));
convert_ms_hls_to_rgb(&ms_hls)
}

#[must_use]
pub fn calculate_final_lum_value(tint: f64, lum: f64) -> i32 {
let lum1 = if tint < 0.0 {
lum * (1.0 + tint)
Expand All @@ -35,6 +37,7 @@ pub fn calculate_final_lum_value(tint: f64, lum: f64) -> i32 {
to_i32(lum1)
}

#[must_use]
pub fn split_rgb(rgb: &str) -> (i32, i32, i32) {
let r = i32::from_str_radix(&rgb[0..2], 16).unwrap();
let g = i32::from_str_radix(&rgb[2..4], 16).unwrap();
Expand All @@ -43,27 +46,30 @@ pub fn split_rgb(rgb: &str) -> (i32, i32, i32) {
}

#[inline]
#[must_use]
pub fn join_rgb(r: i32, g: i32, b: i32) -> String {
format!("{:02X}{:02X}{:02X}", r, g, b)
format!("{r:02X}{g:02X}{b:02X}")
}

#[must_use]
pub fn convert_rgb_to_ms_hls(rgb: &str) -> MsHlsColor {
let hls = convert_rgb_to_hls(rgb);
MsHlsColor {
h: to_i32(hls.h * self::HLSMAX),
l: to_i32(hls.l * self::HLSMAX),
s: to_i32(hls.s * self::HLSMAX),
h: to_i32(hls.h * HLSMAX),
l: to_i32(hls.l * HLSMAX),
s: to_i32(hls.s * HLSMAX),
}
}

#[must_use]
pub fn convert_rgb_to_hls(rgb: &str) -> HlsColor {
let mut hls = HlsColor::default();

let (r_i, g_i, b_i) = split_rgb(rgb);

let r = r_i as f64 / RGBMAX;
let g = g_i as f64 / RGBMAX;
let b = b_i as f64 / RGBMAX;
let r = f64::from(r_i) / RGBMAX;
let g = f64::from(g_i) / RGBMAX;
let b = f64::from(b_i) / RGBMAX;

let mut min = r;
if min > g {
Expand Down Expand Up @@ -115,15 +121,17 @@ pub fn convert_rgb_to_hls(rgb: &str) -> HlsColor {
hls
}

#[must_use]
pub fn convert_ms_hls_to_rgb(ms_hls: &MsHlsColor) -> String {
let hls = HlsColor {
h: (ms_hls.h as f64 / self::HLSMAX),
l: (ms_hls.l as f64 / self::HLSMAX),
s: (ms_hls.s as f64 / self::HLSMAX),
h: (f64::from(ms_hls.h) / HLSMAX),
l: (f64::from(ms_hls.l) / HLSMAX),
s: (f64::from(ms_hls.s) / HLSMAX),
};
convert_hls_to_rgb(&hls)
}

#[must_use]
pub fn convert_hls_to_rgb(hls: &HlsColor) -> String {
if hls.s == 0.0 {
let rtn_l = to_i32(hls.l * RGBMAX);
Expand Down Expand Up @@ -151,6 +159,7 @@ pub fn convert_hls_to_rgb(hls: &HlsColor) -> String {
join_rgb(rtn_r, rtn_g, rtn_b)
}

#[must_use]
pub fn set_color(t1: f64, t2: f64, t3: f64) -> f64 {
let t3 = positive_decimal_part(t3);

Expand Down
3 changes: 3 additions & 0 deletions src/helper/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub fn column_index_from_string<S: AsRef<str>>(column: S) -> u32 {
}

#[inline]
#[must_use]
pub fn string_from_column_index(column_index: u32) -> String {
assert!(column_index >= 1u32, "Column number starts from 1.");

Expand Down Expand Up @@ -94,10 +95,12 @@ where
}

#[inline]
#[must_use]
pub fn coordinate_from_index(col: u32, row: u32) -> String {
format!("{}{}", string_from_column_index(col), row)
}

#[must_use]
pub fn coordinate_from_index_with_lock(
col: u32,
row: u32,
Expand Down
17 changes: 8 additions & 9 deletions src/helper/crypt.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use super::const_str::*;
use super::const_str::{CERTIFICATE_NS, ENCRYPTION_NS, PASSWORD_NS};
use crate::structs::SheetProtection;
use crate::structs::WorkbookProtection;
use crate::writer::driver::*;
use crate::writer::driver::{write_end_tag, write_new_line, write_start_tag};
use aes::cipher::{block_padding::NoPadding, BlockEncryptMut, KeyIvInit};
use base64::{engine::general_purpose::STANDARD, Engine as _};
use byteorder::{ByteOrder, LittleEndian};
use cfb;
use hmac::{Hmac, Mac};
use quick_xml::events::{BytesDecl, Event};
use quick_xml::Writer;
Expand Down Expand Up @@ -387,7 +386,7 @@ fn hmac(algorithm: &str, key: &[u8], buffers: Vec<&[u8]>) -> Result<Vec<u8>, Str
HmacSha512::new_from_slice(key).unwrap()
}
_ => {
return Err(format!("algorithm {} not supported!", algorithm));
return Err(format!("algorithm {algorithm} not supported!"));
}
};
mac.update(&buffer_concat(buffers));
Expand Down Expand Up @@ -470,7 +469,7 @@ fn hash(algorithm: &str, buffers: Vec<&[u8]>) -> Result<Vec<u8>, String> {
let mut digest = match algorithm {
"SHA512" | "SHA-512" => Sha512::new(),
_ => {
return Err(format!("algorithm {} not supported!", algorithm));
return Err(format!("algorithm {algorithm} not supported!"));
}
};
digest.update(&buffer_concat(buffers)[..]);
Expand Down Expand Up @@ -633,7 +632,7 @@ fn build_encryption_info(
write_end_tag(&mut writer, "keyEncryptors");
write_end_tag(&mut writer, "encryption");

let result = writer.into_inner().into_inner().to_vec();
let result = writer.into_inner().into_inner().clone();
buffer_concat(vec![ENCRYPTION_INFO_PREFIX, &result])
}

Expand All @@ -656,7 +655,7 @@ fn buffer_concat(buffers: Vec<&[u8]>) -> Vec<u8> {
}
fn buffer_copy(buffer1: &mut [u8], buffer2: &[u8]) {
for (i, byte) in buffer2.iter().enumerate() {
let _ = std::mem::replace(&mut buffer1[i], *byte);
let _unused = std::mem::replace(&mut buffer1[i], *byte);
}
}

Expand Down Expand Up @@ -696,7 +695,7 @@ mod tests {
fn test_encrypt() {
let mut file = File::open("./tests/test_files/aaa.xlsx").unwrap();
let mut data = Vec::new();
let _ = file.read_to_end(&mut data).unwrap();
let _unused = file.read_to_end(&mut data).unwrap();

let password = "password";

Expand Down Expand Up @@ -869,7 +868,7 @@ mod tests {
//assert_eq!(&converted, "0d9c888111b40b630b739c95a5f5b6be67c8f96acdd1bee185bd808b507f652760a2e77f63a6ad0c46f985f2bb8dab4fcf9b86d6a40d9c21299bb4ddf788b250");

// XML
let _ = build_encryption_info(
let _unused = build_encryption_info(
&package_salt_value,
package_block_size,
package_key_bits,
Expand Down
9 changes: 7 additions & 2 deletions src/helper/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use chrono::{Duration, NaiveDateTime};
pub const CALENDAR_WINDOWS_1900: &str = "1900";
pub const CALENDAR_MAC_1904: &str = "1904";

#[must_use]
pub fn excel_to_date_time_object(excel_timestamp: f64, time_zone: Option<String>) -> NaiveDateTime {
let _time_zone = match time_zone {
Some(v) => v,
Expand Down Expand Up @@ -42,6 +43,7 @@ fn get_default_timezone() -> String {
}

#[inline]
#[must_use]
pub fn convert_date(
year: i32,
month: i32,
Expand All @@ -54,6 +56,7 @@ pub fn convert_date(
}

#[inline]
#[must_use]
pub fn convert_date_windows_1900(
year: i32,
month: i32,
Expand All @@ -66,6 +69,7 @@ pub fn convert_date_windows_1900(
}

#[inline]
#[must_use]
pub fn convert_date_mac_1904(
year: i32,
month: i32,
Expand All @@ -77,6 +81,7 @@ pub fn convert_date_mac_1904(
convert_date_crate(year, month, day, hours, minutes, seconds, false)
}

#[must_use]
pub fn convert_date_crate(
year: i32,
month: i32,
Expand Down Expand Up @@ -114,8 +119,8 @@ pub fn convert_date_crate(
+ is_leap_year;

// Calculate the time portion of the date
let time_in_days = ((hours * 3600 + minutes * 60 + seconds) as f64) / 86400.0;
let time_in_days = f64::from(hours * 3600 + minutes * 60 + seconds) / 86400.0;

// Return the final Excel date and time
julian_date as f64 + time_in_days
f64::from(julian_date) + time_in_days
}
Loading

0 comments on commit 48b864b

Please sign in to comment.