From 6b54b5456ad33f333eaefcc92d0bff15d98bd5a7 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Sun, 1 May 2022 16:01:39 +0200 Subject: [PATCH] Encode as hex --- Cargo.toml | 1 + README.md | 6 +++--- src/info.rs | 24 ++++++++++++++++++++---- src/lib.rs | 3 +++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a0a3566..7a3ab7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/README.md b/README.md index 3be9822..017e1d0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/info.rs b/src/info.rs index cd60c51..9357e16 100644 --- a/src/info.rs +++ b/src/info.rs @@ -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 = Crc::::new(&CRC_32_MPEG_2); pub struct SpliceInfoSection @@ -125,7 +122,7 @@ impl SpliceInfoSection where C: SpliceCommand, { - fn into_encoded(self) -> Result, CueError> { + pub fn into_encoded(self) -> Result, 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 { Ok(base64::encode(self.encoded.final_data.as_slice())) } + + pub fn as_hex(&self) -> Result { + 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<()> { diff --git a/src/lib.rs b/src/lib.rs index 3237130..43f75e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(&self, buffer: &mut W) -> Result<(), CueError> where