anni_flac/blocks/
application.rs

1use crate::prelude::*;
2use crate::utils::*;
3use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
4use std::fmt;
5use std::io::{Read, Write};
6
7pub struct BlockApplication {
8    /// Registered application ID.
9    /// (Visit the [registration page](https://xiph.org/flac/id.html) to register an ID with FLAC.)
10    pub application_id: u32,
11    /// Application data (n must be a multiple of 8)
12    pub data: Vec<u8>,
13}
14
15impl Decode for BlockApplication {
16    fn from_reader<R: Read>(reader: &mut R) -> Result<Self> {
17        Ok(BlockApplication {
18            application_id: reader.read_u32::<BigEndian>()?,
19            data: take_to_end(reader)?,
20        })
21    }
22}
23
24#[cfg(feature = "async")]
25#[async_trait::async_trait]
26impl AsyncDecode for BlockApplication {
27    async fn from_async_reader<R>(reader: &mut R) -> Result<Self>
28    where
29        R: AsyncRead + Unpin + Send,
30    {
31        Ok(BlockApplication {
32            application_id: reader.read_u32().await?,
33            data: take_to_end_async(reader).await?,
34        })
35    }
36}
37
38impl Encode for BlockApplication {
39    fn write_to<W: Write>(&self, writer: &mut W) -> Result<()> {
40        writer.write_u32::<BigEndian>(self.application_id)?;
41        writer.write_all(&self.data)?;
42        Ok(())
43    }
44}
45
46impl fmt::Debug for BlockApplication {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        let mut prefix = "".to_owned();
49        if let Some(width) = f.width() {
50            prefix = " ".repeat(width);
51        }
52        writeln!(
53            f,
54            "{prefix}application ID: {:x}",
55            self.application_id,
56            prefix = prefix
57        )?;
58        writeln!(f, "{prefix}data contents:", prefix = prefix)?;
59        // TODO: hexdump
60        writeln!(f, "{prefix}<TODO>", prefix = prefix)
61    }
62}