gosuto_libwebrtc/
session_description.rs1use std::{
16 fmt::{Debug, Display},
17 str::FromStr,
18};
19
20use thiserror::Error;
21
22use crate::imp::session_description as sd_imp;
23
24#[derive(Debug, Copy, Clone, PartialEq, Eq)]
25pub enum SdpType {
26 Offer,
27 PrAnswer,
28 Answer,
29 Rollback,
30}
31
32impl FromStr for SdpType {
33 type Err = &'static str;
34
35 fn from_str(sdp_type: &str) -> Result<Self, Self::Err> {
36 match sdp_type {
37 "offer" => Ok(Self::Offer),
38 "pranswer" => Ok(Self::PrAnswer),
39 "answer" => Ok(Self::Answer),
40 "rollback" => Ok(Self::Rollback),
41 _ => Err("invalid SdpType"),
42 }
43 }
44}
45
46impl Display for SdpType {
47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 let s = match self {
49 SdpType::Offer => "offer",
50 SdpType::PrAnswer => "pranswer",
51 SdpType::Answer => "answer",
52 SdpType::Rollback => "rollback",
53 };
54 write!(f, "{}", s)
55 }
56}
57
58#[derive(Clone)]
59pub struct SessionDescription {
60 pub(crate) handle: sd_imp::SessionDescription,
61}
62
63#[derive(Clone, Error, Debug)]
64#[error("Failed to parse sdp: {line} - {description}")]
65pub struct SdpParseError {
66 pub line: String,
67 pub description: String,
68}
69
70impl SessionDescription {
71 pub fn parse(sdp: &str, sdp_type: SdpType) -> Result<Self, SdpParseError> {
72 sd_imp::SessionDescription::parse(sdp, sdp_type)
73 }
74
75 pub fn sdp_type(&self) -> SdpType {
76 self.handle.sdp_type()
77 }
78}
79
80impl ToString for SessionDescription {
81 fn to_string(&self) -> String {
82 self.handle.to_string()
83 }
84}
85
86impl Debug for SessionDescription {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88 f.debug_struct("SessionDescription").field("sdp_type", &self.sdp_type()).finish()
89 }
90}