aboutsummaryrefslogtreecommitdiff
path: root/matroska/src/write.rs
diff options
context:
space:
mode:
Diffstat (limited to 'matroska/src/write.rs')
-rw-r--r--matroska/src/write.rs17
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();