srt_protocol/options/
stream_id.rs1use std::{
2 convert::TryFrom,
3 error::Error,
4 fmt::{Debug, Display, Formatter},
5 io::{self, ErrorKind},
6 ops::Deref,
7 str::FromStr,
8 string::FromUtf8Error,
9};
10
11#[derive(Debug, Clone, Eq, PartialEq)]
17pub struct StreamId(String);
18
19impl Deref for StreamId {
20 type Target = String;
21
22 fn deref(&self) -> &Self::Target {
23 &self.0
24 }
25}
26
27#[derive(Debug, Clone, Eq, PartialEq)]
28pub enum StreamIdError {
29 FromUtf8(FromUtf8Error),
30 Length(usize),
31}
32
33impl Display for StreamIdError {
34 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35 use StreamIdError::*;
36 match self {
37 FromUtf8(error) => Display::fmt(error, f),
38 Length(len) => write!(
39 f,
40 "StreamId value length of {len} exceeded the limit of 512 bytes"
41 ),
42 }
43 }
44}
45
46impl Error for StreamIdError {}
47
48impl From<StreamIdError> for io::Error {
49 fn from(error: StreamIdError) -> Self {
50 io::Error::new(ErrorKind::InvalidInput, error)
51 }
52}
53
54impl TryFrom<Vec<u8>> for StreamId {
55 type Error = StreamIdError;
56
57 fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
58 if value.len() > 512 {
59 return Err(StreamIdError::Length(value.len()));
60 }
61 let stream_id = String::from_utf8(value).map_err(StreamIdError::FromUtf8)?;
62 Ok(StreamId(stream_id))
63 }
64}
65
66impl TryFrom<String> for StreamId {
67 type Error = StreamIdError;
68
69 fn try_from(value: String) -> Result<Self, Self::Error> {
70 if value.len() > 512 {
71 return Err(StreamIdError::Length(value.len()));
72 }
73 Ok(StreamId(value))
74 }
75}
76
77impl FromStr for StreamId {
78 type Err = StreamIdError;
79
80 fn from_str(s: &str) -> Result<Self, Self::Err> {
81 Self::try_from(s.to_string())
82 }
83}
84
85impl Display for StreamId {
86 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
87 write!(f, "{}", self.0)
88 }
89}