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

Crate linter attributes #15

Merged
merged 5 commits into from
Feb 2, 2022
Merged
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
5 changes: 5 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ jobs:
with:
fetch-depth: 2

- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile

- name: Set up Python
uses: actions/setup-python@v2

Expand Down
7 changes: 5 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.1.0
hooks:
- id: check-case-conflict
- id: check-json
Expand All @@ -19,11 +19,14 @@ repos:
- id: fix-byte-order-marker
- id: forbid-new-submodules
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
hooks:
- id: codespell
args: [ --ignore-words=.codespellignore ]
args: [
--ignore-words=.codespellignore
]
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ base64 = "0.13"
thiserror = "1"

[dev-dependencies]
id3 = "0.6"
id3 = "1"
textwrap = "0.14"
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Error types and helper functions.

extern crate base64;
extern crate thiserror;

/// Error enumerates all possible errors returned by this library.
#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@
//! possible, but since this feature is still under development, the API is *not* stable yet and
//! might change in the future.

#![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))]

pub mod error;
pub mod library;
pub mod tag;
Expand Down
2 changes: 0 additions & 2 deletions src/library/database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate nom;

use crate::error::Error;
use crate::util::Res;
use std::path::PathBuf;
Expand Down
18 changes: 14 additions & 4 deletions src/library/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct Track {

impl Track {
/// Creates a new, empty Track object.
#[must_use]
pub fn new() -> Self {
Self {
file_type: None,
Expand Down Expand Up @@ -120,14 +121,15 @@ impl Default for Track {
}

/// DAO that reads Serato libraries from the file system.
#[derive(Debug, Clone)]
pub struct Library {
path: PathBuf,
tracks: HashMap<PathBuf, Track>,
}

impl Library {
/// Read the library in the given path.
pub fn read_from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
pub fn read_from_path_ref(path: &Path) -> Result<Self, Error> {
let path = fs::canonicalize(path)?;
let tracks = HashMap::new();
let mut library = Library { path, tracks };
Expand All @@ -136,6 +138,14 @@ impl Library {
Ok(library)
}

/// Read the library in the given path.
///
/// Convenience function that accepts anything that could be
/// converted into a `Path` reference.
pub fn read_from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
Self::read_from_path_ref(path.as_ref())
}

fn serato_path(&self) -> PathBuf {
self.path.join(SERATO_DIR)
}
Expand All @@ -148,7 +158,7 @@ impl Library {
file.read_to_end(&mut data)?;

let fields = database::parse(&data)?;
let tracks: Result<HashMap<_, _>, Error> = fields
self.tracks = fields
.into_iter()
.filter_map(|field| {
if let database::Field::Track(t) = field {
Expand All @@ -158,8 +168,7 @@ impl Library {
}
})
.map(Track::from_fields)
.collect();
self.tracks = tracks?;
.collect::<Result<HashMap<_, _>, _>>()?;

Ok(())
}
Expand All @@ -170,6 +179,7 @@ impl Library {
}

/// Get the track struct for the given path.
#[must_use]
pub fn track(&self, file_path: &Path) -> Option<&Track> {
self.tracks.get(file_path)
}
Expand Down
2 changes: 1 addition & 1 deletion src/tag/autotags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::io;
/// println!("{:?}", content);
/// }
/// ```
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Autotags {
/// The tag version.
pub version: Version,
Expand Down
6 changes: 3 additions & 3 deletions src/tag/beatgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::io;
///
/// The last beatgrid marker always has to be a terminal one. This is also the case if the tag only
/// contains a single beatgrid marker.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct TerminalMarker {
/// The position in seconds.
pub position: f32,
Expand All @@ -22,7 +22,7 @@ pub struct TerminalMarker {
/// Represents a non-terminal beatgrid marker in the `Serato BeatGrid` tag.
///
/// All beatgrid markers before the last one are non-terminal beatgrid markers.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct NonTerminalMarker {
/// The position in seconds.
pub position: f32,
Expand All @@ -47,7 +47,7 @@ pub struct NonTerminalMarker {
/// println!("{:?}", content);
/// }
/// ```
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Beatgrid {
/// The analysis version.
pub version: Version,
Expand Down
4 changes: 4 additions & 0 deletions src/tag/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl Color {
///
/// assert_eq!(non_pro_color, Color { red: 0xC0, green: 0xFF, blue: 0xEE });
/// ```
#[must_use]
pub fn into_pro_hotcue_color(self) -> Self {
if let Some(index) = HOTCUE_COLORS_INTRO.iter().position(|&x| x == self) {
return HOTCUE_COLORS_PRO[index];
Expand Down Expand Up @@ -151,6 +152,7 @@ impl Color {
///
/// assert_eq!(non_intro_color, Color { red: 0xC0, green: 0xFF, blue: 0xEE });
/// ```
#[must_use]
pub fn into_intro_hotcue_color(self) -> Self {
if let Some(index) = HOTCUE_COLORS_PRO.iter().position(|&x| x == self) {
return HOTCUE_COLORS_INTRO[index];
Expand All @@ -176,6 +178,7 @@ impl Color {
/// let displayed_color = stored_color.into_displayed_track_color();
/// assert_eq!(displayed_color, None);
/// ```
#[must_use]
pub fn into_displayed_track_color(self) -> Option<Self> {
match self {
Color {
Expand Down Expand Up @@ -205,6 +208,7 @@ impl Color {
/// let stored_color = Color::from_displayed_track_color(None);
/// assert_eq!(stored_color, Color { red: 0xFF, green: 0xFF, blue: 0xFF });
/// ```
#[must_use]
pub fn from_displayed_track_color(color: Option<Self>) -> Self {
let code: u32 = match color {
Some(color) => color.into(),
Expand Down
13 changes: 12 additions & 1 deletion src/tag/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::io;
///
/// Some of the data in Serato's tags is redundant and may contradict each other. This class
/// implements the same merge strategies for inconsistent data that Serato uses, too.
#[derive(Debug, Clone)]
pub struct TagContainer {
autotags: Option<Autotags>,
beatgrid: Option<Beatgrid>,
Expand All @@ -26,6 +27,7 @@ pub struct TagContainer {
///
/// The format of the Serato tag data differs between tag types.
/// Therefore it's necessary to tell the parser from what kind of the the data originates from.
#[derive(Debug, Clone, Copy)]
pub enum TagFormat {
ID3,
FLAC,
Expand All @@ -35,7 +37,8 @@ pub enum TagFormat {

impl TagContainer {
/// Create an empty Serato tag container.
pub fn new() -> Self {
#[must_use]
pub const fn new() -> Self {
Self {
autotags: None,
beatgrid: None,
Expand Down Expand Up @@ -219,6 +222,7 @@ impl TagContainer {
}

/// Returns the [`auto_gain`](Autotags::auto_gain) value from the [`Serato Autotags`](Autotags) tag.
#[must_use]
pub fn auto_gain(&self) -> Option<f64> {
if let Some(tag) = &self.autotags {
return Some(tag.auto_gain);
Expand All @@ -228,6 +232,7 @@ impl TagContainer {
}

/// Returns the [`gain_db`](Autotags::gain_db) value from the [`Serato Autotags`](Autotags) tag.
#[must_use]
pub fn gain_db(&self) -> Option<f64> {
if let Some(tag) = &self.autotags {
return Some(tag.gain_db);
Expand All @@ -237,6 +242,7 @@ impl TagContainer {
}

/// Returns the beatgrid from the [`Serato BeatGrid`](Beatgrid) tag.
#[must_use]
pub fn beatgrid(
&self,
) -> Option<(&Vec<beatgrid::NonTerminalMarker>, &beatgrid::TerminalMarker)> {
Expand All @@ -248,6 +254,7 @@ impl TagContainer {
}

/// Returns BPM lock status from the [`Serato Markers2`](Markers2) tag.
#[must_use]
pub fn bpm_locked(&self) -> Option<bool> {
if let Some(m) = &self.markers2 {
return m.bpm_locked();
Expand All @@ -261,6 +268,7 @@ impl TagContainer {
/// This retrieves the `Serato Markers2` cues first, then overwrite the values with those from
/// `Serato Markers_`. This is what Serato does too (i.e. if `Serato Markers_` and `Serato
/// Markers2` contradict each other, Serato will use the values from `Serato Markers_`).
#[must_use]
pub fn cues(&self) -> Vec<generic::Cue> {
let mut map = std::collections::BTreeMap::new();

Expand Down Expand Up @@ -325,6 +333,7 @@ impl TagContainer {
/// This retrieves the `Serato Markers2` loops first, then overwrite the values with those from
/// `Serato Markers_`. This is what Serato does too (i.e. if `Serato Markers_` and `Serato
/// Markers2` contradict each other, Serato will use the values from `Serato Markers_`).
#[must_use]
pub fn loops(&self) -> Vec<generic::Loop> {
let mut map = std::collections::BTreeMap::new();

Expand Down Expand Up @@ -394,6 +403,7 @@ impl TagContainer {
/// `Serato Markers2`. This is what Serato does too, i.e. if `Serato Markers_`
/// and `Serato Markers2` contradict each other, Serato will use the value
/// from `Serato Markers_`.
#[must_use]
pub fn track_color(&self) -> Option<Color> {
self.markers
.as_ref()
Expand All @@ -402,6 +412,7 @@ impl TagContainer {
}

/// Returns the waveform overview data color from the [`Serato Overview`](Overview) tag.
#[must_use]
pub fn overview_data(&self) -> Option<&[Vec<u8>]> {
self.overview.as_ref().map(|overview| &overview.data[..])
}
Expand Down
4 changes: 2 additions & 2 deletions src/tag/format/enveloped.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! Helper for FLAC and MP4 tags
extern crate base64;
extern crate nom;

use super::Tag;
use crate::error::Error;
Expand Down Expand Up @@ -29,10 +27,12 @@ pub fn parse_envelope(input: &[u8]) -> Result<(&str, &[u8]), Error> {
Ok((name, input))
}

#[must_use]
pub fn is_base64(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || byte == b'+' || byte == b'/' || byte == b'='
}

#[must_use]
pub fn is_newline(byte: u8) -> bool {
byte == b'\n'
}
Expand Down
2 changes: 0 additions & 2 deletions src/tag/format/flac.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! FLAC tags
extern crate base64;
extern crate nom;

use super::enveloped::EnvelopedTag;
use crate::error::Error;
Expand Down
3 changes: 0 additions & 3 deletions src/tag/format/mp4.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! MP4 tags

extern crate base64;
extern crate nom;

use super::enveloped::EnvelopedTag;
use crate::error::Error;
use std::io;
Expand Down
7 changes: 5 additions & 2 deletions src/tag/markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::io;
use std::io::Cursor;

/// Represents a single marker in the `Serato Markers_` tag.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Marker {
/// The position of the loop or cue.
pub start_position: Option<Position>,
Expand Down Expand Up @@ -57,7 +57,7 @@ pub struct Marker {
/// println!("{:?}", content);
/// }
/// ```
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Markers {
/// The tag version.
pub version: Version,
Expand All @@ -70,6 +70,7 @@ pub struct Markers {
}

impl Markers {
#[must_use]
pub fn cues(&self) -> Vec<(u8, &Marker)> {
let mut index: u8 = 0;
let mut cues = Vec::new();
Expand All @@ -84,6 +85,7 @@ impl Markers {
cues
}

#[must_use]
pub fn loops(&self) -> Vec<(u8, &Marker)> {
let mut index: u8 = 0;
let mut loops = Vec::new();
Expand All @@ -98,6 +100,7 @@ impl Markers {
loops
}

#[must_use]
pub fn track_color(&self) -> Color {
self.track_color
}
Expand Down
Loading