sdp_rs/tokenizers/
value.rs

1use crate::TResult;
2
3#[derive(Debug, PartialEq, Eq, Clone)]
4pub struct Tokenizer<'a, const C: char> {
5    pub value: &'a str,
6}
7
8impl<'a, const C: char> Tokenizer<'a, C> {
9    pub fn tokenize(part: &'a str) -> TResult<'a, Self> {
10        use crate::parser_utils::until_newline;
11        use nom::{bytes::complete::tag, sequence::preceded};
12
13        let (rem, value) = preceded(tag(Self::prefix().as_str()), until_newline)(part)?;
14
15        Ok((rem, value.into()))
16    }
17
18    //TODO: this should be generated by a concat-related macro, but atm at stable this is not
19    //possible, will come back once const generics expands on stable
20    fn prefix() -> String {
21        format!("{}=", C)
22    }
23}
24
25impl<'a, const C: char> From<&'a str> for Tokenizer<'a, C> {
26    fn from(value: &'a str) -> Self {
27        Self { value }
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn tokenizer() {
37        let value = concat!("i=a value here #sdp #rocks\r\nsomething",);
38
39        assert_eq!(
40            Tokenizer::<'i'>::tokenize(value),
41            Ok(("something", "a value here #sdp #rocks".into())),
42        );
43    }
44}