Encode as hex

This commit is contained in:
Rafael Caricio 2022-05-01 16:01:39 +02:00
parent f13606d851
commit 6b54b5456a
Signed by: rafaelcaricio
GPG key ID: 3C86DBCE8E93C947
4 changed files with 27 additions and 7 deletions

View file

@ -12,6 +12,7 @@ bitstream-io = "1.3"
crc = "3.0"
thiserror = "1"
serde = { version = "1", features = ["derive"], optional = true }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
[dev-dependencies]
serde_json = "1"

View file

@ -15,9 +15,9 @@ other purposes. More information can be found at
Implemented parts of the standard are:
- [ ] Splice Info section
- Splice Command section:
- [ ] Splice Null
- [x] Splice Info section
- Splice Commands:
- [x] Splice Null
- [ ] Splice Insert
- [ ] Splice Schedule
- [ ] Time Signal

View file

@ -5,9 +5,6 @@ use bitstream_io::{BigEndian, BitWrite, BitWriter};
use crc::{Crc, CRC_32_MPEG_2};
use std::fmt::{Display, Formatter};
#[cfg(feature = "serde")]
use serde::Serialize;
pub const MPEG_2: Crc<u32> = Crc::<u32>::new(&CRC_32_MPEG_2);
pub struct SpliceInfoSection<C, S>
@ -125,7 +122,7 @@ impl<C> SpliceInfoSection<C, NotEncoded>
where
C: SpliceCommand,
{
fn into_encoded(self) -> Result<SpliceInfoSection<C, EncodedData>, CueError> {
pub fn into_encoded(self) -> Result<SpliceInfoSection<C, EncodedData>, CueError> {
// Write splice command to a temporary buffer
let mut splice_data = Vec::new();
self.state.splice_command.write_to(&mut splice_data)?;
@ -205,6 +202,13 @@ where
pub fn as_base64(&self) -> Result<String, CueError> {
Ok(base64::encode(self.encoded.final_data.as_slice()))
}
pub fn as_hex(&self) -> Result<String, CueError> {
Ok(format!(
"0x{}",
hex::encode(self.encoded.final_data.as_slice())
))
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
@ -339,6 +343,18 @@ mod tests {
Ok(())
}
#[test]
fn write_splice_null_as_hex() -> Result<()> {
let splice = SpliceInfoSection::new(SpliceNull::new());
assert_eq!(
splice.into_encoded()?.as_hex()?,
"0xfc301100000000000000fff0000000007a4fbfff".to_string()
);
Ok(())
}
#[cfg(feature = "serde")]
#[test]
fn serialize_as_json() -> Result<()> {

View file

@ -5,6 +5,9 @@ mod commands;
mod descriptors;
mod info;
pub use commands::SpliceNull;
pub use info::{EncryptionAlgorithm, SAPType, SpliceInfoSection};
pub trait TransportPacketWrite {
fn write_to<W>(&self, buffer: &mut W) -> Result<(), CueError>
where