Use unwrap() for writing to cursor

Writing to cursors should never fail
This commit is contained in:
Simon Bruder 2020-06-25 18:47:11 +02:00
parent 8c8f954bbe
commit 2b0b66ddca
No known key found for this signature in database
GPG key ID: 6F03E0000CC5B62F
2 changed files with 8 additions and 9 deletions

View file

@ -1,6 +1,5 @@
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};
use anyhow::Result;
use byteorder::{LittleEndian, WriteBytesExt}; use byteorder::{LittleEndian, WriteBytesExt};
use log::{debug, trace}; use log::{debug, trace};
@ -88,7 +87,7 @@ impl WaveChunk for RIFFHeader {
} }
} }
pub fn build_wav(format: WaveFormat, data: &[u8]) -> Result<Vec<u8>> { pub fn build_wav(format: WaveFormat, data: &[u8]) -> Vec<u8> {
debug!("Building file"); debug!("Building file");
let riff_header = RIFFHeader { let riff_header = RIFFHeader {
file_size: 82 + data.len() as u32, file_size: 82 + data.len() as u32,
@ -104,15 +103,15 @@ pub fn build_wav(format: WaveFormat, data: &[u8]) -> Result<Vec<u8>> {
let mut buf = Cursor::new(Vec::new()); let mut buf = Cursor::new(Vec::new());
trace!("Building RIFF header"); trace!("Building RIFF header");
buf.write_all(&riff_header.to_chunk())?; buf.write_all(&riff_header.to_chunk()).unwrap();
trace!("Building fmt chunk"); trace!("Building fmt chunk");
buf.write_all(&format.to_chunk())?; buf.write_all(&format.to_chunk()).unwrap();
trace!("Building fact chunk"); trace!("Building fact chunk");
buf.write_all(&fact.to_chunk())?; buf.write_all(&fact.to_chunk()).unwrap();
write!(buf, "data")?; write!(buf, "data").unwrap();
buf.write_u32::<LittleEndian>(data.len() as u32).unwrap(); buf.write_u32::<LittleEndian>(data.len() as u32).unwrap();
buf.write_all(data)?; buf.write_all(data).unwrap();
Ok(buf.into_inner()) buf.into_inner()
} }

View file

@ -244,7 +244,7 @@ pub struct Sound<'a> {
impl Sound<'_> { impl Sound<'_> {
pub fn to_wav(&self) -> Result<Vec<u8>> { pub fn to_wav(&self) -> Result<Vec<u8>> {
match &self.format.tag { match &self.format.tag {
FormatTag::ADPCM => adpcm::build_wav(self.format.clone().try_into()?, self.data), FormatTag::ADPCM => Ok(adpcm::build_wav(self.format.clone().try_into()?, self.data)),
_ => Err(Error::UnsupportedFormat(self.format.tag.clone()).into()), _ => Err(Error::UnsupportedFormat(self.format.tag.clone()).into()),
} }
} }