Skip to content

Commit

Permalink
update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
webern committed Oct 5, 2023
1 parent f774073 commit 15c791d
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 194 deletions.
460 changes: 313 additions & 147 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ categories = ["encoding"]

[dependencies]
log = "0.4"
snafu = "0.6"
snafu = "0.7"

[dev-dependencies]
chrono = "0.4"
env_logger = "0.8"
env_logger = "0.10"
tempfile = "3"
32 changes: 16 additions & 16 deletions src/byte_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const MB: usize = KB * 1024;
impl ByteIter<BufReader<File>> {
pub(crate) fn new_file<P: AsRef<Path>>(path: P) -> ByteResult<Self> {
let path = path.as_ref();
let f = File::open(path).context(FileOpen { path })?;
let f = File::open(path).context(FileOpenSnafu { path })?;
let buf = BufReader::with_capacity(MB, f);
Self::new(buf.bytes())
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<R: Read> ByteIter<R> {
Some(result) => match result {
Ok(val) => Ok(Some(val)),
Err(ref e) if e.kind() == ErrorKind::UnexpectedEof => Ok(None),
Err(e) => Err(e).context(Io { position }),
Err(e) => Err(e).context(IoSnafu { position }),
},
}
}
Expand Down Expand Up @@ -178,40 +178,40 @@ impl<R: Read> ByteIter<R> {
e
}
};
Err(e).context(Io {
Err(e).context(IoSnafu {
position: self.position.unwrap_or(0),
})
}

pub(crate) fn read_or_die(&mut self) -> ByteResult<u8> {
self.read()?.context(End {
self.read()?.context(EndSnafu {
position: self.position.unwrap_or(0),
})
}

pub(crate) fn read2(&mut self) -> ByteResult<[u8; 2]> {
let mut retval = [0u8; 2];
retval[0] = self.read()?.context(End {
retval[0] = self.read()?.context(EndSnafu {
position: self.position.unwrap_or(0),
})?;
retval[1] = self.read()?.context(End {
retval[1] = self.read()?.context(EndSnafu {
position: self.position.unwrap_or(0),
})?;
Ok(retval)
}

pub(crate) fn read4(&mut self) -> ByteResult<[u8; 4]> {
let mut retval = [0u8; 4];
retval[0] = self.read()?.context(End {
retval[0] = self.read()?.context(EndSnafu {
position: self.position.unwrap_or(0),
})?;
retval[1] = self.read()?.context(End {
retval[1] = self.read()?.context(EndSnafu {
position: self.position.unwrap_or(0),
})?;
retval[2] = self.read()?.context(End {
retval[2] = self.read()?.context(EndSnafu {
position: self.position.unwrap_or(0),
})?;
retval[3] = self.read()?.context(End {
retval[3] = self.read()?.context(EndSnafu {
position: self.position.unwrap_or(0),
})?;
Ok(retval)
Expand All @@ -235,7 +235,7 @@ impl<R: Read> ByteIter<R> {
while current_byte & CONTINUE == CONTINUE {
ensure!(
byte_count <= 4,
VlqTooBig {
VlqTooBigSnafu {
position: self.position.unwrap_or(0)
}
);
Expand All @@ -248,7 +248,7 @@ impl<R: Read> ByteIter<R> {

pub(crate) fn read_vlq_u32(&mut self) -> ByteResult<u32> {
let bytes = self.read_vlq_bytes()?;
let decoded = decode_slice(&bytes).context(VlqDecode {
let decoded = decode_slice(&bytes).context(VlqDecodeSnafu {
position: self.position.unwrap_or(0),
})?;
trace!("decoded vlq value {} from {} bytes", decoded, bytes.len());
Expand All @@ -260,7 +260,7 @@ impl<R: Read> ByteIter<R> {
}

pub(crate) fn peek_or_die(&self) -> ByteResult<u8> {
self.peek1.context(End {
self.peek1.context(EndSnafu {
position: self.position.unwrap_or(0),
})
}
Expand All @@ -278,12 +278,12 @@ impl<R: Read> ByteIter<R> {

pub(crate) fn expect_tag(&mut self, expected_tag: &str) -> ByteResult<()> {
let tag_bytes = self.read4()?;
let actual_tag = from_utf8(&tag_bytes).context(Str {
let actual_tag = from_utf8(&tag_bytes).context(StrSnafu {
position: self.position.unwrap_or(0),
})?;
ensure!(
expected_tag == actual_tag,
Tag {
TagSnafu {
expected: expected_tag,
found: actual_tag,
position: self.position.unwrap_or(0)
Expand All @@ -306,7 +306,7 @@ impl<R: Read> ByteIter<R> {
let found = self.read_or_die()?;
ensure!(
expected == found,
ReadExpect {
ReadExpectSnafu {
expected,
found,
position: self.position.unwrap_or(0)
Expand Down
2 changes: 1 addition & 1 deletion src/core/duration_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl DurationName {
v if DurationName::D256 as u8 == v => Ok(DurationName::D256),
v if DurationName::D512 as u8 == v => Ok(DurationName::D512),
v if DurationName::D1024 as u8 == v => Ok(DurationName::D1024),
_ => crate::error::Other { site: site!() }.fail(),
_ => crate::error::OtherSnafu { site: site!() }.fail(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl Message {
iter.set_running_status_detected();
let running_status = iter
.latest_message_byte()
.context(error::RunningStatus { site: site!() })?;
.context(error::RunningStatusSnafu { site: site!() })?;
trace!("running status byte {:#x}", running_status);
running_status
} else {
Expand Down Expand Up @@ -781,7 +781,7 @@ impl Control {
x if x == Control::Undefined117 as u8 => Ok(Control::Undefined117),
x if x == Control::Undefined118 as u8 => Ok(Control::Undefined118),
x if x == Control::Undefined119 as u8 => Ok(Control::Undefined119),
_ => error::Other { site: site!() }.fail(),
_ => error::OtherSnafu { site: site!() }.fail(),
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) type LibResult<T> = std::result::Result<T, LibError>;

/// The internal Error type for this library.
#[derive(Debug, Snafu)]
#[snafu(visibility = "pub(crate)")]
#[snafu(visibility(pub(crate)))]
pub(crate) enum LibError {
#[snafu(display("{} Error creating file '{}': {}", site, path.display(), source))]
Create {
Expand Down Expand Up @@ -82,31 +82,31 @@ macro_rules! site {

macro_rules! io {
() => {
crate::error::Read { site: site!() }
crate::error::ReadSnafu { site: site!() }
};
}

macro_rules! wr {
() => {
crate::error::Write { site: site!() }
crate::error::WriteSnafu { site: site!() }
};
}

macro_rules! invalid_file_s {
() => {
crate::error::InvalidFile {
crate::error::InvalidFileSnafu {
site: site!(),
description: "[no description]",
}
};
($msg:expr) => {
crate::error::InvalidFile {
crate::error::InvalidFileSnafu {
site: site!(),
description: $msg,
}
};
($fmt:expr, $($arg:expr),+) => {
crate::error::InvalidFile {
crate::error::InvalidFileSnafu {
site: site!(),
description: format!($fmt, $($arg),+),
}
Expand Down Expand Up @@ -151,7 +151,7 @@ macro_rules! invalid_file {

macro_rules! noimpl {
($name:expr) => {
return crate::error::Unimplemented {
return crate::error::UnimplementedSnafu {
site: site!(),
feature: $name.to_string(),
}
Expand Down
4 changes: 2 additions & 2 deletions src/file/division.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Division {
pub(crate) fn from_u16(value: u16) -> LibResult<Self> {
if value & DIVISION_TYPE_BIT == DIVISION_TYPE_BIT {
// TODO - implement SMPTE division
crate::error::Other { site: site!() }.fail()
crate::error::OtherSnafu { site: site!() }.fail()
} else {
Ok(Division::QuarterNote(QuarterNoteDivision::new(value)))
}
Expand All @@ -50,7 +50,7 @@ impl Division {
pub(crate) fn write<W: Write>(&self, w: &mut Scribe<W>) -> LibResult<()> {
match self {
Division::QuarterNote(q) => Ok(w.write_all(&q.get().to_be_bytes()).context(wr!())?),
Division::Smpte(_) => crate::error::Other { site: site!() }.fail(),
Division::Smpte(_) => crate::error::OtherSnafu { site: site!() }.fail(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/file/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Format {
0 => Ok(Format::Single),
1 => Ok(Format::Multi),
2 => Ok(Format::Sequential),
_ => crate::error::Other { site: site!() }.fail(),
_ => crate::error::OtherSnafu { site: site!() }.fail(),
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/file/meta_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ impl MetaEvent {

pub(crate) fn parse_text<R: Read>(iter: &mut ByteIter<R>) -> LibResult<Self> {
// we should be on a type-byte with a value between 0x01 and 0x09 (the text range).
let text_type = iter.current().context(error::Other { site: site!() })?;
let text_type = iter
.current()
.context(error::OtherSnafu { site: site!() })?;
let length = iter.read_vlq_u32().context(io!())?;
let bytes = iter.read_n(length as usize).context(io!())?;
// the spec does not strictly specify what encoding should be used for strings
Expand All @@ -263,7 +265,8 @@ impl MetaEvent {
fn write_text<W: Write>(w: &mut Scribe<W>, text_type: u8, text: &Text) -> LibResult<()> {
w.write_all(&text_type.to_be_bytes()).context(wr!())?;
let bytes = text.as_bytes();
let size_u32 = u32::try_from(bytes.len()).context(error::StringTooLong { site: site!() })?;
let size_u32 =
u32::try_from(bytes.len()).context(error::StringTooLongSnafu { site: site!() })?;
let size = Vlq::new(size_u32).to_bytes();
w.write_all(&size).context(wr!())?;
w.write_all(bytes).context(wr!())?;
Expand Down Expand Up @@ -365,7 +368,7 @@ pub struct TimeSignatureValue {
impl TimeSignatureValue {
/// Create a new `TimeSignatureValue` object.
pub fn new(numerator: u8, denominator: DurationName, click: Clocks) -> Result<Self> {
ensure!(numerator > 0, error::Other { site: site!() });
ensure!(numerator > 0, error::OtherSnafu { site: site!() });
Ok(Self {
numerator,
denominator,
Expand Down
2 changes: 1 addition & 1 deletion src/file/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl Track {

// write the length of the track
let track_length = u32::try_from(track_data.len())
.context(crate::error::TrackTooLong { site: site!() })?;
.context(crate::error::TrackTooLongSnafu { site: site!() })?;
w.write_all(&track_length.to_be_bytes()).context(wr!())?;

// write the track data
Expand Down
36 changes: 24 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ impl MidiFile {

/// Write a `MidiFile` to bytes.
pub fn write<W: Write>(&self, w: &mut W) -> Result<()> {
let ntracks =
u16::try_from(self.tracks.len()).context(error::TooManyTracks { site: site!() })?;
let ntracks = u16::try_from(self.tracks.len())
.context(error::TooManyTracksSnafu { site: site!() })?;
let mut scribe = Scribe::new(
w,
ScribeSettings {
Expand All @@ -169,7 +169,7 @@ impl MidiFile {
/// Save a `MidiFile` to a file path.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let path = path.as_ref();
let file = File::create(path).context(error::Create {
let file = File::create(path).context(error::CreateSnafu {
site: site!(),
path,
})?;
Expand Down Expand Up @@ -202,30 +202,42 @@ impl MidiFile {
}

pub fn push_track(&mut self, track: Track) -> Result<()> {
ensure!(self.tracks_len() < u32::MAX, error::Other { site: site!() });
ensure!(
self.tracks_len() < u32::MAX,
error::OtherSnafu { site: site!() }
);
if *self.header().format() == Format::Single {
ensure!(self.tracks_len() <= 1, error::Other { site: site!() });
ensure!(self.tracks_len() <= 1, error::OtherSnafu { site: site!() });
}
self.tracks.push(ensure_end_of_track(track)?);
Ok(())
}

pub fn insert_track(&mut self, index: u32, track: Track) -> Result<()> {
ensure!(self.tracks_len() < u32::MAX, error::Other { site: site!() });
ensure!(
self.tracks_len() < u32::MAX,
error::OtherSnafu { site: site!() }
);
if *self.header().format() == Format::Single {
ensure!(self.tracks_len() <= 1, error::Other { site: site!() });
ensure!(self.tracks_len() <= 1, error::OtherSnafu { site: site!() });
}
ensure!(index < self.tracks_len(), error::Other { site: site!() });
ensure!(
index < self.tracks_len(),
error::OtherSnafu { site: site!() }
);
self.tracks.insert(
usize::try_from(index).context(error::TooManyTracks { site: site!() })?,
usize::try_from(index).context(error::TooManyTracksSnafu { site: site!() })?,
ensure_end_of_track(track)?,
);
Ok(())
}

pub fn remove_track(&mut self, index: u32) -> Result<Track> {
ensure!(index < self.tracks_len(), error::Other { site: site!() });
let i = usize::try_from(index).context(error::TooManyTracks { site: site!() })?;
ensure!(
index < self.tracks_len(),
error::OtherSnafu { site: site!() }
);
let i = usize::try_from(index).context(error::TooManyTracksSnafu { site: site!() })?;
Ok(self.tracks.remove(i))
}

Expand All @@ -235,7 +247,7 @@ impl MidiFile {
let chunk_length = iter.read_u32().context(io!())?;
// header chunk length is always 6
if chunk_length != 6 {
return error::Other { site: site!() }.fail();
return error::OtherSnafu { site: site!() }.fail();
}
let format_word = iter.read_u16().context(io!())?;
let num_tracks = iter.read_u16().context(io!())?;
Expand Down

0 comments on commit 15c791d

Please sign in to comment.