smptera_format_identifiers_rust/
lib.rs

1//! [Format Identifier](https://smpte-ra.org/registered-mpeg-ts-ids) value on record with the
2//! [SMPTE Registration Authority](https://smpte-ra.org/) for use in _MPEG Transport Stream_ data.
3//!
4//! (This create is automatically generated from a
5//! [mirror of the SMPTE-RA data](https://github.com/dholroyd/smptera-format-identifiers)
6//! by code in the
7//! [smptera-format-identifiers-rust](https://github.com/dholroyd/smptera-format-identifiers-rust)
8//! project.)
9//!
10//! The simple wrapper type [`FormatIdentifier`](struct.FormatIdentifier.html) defines a constant
11//! for each _format identifier_ in the SMPTE-RA data,
12//!
13//! ```rust
14//! # use smptera_format_identifiers_rust::FormatIdentifier;
15//! use std::io::stdout;
16//! println!("{:?}", FormatIdentifier::AC_3);
17//! // prints: FormatIdentifier(FourCC{AC-3})
18//! ```
19//!
20//! ## Usage
21//!
22//! Create from a slice,
23//!
24//! ```rust
25//! # use smptera_format_identifiers_rust::FormatIdentifier;
26//! let descriptor_data = b"\x05\x04CUEI";
27//! let id = FormatIdentifier::from(&descriptor_data[2..6]);
28//! assert_eq!(id, FormatIdentifier::CUEI);
29//! ```
30//!
31//! Wrap an existing FourCC value,
32//!
33//! ```rust
34//! # use smptera_format_identifiers_rust::FormatIdentifier;
35//! # use four_cc::FourCC;
36//! let fcc = FourCC(*b"CUEI");
37//! let id = FormatIdentifier(fcc);
38//! assert_eq!(id, FormatIdentifier::CUEI);
39//! ```
40//!
41//! Use provided constants in matches,
42//!
43//! ```rust
44//! # use smptera_format_identifiers_rust::FormatIdentifier;
45//! # let descriptor_data = b"\x05\x04CUEI";
46//! match FormatIdentifier::from(&descriptor_data[2..6]) {
47//!     FormatIdentifier::CUEI => println!("SCTE-35 suspected"),
48//!     FormatIdentifier::KLVA => println!("SMPTE RP 217-2001 KLV Packets?"),
49//!     other_id => println!("Some other kinda stuff: {:?}", other_id),
50//! }
51//! ```
52//!
53//! Write bytes values,
54//!
55//! ```rust
56//! # use smptera_format_identifiers_rust::FormatIdentifier;
57//! # use std::io::{Cursor, Write};
58//! let mut data = vec![];
59//! let mut io = Cursor::new(data);
60//!
61//! let id = &FormatIdentifier::ID3;
62//! io.write(id.into())
63//!     .expect("write failed");
64//!
65//! assert_eq!(io.into_inner(), [b'I', b'D', b'3', b' ']);
66//! ```
67
68#![forbid(unsafe_code)]
69#![deny(missing_docs)]
70
71use four_cc::FourCC;
72
73/// Identifier for data formats used in MPEG Transport Streams
74#[derive(Debug, Copy, Clone, PartialEq, Eq)]
75pub struct FormatIdentifier(pub FourCC);
76
77impl<'a> From<&'a [u8]> for FormatIdentifier {
78    fn from(buf: &[u8]) -> Self {
79        FormatIdentifier(FourCC::from(buf))
80    }
81}
82
83impl<'a> From<&'a FormatIdentifier> for &'a [u8] {
84    fn from(id: &'a FormatIdentifier) -> Self {
85        &(id.0).0
86    }
87}
88
89include!("generated.rs");
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn smoke() {
97        assert_eq!(FormatIdentifier::CUEI, FormatIdentifier::from(&b"CUEI"[..]));
98        assert_eq!(&b"CUEI"[..], <&[u8]>::from(&FormatIdentifier::CUEI));
99    }
100}