Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies and lints #19

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .codespellignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
crate
mis
wll
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
rev: v2.2.6
hooks:
- id: codespell
args:
Expand All @@ -48,11 +48,11 @@ repos:
warnings,
]
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.6.0
rev: v0.10.0
hooks:
- id: markdownlint-cli2
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.4
rev: v3.0.3
hooks:
- id: prettier
# We use markdowncli
Expand All @@ -66,6 +66,6 @@ repos:
hooks:
- id: sourceheaders
- repo: https://github.com/jorisroovers/gitlint
rev: v0.19.0dev
rev: v0.19.1
hooks:
- id: gitlint
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ categories = ["parser-implementations"]
edition = "2021"

[dependencies]
nom = "7"
base64 = "0.13"
thiserror = "1"
nom = "7.1"
base64 = "0.21"
thiserror = "1.0"

[dev-dependencies]
id3 = "1"
textwrap = "0.14"
id3 = "1.8"
textwrap = "0.16"
32 changes: 18 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,25 @@
//! possible, but since this feature is still under development, the API is *not* stable yet and
//! might change in the future.

// Opt-in for allowed-by-default lints (in alphabetical order)
// See also: <https://doc.rust-lang.org/rustc/lints>
#![warn(future_incompatible)]
#![warn(let_underscore)]
#![warn(missing_debug_implementations)]
//#![warn(missing_docs)] // TODO
#![warn(rust_2018_idioms)]
#![warn(rust_2021_compatibility)]
#![warn(unreachable_pub)]
#![warn(unsafe_code)]
#![cfg_attr(not(debug_assertions), deny(warnings))]
#![deny(rust_2018_idioms)]
#![deny(rust_2021_compatibility)]
#![deny(missing_debug_implementations)]
// TODO: Add missing docs
//#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(clippy::all)]
#![deny(clippy::explicit_deref_methods)]
#![deny(clippy::explicit_into_iter_loop)]
#![deny(clippy::explicit_iter_loop)]
#![deny(clippy::must_use_candidate)]
#![cfg_attr(not(test), deny(clippy::panic_in_result_fn))]
#![cfg_attr(not(debug_assertions), deny(clippy::used_underscore_binding))]
#![warn(unused)]
// Clippy lints
//#![warn(clippy::pedantic)] // TODO
// Additional restrictions
#![warn(clippy::clone_on_ref_ptr)]
//#![warn(clippy::missing_const_for_fn)] // TODO
// Relaxations
#![allow(clippy::module_name_repetitions)] // OK
#![allow(clippy::needless_pub_self)] // TODO

pub mod error;
pub mod library;
Expand Down
2 changes: 1 addition & 1 deletion src/library/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn take_u16_bytes(input: &[u8]) -> Res<&[u8], Vec<u16>> {

fn parse_u16_text(input: &[u8]) -> Res<&[u8], String> {
let (input, bytes) = nom::combinator::all_consuming(take_u16_bytes)(input)?;
let text = std::char::decode_utf16(bytes.into_iter())
let text = std::char::decode_utf16(bytes)
.map(|r| r.unwrap_or(std::char::REPLACEMENT_CHARACTER))
.collect::<String>();
Ok((input, text))
Expand Down
19 changes: 14 additions & 5 deletions src/tag/format/enveloped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

//! Helper for FLAC and MP4 tags

use std::io;
use std::io::Cursor;

use base64::Engine as _;

use super::Tag;
use crate::error::Error;
use crate::util::{take_utf8, Res};
use std::io;
use std::io::Cursor;

pub trait EnvelopedTag: Tag {
fn parse_enveloped(input: &[u8]) -> Result<Self, Error> {
Expand Down Expand Up @@ -49,14 +52,18 @@ pub fn take_base64_with_newline(input: &[u8]) -> Res<&[u8], &[u8]> {
nom::bytes::complete::take_while(|b| is_base64(b) || is_newline(b))(input)
}

const BASE64_FORGIVING: base64::Config = base64::STANDARD_NO_PAD.decode_allow_trailing_bits(true);
const BASE64_FORGIVING: base64::engine::GeneralPurpose =
base64::engine::general_purpose::GeneralPurpose::new(
&base64::alphabet::STANDARD,
base64::engine::general_purpose::NO_PAD.with_decode_allow_trailing_bits(true),
);

pub fn base64_decode(input: &[u8]) -> Result<Vec<u8>, Error> {
let mut encoded: Vec<u8> = input.iter().filter(|&b| !is_newline(*b)).copied().collect();
if encoded.len() % 4 != 2 {
encoded.pop();
}
let decoded = base64::decode_config(encoded, BASE64_FORGIVING);
let decoded = BASE64_FORGIVING.decode(encoded);
match decoded {
Ok(data) => Ok(data),
Err(e) => Err(Error::Base64DecodeError { source: e }),
Expand All @@ -70,7 +77,9 @@ pub fn base64_encode(writer: &mut impl io::Write, input: &[u8]) -> Result<usize,
for (i, chunk) in chunks.enumerate() {
let mut buf = Vec::new();
buf.resize(72, 0);
let bytes_encoded = base64::encode_config_slice(chunk, BASE64_FORGIVING, &mut buf);
let bytes_encoded = BASE64_FORGIVING
.encode_slice(chunk, &mut buf)
.expect("should never fail");
bytes_written += writer.write(&buf[..bytes_encoded])?;
if i == last_chunk_index {
if bytes_encoded % 4 != 2 {
Expand Down
17 changes: 11 additions & 6 deletions src/tag/markers2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
//!
//! The minimum length of this tag seems to be 470 bytes, and shorter contents are padded with null bytes.

use std::io;
use std::io::Cursor;

use base64::Engine as _;
use nom::error::ParseError;

use super::color::Color;
use super::format::{enveloped, flac, id3, mp4, ogg, Tag};
use super::generic::{
Expand All @@ -23,9 +29,6 @@ use super::generic::{
use super::util::{take_color, take_version, write_color, write_version};
use crate::error::Error;
use crate::util::{take_utf8, Res, NULL};
use nom::error::ParseError;
use std::io;
use std::io::Cursor;

/// A marker in the `Serato Markers2` tag.
///
Expand Down Expand Up @@ -263,12 +266,14 @@ fn decode_base64_chunks(
}
let mut buf = [0; 54];
// TODO: Add proper error handling here
let mut res = base64::decode_config_slice(chunk, base64::STANDARD, &mut buf);
if let Err(base64::DecodeError::InvalidLength) = res {
let res = base64::engine::general_purpose::STANDARD.decode_slice(chunk, &mut buf);
if let Err(base64::DecodeSliceError::OutputSliceTooSmall) = res {
let mut v = Vec::new();
v.extend_from_slice(chunk);
v.push(b'A');
res = base64::decode_config_slice(v.as_slice(), base64::STANDARD, &mut buf);
base64::engine::general_purpose::STANDARD
.decode_slice(chunk, &mut buf)
.expect("should never fail");
}
let num_bytes = res.unwrap();
decoded_data.extend_from_slice(&buf[..num_bytes]);
Expand Down
8 changes: 4 additions & 4 deletions src/tag/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use nom::bytes::complete::take;
use std::io;

/// Returns a `Color` struct parsed from the first 3 input bytes.
pub fn take_color(input: &[u8]) -> Res<&[u8], Color> {
pub(crate) fn take_color(input: &[u8]) -> Res<&[u8], Color> {
let (input, bytes) = nom::bytes::complete::take(3usize)(input)?;
let (bytes, red) = nom::number::complete::u8(bytes)?;
let (bytes, green) = nom::number::complete::u8(bytes)?;
Expand Down Expand Up @@ -51,13 +51,13 @@ fn test_take_color() {
assert!(take_color(&[0xAB, 0xCD]).is_err());
}

pub fn write_color(writer: &mut impl io::Write, color: Color) -> Result<usize, Error> {
pub(crate) fn write_color(writer: &mut impl io::Write, color: Color) -> Result<usize, Error> {
let Color { blue, green, red } = color;
Ok(writer.write(&[red, green, blue])?)
}

/// Returns a `Version` struct parsed from the first 2 input bytes.
pub fn take_version(input: &[u8]) -> Res<&[u8], Version> {
pub(crate) fn take_version(input: &[u8]) -> Res<&[u8], Version> {
let (input, version) = take(2usize)(input)?;
Ok((
input,
Expand All @@ -81,7 +81,7 @@ fn test_take_version() {
assert!(take_version(&[0x0A]).is_err());
}

pub fn write_version(writer: &mut impl io::Write, version: Version) -> Result<usize, Error> {
pub(crate) fn write_version(writer: &mut impl io::Write, version: Version) -> Result<usize, Error> {
let Version { major, minor } = version;
Ok(writer.write(&[major, minor])?)
}
8 changes: 4 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

use nom::bytes::complete::take_until;

pub type Res<T, U> = nom::IResult<T, U, nom::error::VerboseError<T>>;
pub(crate) type Res<T, U> = nom::IResult<T, U, nom::error::VerboseError<T>>;

pub(crate) const NULL: &[u8] = &[0x00];

/// Returns the input slice until the first occurrence of a null byte.
pub fn take_until_nullbyte(input: &[u8]) -> Res<&[u8], &[u8]> {
pub(crate) fn take_until_nullbyte(input: &[u8]) -> Res<&[u8], &[u8]> {
take_until(NULL)(input)
}

Expand All @@ -32,7 +32,7 @@ fn test_take_until_nullbyte() {
assert!(take_until_nullbyte(&[0xAB, 0xCD]).is_err());
}

pub fn parse_utf8(input: &[u8]) -> Res<&[u8], &str> {
pub(crate) fn parse_utf8(input: &[u8]) -> Res<&[u8], &str> {
std::str::from_utf8(input)
.map(|s| (&b""[..], s))
.map_err(|_| nom::Err::Incomplete(nom::Needed::Unknown))
Expand All @@ -43,7 +43,7 @@ fn test_parse_utf8() {
assert_eq!(parse_utf8(&[0x41, 0x42]), Ok((&b""[..], "AB")));
}

pub fn take_utf8(input: &[u8]) -> Res<&[u8], &str> {
pub(crate) fn take_utf8(input: &[u8]) -> Res<&[u8], &str> {
let (input, data) = take_until_nullbyte(input)?;
let (_, value) = parse_utf8(data)?;
let (input, _) = nom::bytes::complete::take(1usize)(input)?;
Expand Down