diff options
author | metamuffin <metamuffin@disroot.org> | 2023-08-05 11:51:26 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-08-05 11:51:26 +0200 |
commit | 7cba183debcf2cefd90b4c1e7a630fb0c2152d06 (patch) | |
tree | 4d662af700078a2b53df08371f31bba0ccd1a216 /matroska/src/write.rs | |
parent | 4a0f08126d80dc589e3c97bf0a07571b8b828a74 (diff) | |
download | jellything-7cba183debcf2cefd90b4c1e7a630fb0c2152d06.tar jellything-7cba183debcf2cefd90b4c1e7a630fb0c2152d06.tar.bz2 jellything-7cba183debcf2cefd90b4c1e7a630fb0c2152d06.tar.zst |
(semi-)proper error handling in matroska
Diffstat (limited to 'matroska/src/write.rs')
-rw-r--r-- | matroska/src/write.rs | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/matroska/src/write.rs b/matroska/src/write.rs index 0b3b167..fb56cb9 100644 --- a/matroska/src/write.rs +++ b/matroska/src/write.rs @@ -3,8 +3,9 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2023 metamuffin <metamuffin.org> */ +use crate::error::Error; +use crate::Result; use crate::{matroska::MatroskaTag, size::EbmlSize, Master}; -use anyhow::{bail, Result}; use log::debug; use std::io::{Seek, Write}; @@ -19,7 +20,7 @@ impl<W: Write> EbmlWriter<W> { } pub fn write(&mut self, data: &[u8]) -> Result<()> { - self.inner.write_all(data)?; + self.inner.write_all(data).map_err(Error::Io)?; self.position += data.len(); Ok(()) } @@ -33,7 +34,7 @@ impl<W: Write> EbmlWriter<W> { let mut size = position - self.position; match size { 0 => return Ok(()), - 1 => bail!("this is sadly not possible"), + 1 => Err(Error::InvalidPadding)?, _ => (), } size -= 1; // subtract tag size @@ -63,7 +64,7 @@ impl<W: Write> EbmlWriter<W> { pub fn write_vint(&mut self, i: u64) -> Result<()> { if i > (1 << 56) - 1 { - bail!("vint does not fit"); + Err(Error::VarintTooLong)? } self.write_vint_len(i, vint_length(i)) } @@ -172,21 +173,21 @@ impl WriteValue for u64 { } } impl WriteValue for f64 { - fn write_to(&self, w: &mut Vec<u8>) -> Result<(), anyhow::Error> { + fn write_to(&self, w: &mut Vec<u8>) -> Result<()> { w.push(0x88); w.extend_from_slice(&self.to_be_bytes()); Ok(()) } } impl WriteValue for Vec<u8> { - fn write_to(&self, w: &mut Vec<u8>) -> Result<(), anyhow::Error> { + fn write_to(&self, w: &mut Vec<u8>) -> Result<()> { write_vint(w, self.len() as u64)?; w.extend_from_slice(self); Ok(()) } } impl WriteValue for String { - fn write_to(&self, w: &mut Vec<u8>) -> Result<(), anyhow::Error> { + fn write_to(&self, w: &mut Vec<u8>) -> Result<()> { let sl = self.as_bytes(); write_vint(w, sl.len() as u64)?; w.extend_from_slice(sl); @@ -226,7 +227,7 @@ impl WriteValue for Master { pub fn write_vint(w: &mut Vec<u8>, i: u64) -> Result<()> { if i > (1 << 56) - 1 { - bail!("vint does not fit"); + Err(Error::VarintTooLong)? } let len = (64 - i.leading_zeros() as usize) / 7 + 1; let mut bytes = i.to_be_bytes(); |