diff --git a/Makefile b/Makefile index be120aa..b7b8a4d 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ checknostd: cd ensure_no_std && cargo rustc -- -C link-arg=-nostartfiles check: - cargo clippy -- -W clippy::all -W clippy::correctness -W clippy::suspicious -W clippy::complexity -W clippy::perf -W clippy::style -W clippy::pedantic -A clippy::missing_errors_doc -A clippy::missing_panics_doc -A clippy::doc_markdown -A clippy::wildcard_imports -D warnings + cargo clippy -- -W clippy::all -W clippy::correctness -W clippy::suspicious -W clippy::complexity -W clippy::perf -W clippy::style -W clippy::pedantic -A clippy::missing_errors_doc -A clippy::missing_panics_doc -A clippy::doc_markdown -A clippy::wildcard_imports -A clippy::module_name_repetitions -D warnings testunit: cargo test --features debug -- --nocapture diff --git a/musicxml/src/datatypes/measure_text.rs b/musicxml/src/datatypes/measure_text.rs index 6423f60..c7e1d89 100644 --- a/musicxml/src/datatypes/measure_text.rs +++ b/musicxml/src/datatypes/measure_text.rs @@ -23,10 +23,10 @@ impl Deref for MeasureText { impl DatatypeDeserializer for MeasureText { fn deserialize(value: &str) -> Result { - if !value.is_empty() { - Ok(MeasureText(String::from(value))) - } else { + if value.is_empty() { Err(format!("Value {value} is invalid for the data type")) + } else { + Ok(MeasureText(String::from(value))) } } } diff --git a/musicxml/src/datatypes/staff_line.rs b/musicxml/src/datatypes/staff_line.rs index 120ca77..0a63cf7 100644 --- a/musicxml/src/datatypes/staff_line.rs +++ b/musicxml/src/datatypes/staff_line.rs @@ -23,6 +23,7 @@ impl DatatypeDeserializer for StaffLine { fn deserialize(value: &str) -> Result { match PositiveInteger::deserialize(value) { Ok(val) => match *val { + #[allow(clippy::cast_possible_truncation)] 1..=255 => Ok(StaffLine(*val as u8)), _ => Err(format!("Value {} is invalid for the data type", *val)), }, diff --git a/musicxml/src/datatypes/staff_number.rs b/musicxml/src/datatypes/staff_number.rs index b723f33..e9c6e1c 100644 --- a/musicxml/src/datatypes/staff_number.rs +++ b/musicxml/src/datatypes/staff_number.rs @@ -23,6 +23,7 @@ impl DatatypeDeserializer for StaffNumber { fn deserialize(value: &str) -> Result { match PositiveInteger::deserialize(value) { Ok(val) => match *val { + #[allow(clippy::cast_possible_truncation)] 1..=255 => Ok(StaffNumber(*val as u8)), _ => Err(format!("Value {} is invalid for the data type", *val)), }, diff --git a/musicxml/src/datatypes/string_number.rs b/musicxml/src/datatypes/string_number.rs index 2d7596f..4900c3e 100644 --- a/musicxml/src/datatypes/string_number.rs +++ b/musicxml/src/datatypes/string_number.rs @@ -23,6 +23,7 @@ impl DatatypeDeserializer for StringNumber { fn deserialize(value: &str) -> Result { match PositiveInteger::deserialize(value) { Ok(val) => match *val { + #[allow(clippy::cast_possible_truncation)] 1..=255 => Ok(StringNumber(*val as u8)), _ => Err(format!("Value {} is invalid for the data type", *val)), }, diff --git a/musicxml/src/datatypes/time_only.rs b/musicxml/src/datatypes/time_only.rs index a9b0cb5..0779c3f 100644 --- a/musicxml/src/datatypes/time_only.rs +++ b/musicxml/src/datatypes/time_only.rs @@ -38,6 +38,7 @@ impl DatatypeDeserializer for TimeOnly { value.split(',').for_each(|item| { if let Ok(token) = PositiveInteger::deserialize(item) { match *token { + #[allow(clippy::cast_possible_truncation)] 1..=255 => res.push(*token as u8), _ => errs = true, } diff --git a/musicxml/src/datatypes/yes_no_number.rs b/musicxml/src/datatypes/yes_no_number.rs index 90fde08..3d62a50 100644 --- a/musicxml/src/datatypes/yes_no_number.rs +++ b/musicxml/src/datatypes/yes_no_number.rs @@ -28,6 +28,7 @@ impl DatatypeSerializer for YesNoNumber { impl DatatypeDeserializer for YesNoNumber { fn deserialize(value: &str) -> Result { if let Ok(dec_val) = Decimal::deserialize(value) { + #[allow(clippy::cast_possible_truncation)] Ok(YesNoNumber::Decimal(*dec_val as f32)) } else if value.to_lowercase() == "yes" { Ok(YesNoNumber::Yes) diff --git a/musicxml/src/datatypes/yyyy_mm_dd.rs b/musicxml/src/datatypes/yyyy_mm_dd.rs index 089b9dd..ea8e952 100644 --- a/musicxml/src/datatypes/yyyy_mm_dd.rs +++ b/musicxml/src/datatypes/yyyy_mm_dd.rs @@ -40,9 +40,9 @@ impl DatatypeDeserializer for YyyyMmDd { let regex = Regex::new(r"([0-9]{4})-([0-9]{2})-([0-9]{2})$").unwrap(); if let Some(captures) = regex.captures(value) { let date = YyyyMmDd::new( - u16::from_str_radix(captures.get(1).unwrap().as_str(), 10).unwrap(), - u8::from_str_radix(captures.get(2).unwrap().as_str(), 10).unwrap(), - u8::from_str_radix(captures.get(3).unwrap().as_str(), 10).unwrap(), + captures.get(1).unwrap().as_str().parse::().unwrap(), + captures.get(2).unwrap().as_str().parse::().unwrap(), + captures.get(3).unwrap().as_str().parse::().unwrap(), ); if date.month > 0 && date.month < 13 && date.date > 0 && date.date < 32 { Ok(date) diff --git a/musicxml/src/elements/note.rs b/musicxml/src/elements/note.rs index a09a209..1ef7369 100644 --- a/musicxml/src/elements/note.rs +++ b/musicxml/src/elements/note.rs @@ -116,7 +116,7 @@ impl ContentDeserializer for GraceNormalInfo { } } Ok(GraceNormalInfo { - chord: chord, + chord, audible: if let Some(audible) = audible { audible } else { @@ -177,7 +177,7 @@ impl ContentDeserializer for GraceCueInfo { } else { Err("Missing element")? }, - chord: chord, + chord, audible: if let Some(audible) = audible { audible } else { @@ -282,7 +282,7 @@ impl ContentDeserializer for CueInfo { } else { Err("Missing element")? }, - chord: chord, + chord, audible: if let Some(audible) = audible { audible } else { @@ -345,7 +345,7 @@ impl ContentDeserializer for NormalInfo { } } Ok(NormalInfo { - chord: chord, + chord, audible: if let Some(audible) = audible { audible } else { diff --git a/musicxml/src/parser/mod.rs b/musicxml/src/parser/mod.rs index 7aa30ab..450beb9 100644 --- a/musicxml/src/parser/mod.rs +++ b/musicxml/src/parser/mod.rs @@ -51,9 +51,8 @@ fn get_musicxml_contents_from_file(path: &str) -> Result { let file = archive .get_file(full_path.as_str()) .ok_or("MXL file missing expected contents")?; - contents = core::str::from_utf8(file.data.as_slice()) - .map_err(|e| e.to_string())? - .to_owned(); + core::str::from_utf8(file.data.as_slice()) + .map_err(|e| e.to_string())?.clone_into(&mut contents); } else { Err(String::from("Cannot find MusicXML file in compressed archive"))?; } @@ -111,6 +110,7 @@ fn write_musicxml_to_file(path: &str, xml: &XmlElement, compressed: bool, pretty let mut file = std::fs::OpenOptions::new() .write(true) .create(true) + .truncate(true) .open(path) .map_err(|e| e.to_string())?; write_musicxml_file(&mut file, xml, pretty_print) diff --git a/musicxml/src/parser/xml_parser.rs b/musicxml/src/parser/xml_parser.rs index f516585..4e5326f 100644 --- a/musicxml/src/parser/xml_parser.rs +++ b/musicxml/src/parser/xml_parser.rs @@ -35,8 +35,7 @@ fn read_tag_str(str: &mut core::str::Chars) -> TagType { TagType::Opening(tag) }; } - '\r' => (), - '\n' => (), + '\r' | '\n' => (), '/' => { if in_string { value.push(c); diff --git a/musicxml/src/parser/zip_parser.rs b/musicxml/src/parser/zip_parser.rs index 039dc8a..080b079 100644 --- a/musicxml/src/parser/zip_parser.rs +++ b/musicxml/src/parser/zip_parser.rs @@ -2,7 +2,7 @@ use alloc::{collections::BTreeMap, vec::Vec}; use core2::io::Read; -use libflate::deflate::{Decoder, Encoder}; +use libflate::deflate::Decoder; const DEFLATE_METHOD: u16 = 8; const LOCAL_FILE_HEADER_LEN: usize = core::mem::size_of::(); @@ -232,14 +232,14 @@ impl<'a> Iterator for ZipParser<'a> { self.buffer_len -= entry_length; // Decompress the file if its method is DEFLATE - if compression_method != DEFLATE_METHOD { - self.next() - } else { + if compression_method == DEFLATE_METHOD { Some(LocalFile::new( file_name, file_header, LocalFileHeader::from_bytes(file_header), )) + } else { + self.next() } } }