1 use super::description::session::SessionDescription;
2 use super::error::{Error, Result};
3
4 use std::io;
5 use std::io::SeekFrom;
6
7 pub(crate) const END_LINE: &str = "\r\n";
8
9 pub struct Lexer<'a, R: io::BufRead + io::Seek> {
10 pub desc: SessionDescription,
11 pub reader: &'a mut R,
12 }
13
14 pub type StateFnType<'a, R> = fn(&mut Lexer<'a, R>) -> Result<Option<StateFn<'a, R>>>;
15
16 pub struct StateFn<'a, R: io::BufRead + io::Seek> {
17 pub f: StateFnType<'a, R>,
18 }
19
read_type<R: io::BufRead + io::Seek>(reader: &mut R) -> Result<(Vec<u8>, usize)>20 pub fn read_type<R: io::BufRead + io::Seek>(reader: &mut R) -> Result<(Vec<u8>, usize)> {
21 let mut b = [0; 1];
22
23 loop {
24 if reader.read_exact(&mut b).is_err() {
25 return Ok((b"".to_vec(), 0));
26 }
27
28 if b[0] == b'\n' || b[0] == b'\r' {
29 continue;
30 }
31 reader.seek(SeekFrom::Current(-1))?;
32
33 let mut buf = Vec::with_capacity(2);
34 let num_bytes = reader.read_until(b'=', &mut buf)?;
35 if num_bytes == 0 {
36 return Ok((b"".to_vec(), num_bytes));
37 }
38 match buf.len() {
39 2 => return Ok((buf, num_bytes)),
40 _ => return Err(Error::SdpInvalidSyntax(String::from_utf8(buf)?)),
41 }
42 }
43 }
44
read_value<R: io::BufRead + io::Seek>(reader: &mut R) -> Result<(String, usize)>45 pub fn read_value<R: io::BufRead + io::Seek>(reader: &mut R) -> Result<(String, usize)> {
46 let mut value = String::new();
47 let num_bytes = reader.read_line(&mut value)?;
48 Ok((value.trim().to_string(), num_bytes))
49 }
50
index_of(element: &str, data: &[&str]) -> i3251 pub fn index_of(element: &str, data: &[&str]) -> i32 {
52 for (k, &v) in data.iter().enumerate() {
53 if element == v {
54 return k as i32;
55 }
56 }
57 -1
58 }
59
key_value_build(key: &str, value: Option<&String>) -> String60 pub fn key_value_build(key: &str, value: Option<&String>) -> String {
61 if let Some(val) = value {
62 format!("{key}{val}{END_LINE}")
63 } else {
64 "".to_string()
65 }
66 }
67