pile-audio refactor
This commit is contained in:
53
crates/pile-audio/src/blocks/padding.rs
Normal file
53
crates/pile-audio/src/blocks/padding.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use std::{fmt::Debug, io::Read};
|
||||
|
||||
use super::{FlacMetablockDecode, FlacMetablockEncode, FlacMetablockHeader, FlacMetablockType};
|
||||
use crate::{FlacDecodeError, FlacEncodeError};
|
||||
|
||||
/// A padding block in a FLAC file.
|
||||
#[derive(Debug)]
|
||||
pub struct FlacPaddingBlock {
|
||||
/// The length of this padding, in bytes.
|
||||
pub size: u32,
|
||||
}
|
||||
|
||||
impl FlacMetablockDecode for FlacPaddingBlock {
|
||||
#[expect(clippy::expect_used)]
|
||||
fn decode(data: &[u8]) -> Result<Self, FlacDecodeError> {
|
||||
if data.iter().any(|x| *x != 0u8) {
|
||||
return Err(FlacDecodeError::MalformedBlock);
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
size: data
|
||||
.len()
|
||||
.try_into()
|
||||
.expect("padding size does not fit into u32"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FlacMetablockEncode for FlacPaddingBlock {
|
||||
fn get_len(&self) -> u32 {
|
||||
self.size
|
||||
}
|
||||
|
||||
fn encode(
|
||||
&self,
|
||||
is_last: bool,
|
||||
with_header: bool,
|
||||
target: &mut impl std::io::Write,
|
||||
) -> Result<(), FlacEncodeError> {
|
||||
if with_header {
|
||||
let header = FlacMetablockHeader {
|
||||
block_type: FlacMetablockType::Padding,
|
||||
length: self.get_len(),
|
||||
is_last,
|
||||
};
|
||||
header.encode(target)?;
|
||||
}
|
||||
|
||||
std::io::copy(&mut std::io::repeat(0u8).take(self.size.into()), target)?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user