livekit_data_stream/
utils.rs1use thiserror::Error;
16
17#[derive(Debug, Clone)]
23pub struct SendError;
24
25pub type StreamResult<T> = Result<T, StreamError>;
27
28#[derive(Debug, Error)]
30pub enum StreamError {
31 #[error("stream has already been closed")]
33 AlreadyClosed,
34
35 #[error("stream closed abnormally: {0}")]
36 AbnormalEnd(String),
37
38 #[error("UTF-8 decoding error: {0}")]
39 Utf8(#[from] std::string::FromUtf8Error),
40
41 #[error("incoming header was invalid")]
42 InvalidHeader,
43
44 #[error("expected chunk index to be exactly one more than the previous")]
45 MissedChunk,
46
47 #[error("read length exceeded total length specified in stream header")]
48 LengthExceeded,
49
50 #[error("stream data is incomplete")]
51 Incomplete,
52
53 #[error("unable to send packet")]
54 SendFailed,
55
56 #[error("I/O error: {0}")]
57 Io(#[from] std::io::Error),
58
59 #[error("internal error")]
60 Internal,
61
62 #[error("encryption type mismatch")]
63 EncryptionTypeMismatch,
64
65 #[error("stream header exceeds maximum size")]
66 HeaderTooLarge,
67
68 #[error("stream payload exceeds maximum size")]
69 PayloadTooLarge,
70
71 #[error("decompression failed")]
72 Decompression,
73
74 #[error("file name must be a plain file name without path separators or '..'")]
75 InvalidFileName,
76}
77
78#[derive(Clone, Copy, Default, Debug, Hash, Eq, PartialEq)]
80pub struct StreamProgress {
81 pub(crate) chunk_index: u64,
82 pub(crate) bytes_processed: u64,
84 pub(crate) bytes_total: Option<u64>,
86}
87
88impl StreamProgress {
89 pub fn bytes_processed(&self) -> u64 {
91 self.bytes_processed
92 }
93
94 pub fn bytes_total(&self) -> Option<u64> {
96 self.bytes_total
97 }
98
99 pub fn percentage(&self) -> Option<f32> {
102 self.bytes_total.map(|total| {
103 if total == 0 {
104 1.0
105 } else {
106 self.bytes_processed as f32 / total as f32
107 }
108 })
109 }
110}