sdp_rs/lines/
active.rs

1//! Types related to the time active line (`a=`).
2
3use crate::Error;
4use std::convert::TryFrom;
5
6/// The time active line (`a=`) tokenizer. This is low level stuff and you shouldn't interact
7/// directly with it, unless you know what you are doing.
8pub use crate::tokenizers::time::active::Tokenizer;
9
10/// The time active (`a=`) of SDP. `start` and `stop` are saved as the appear in SDP, you will need
11/// `chrono` (or `std`) to convert to an actual time.
12#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Clone, Copy)]
13pub struct Active {
14    pub start: u64,
15    pub stop: u64,
16}
17
18impl<'a> TryFrom<Tokenizer<'a>> for Active {
19    type Error = Error;
20
21    fn try_from(tokenizer: Tokenizer<'a>) -> Result<Self, Self::Error> {
22        Ok(Self {
23            start: tokenizer
24                .start
25                .parse()
26                .map_err(|e| Self::Error::parser_with_error("time start", tokenizer.start, e))?,
27            stop: tokenizer
28                .stop
29                .parse()
30                .map_err(|e| Self::Error::parser_with_error("time stop", tokenizer.stop, e))?,
31        })
32    }
33}
34
35impl std::fmt::Display for Active {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(f, "t={} {}", self.start, self.stop)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn from_tokenizer1() {
47        let tokenizer: Tokenizer = ("3724394400", "3724398000").into();
48
49        assert_eq!(
50            Active::try_from(tokenizer),
51            Ok(Active {
52                start: 3724394400,
53                stop: 3724398000,
54            })
55        );
56    }
57
58    #[test]
59    fn display1() {
60        let active = Active {
61            start: 3724394400,
62            stop: 3724398000,
63        };
64
65        assert_eq!(active.to_string(), "t=3724394400 3724398000");
66    }
67}