rtci_sdp/attribute/
sctp.rs1use std::fmt;
5
6use shared::error::Result;
7
8use super::TypedAttribute;
9use crate::grammar::{self, syntax};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct SctpPort(pub u16);
15
16impl TypedAttribute for SctpPort {
17 const NAME: &'static str = "sctp-port";
18
19 fn parse(value: &str) -> Result<Self> {
20 grammar::digits_no_leading_zero("a=sctp-port", value).map(SctpPort)
21 }
22}
23
24impl fmt::Display for SctpPort {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 write!(f, "{}", self.0)
27 }
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34pub struct MaxMessageSize(pub u64);
35
36impl TypedAttribute for MaxMessageSize {
37 const NAME: &'static str = "max-message-size";
38
39 fn parse(value: &str) -> Result<Self> {
40 grammar::digits_no_leading_zero("a=max-message-size", value).map(MaxMessageSize)
41 }
42}
43
44impl fmt::Display for MaxMessageSize {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 write!(f, "{}", self.0)
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq, Hash)]
54pub struct SctpMap {
55 pub port: u16,
57 pub app: String,
59 pub streams: Option<u32>,
61}
62
63impl TypedAttribute for SctpMap {
64 const NAME: &'static str = "sctpmap";
65
66 fn parse(value: &str) -> Result<Self> {
67 let fields = grammar::fields("a=sctpmap", value)?;
68 let (port, app, streams) = match fields[..] {
69 [port, app] => (port, app, None),
70 [port, app, streams] => (port, app, Some(streams)),
71 _ => return Err(syntax("a=sctpmap", value)),
72 };
73 if !grammar::is_token(app) {
74 return Err(syntax("a=sctpmap", value));
75 }
76 Ok(SctpMap {
77 port: grammar::digits("a=sctpmap port", port)?,
78 app: app.to_owned(),
79 streams: streams
80 .map(|s| grammar::digits("a=sctpmap streams", s))
81 .transpose()?,
82 })
83 }
84}
85
86impl fmt::Display for SctpMap {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 write!(f, "{} {}", self.port, self.app)?;
89 if let Some(streams) = self.streams {
90 write!(f, " {streams}")?;
91 }
92 Ok(())
93 }
94}