Skip to content

Commit

Permalink
ready for crates.io
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstoik1 committed Dec 26, 2022
2 parents 7442831 + ad8ae65 commit d7caa06
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Add `cdtoc` to your `dependencies` in `Cargo.toml`, like:
cdtoc = "0.1.*"
```

The disc ID helpers require additional dependencies, so if you aren't using them, be sure to disable the default features (and add back any you _do_ want) to skip the overhead.
The disc ID helpers require additional dependencies, so if you aren't using them, be sure to disable the default features (adding back any you _do_ want) to skip the overhead.

```toml
[dependencies.cdtoc]
Expand Down
3 changes: 2 additions & 1 deletion src/accuraterip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ impl fmt::Display for AccurateRip {
#[cfg(feature = "faster-hex")]
#[allow(unsafe_code)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buf = [b'-', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'];
let mut buf: [u8; 9] = [b'-', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'];

// Length.
write!(f, "{:03}", self.0[0])?;

// ID Parts.
// Safety: all bytes are ASCII.
faster_hex::hex_encode(&[self.0[4], self.0[3], self.0[2], self.0[1]], &mut buf[1..]).unwrap();
f.write_str(unsafe { std::str::from_utf8_unchecked(&buf) })?;

Expand Down
1 change: 1 addition & 0 deletions src/cddb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl fmt::Display for Cddb {
#[allow(unsafe_code)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buf = [b'0'; 8];
// Safety: all bytes are ASCII.
crate::hex_encode_u32(self.0, &mut buf, false);
f.write_str(unsafe { std::str::from_utf8_unchecked(&buf) })
}
Expand Down
29 changes: 28 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,58 @@ use std::{
#[derive(Debug, Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
/// # Error Type.
pub enum TocError {
/// # CDDASample Rate.
/// # CDDA Sample Rate.
///
/// The total number of samples for a given audio track on a CD must be
/// evenly divisible by `588`, the number of samples per sector.
CDDASampleCount,

/// # Invalid characters.
///
/// CDTOC metadata tags comprise HEX-encoded decimals separated by `+`
/// signs. The only other character allowed is an `X`, used to indicate a
/// leading data session.
CDTOCChars,

/// # Invalid Checksum File.
///
/// This is a catch-all error used when an AccurateRip or CTDB checksum
/// manifest contains some sort of logical error (i.e. preventing it being
/// parsed).
Checksums,

/// # No Audio.
///
/// At least one audio track is required for a table of contents.
NoAudio,

/// # No Checksums.
///
/// This error is used when an AccurateRip or CTDB checksum manifest yields
/// no valid checksums.
NoChecksums,

/// # Invalid sector count.
///
/// The stated number of audio tracks should match the number of sectors
/// provided (once data and leadout values have been separated).
SectorCount(u8, usize),

/// # Sector Ordering.
///
/// Audio CD sectors must be sequentially ordered and non-overlapping, and
/// the data session, if any, must come either immediately before or after
/// the audio set. The leadout must be larger than every other sector.
SectorOrder,

/// # Sector Size.
///
/// Sector values cannot exceed [`u32::MAX`].
SectorSize,

/// # Track Count.
///
/// Audio CDs support a maximum of 99 tracks.
TrackCount,
}

Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Add `cdtoc` to your `dependencies` in `Cargo.toml`, like:
cdtoc = "0.1.*"
```
The disc ID helpers require additional dependencies, so if you aren't using them, be sure to disable the default features (and add back any you _do_ want) to skip the overhead.
The disc ID helpers require additional dependencies, so if you aren't using them, be sure to disable the default features (adding back any you _do_ want) to skip the overhead.
```ignore,toml
[dependencies.cdtoc]
Expand Down Expand Up @@ -204,7 +204,6 @@ pub struct Toc {
}

impl fmt::Display for Toc {
#[allow(unsafe_code)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Start with the track count.
write!(f, "{:X}", self.audio.len())?;
Expand Down Expand Up @@ -274,10 +273,12 @@ impl Toc {
/// or the sectors are in the wrong order.
pub fn from_parts(audio: Vec<u32>, data: Option<u32>, leadout: u32)
-> Result<Self, TocError> {
if audio.is_empty() { return Err(TocError::NoAudio); }
// Check length.
let audio_len = audio.len();
if 0 == audio_len { return Err(TocError::NoAudio); }
if 99 < audio_len { return Err(TocError::TrackCount); }

// Audio is out of order?
let audio_len = audio.len();
if
(1 < audio_len && audio.windows(2).any(|pair| pair[1] <= pair[0])) ||
leadout <= audio[audio_len - 1]
Expand Down Expand Up @@ -638,7 +639,6 @@ fn base64_encode(src: &[u8]) -> String {
}

#[cfg(feature = "faster-hex")]
#[allow(unsafe_code)]
/// # HEX Encode u32.
///
/// This convenience wrapper uses faster-hex to encode a u32 to a buffer.
Expand Down
4 changes: 2 additions & 2 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ impl Duration {
/// Return the duration as a tuple containing the total number of seconds
/// and remaining frames (some fraction of a second).
///
/// Audio CDs have 75 frames per second, so the second value will always be
/// in the range of `0..75`.
/// Audio CDs have 75 frames per second, so the frame portion will always
/// be in the range of `0..75`.
///
/// ## Examples
///
Expand Down
5 changes: 4 additions & 1 deletion src/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Track {


#[derive(Debug)]
/// # Tracks.
/// # Audio Tracks Iterator.
///
/// This is an iterator of [`Track`] details for a given [`Toc`](crate::Toc).
///
Expand Down Expand Up @@ -212,6 +212,9 @@ impl<'a> Tracks<'a> {
/// Variants of this type are returned by [`Track::position`].
pub enum TrackPosition {
/// # Invalid.
///
/// This is used for track numbers that are out of range, including pre-gap
/// hidden tracks (#0).
Invalid,

/// # The First Track.
Expand Down

0 comments on commit d7caa06

Please sign in to comment.