rtsp_types/headers/
notify_reason.rs

1// Copyright (C) 2020 Sebastian Dröge <sebastian@centricular.com>
2//
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use super::*;
6
7use std::fmt;
8
9/// `Notify-Reason` header ([RFC 7826 section 18.32](https://tools.ietf.org/html/rfc7826#section-18.32)).
10#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub enum NotifyReason {
13    EndOfStream,
14    MediaPropertiesUpdate,
15    ScaleChange,
16    Extension(String),
17}
18
19impl NotifyReason {
20    pub fn as_str(&self) -> &str {
21        match self {
22            NotifyReason::EndOfStream => "end-of-stream",
23            NotifyReason::MediaPropertiesUpdate => "media-properties-update",
24            NotifyReason::ScaleChange => "scale-change",
25            NotifyReason::Extension(ref s) => s.as_str(),
26        }
27    }
28}
29
30impl fmt::Display for NotifyReason {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        f.write_str(self.as_str())
33    }
34}
35
36impl std::str::FromStr for NotifyReason {
37    type Err = HeaderParseError;
38
39    fn from_str(s: &str) -> Result<Self, HeaderParseError> {
40        match s {
41            "end-of-stream" => Ok(NotifyReason::EndOfStream),
42            "media-properties-update" => Ok(NotifyReason::MediaPropertiesUpdate),
43            "scale-change" => Ok(NotifyReason::ScaleChange),
44            _ => Ok(NotifyReason::Extension(String::from(s))),
45        }
46    }
47}
48
49impl super::TypedHeader for NotifyReason {
50    fn from_headers(headers: impl AsRef<Headers>) -> Result<Option<Self>, HeaderParseError> {
51        let headers = headers.as_ref();
52
53        let header = match headers.get(&NOTIFY_REASON) {
54            None => return Ok(None),
55            Some(header) => header,
56        };
57
58        let notify_reason = header.as_str().parse().map_err(|_| HeaderParseError)?;
59
60        Ok(Some(notify_reason))
61    }
62
63    fn insert_into(&self, mut headers: impl AsMut<Headers>) {
64        let headers = headers.as_mut();
65
66        headers.insert(NOTIFY_REASON, self.to_string());
67    }
68}