Skip to main content

ts_analyzer/
lib.rs

1#![forbid(unsafe_code)]
2// Use these checks when closer to complete. They're a bit too strict for early
3// development. #![deny(future_incompatible, missing_docs, rust_2018_idioms,
4// unused, warnings)]
5#![deny(future_incompatible, rust_2018_idioms)]
6
7//! This crate is used to read the payload data from a given transport stream.
8
9use std::error::Error;
10use std::fmt::Display;
11
12// TODO: Add this back when you figure out a way to not have to set an
13// environment variable. Pulling a test file down is an idea. Not a great idea,
14// but an idea nonetheless.
15//
16// Include the README in the doc-tests.
17// #[doc = include_str!("../README.md")]
18pub mod reader;
19
20pub mod packet;
21
22mod helpers {
23    pub mod tracked_payload;
24}
25
26#[derive(Debug, thiserror::Error)]
27pub enum ErrorKind {
28    /// Error that is thrown when trying to parse a byte array as a transport
29    /// stream packet, but it doesn't start with a `SYNC_BYTE`.
30    #[error("Invalid first byte for transport stream packet `{byte}`")]
31    InvalidFirstByte {
32        /// Invalid first byte.
33        byte: u8,
34    },
35
36    /// Error that is thrown when the payload pointer is larger than possible
37    /// could possibly fit in the remainder of the packet.
38    #[error(
39        "Invalid payload pointer `{pointer}` for payload with `{remainder}` bytes remaining"
40    )]
41    InvalidPayloadPointer { pointer: u8, remainder: u8 },
42
43    /// Error that is thrown when trying read a payload from a packet without a
44    /// payload.
45    #[error("Cannot read payload from packet that has none")]
46    NoPayload,
47
48    /// Error that is thrown when trying to read a transport stream file and no
49    /// SYNC byte can be found.
50    #[error("Stream contains no SYNC byte")]
51    NoSyncByteFound,
52
53    /// Error that is thrown when trying to read the start of a new payload and
54    /// no new payload is present in a packet.
55    #[error("Continuation payload cannot be used as a starting payload")]
56    PayloadIsNotStart,
57
58    #[error(transparent)]
59    Unknown(#[from] std::io::Error),
60}
61
62#[derive(Clone, Copy, Debug, PartialEq)]
63pub enum TransportScramblingControl {
64    NoScrambling = 0,
65    Reserved     = 1,
66    EvenKey      = 2,
67    OddKey       = 3,
68}
69#[derive(Clone, Copy, Debug, PartialEq)]
70pub enum AdaptationFieldControl {
71    Reserved             = 0,
72    Payload              = 1,
73    AdaptationField      = 2,
74    AdaptationAndPayload = 3,
75}
76
77#[derive(Clone, Copy, Debug, PartialEq)]
78pub enum Errors {
79    InvalidFirstByte(u8),
80}
81
82impl Error for Errors {}
83
84impl Display for Errors {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        match self {
87            Errors::InvalidFirstByte(invalid_byte) => {
88                write!(f, "invalid first byte for packet: [{}]", invalid_byte)
89            }
90        }
91    }
92}