1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

use nom::character::complete::{hex_digit1, digit1, space0, alphanumeric1};

#[cfg(test)]
mod tests {
    use crate::candump_parser::*;

    #[test]
    fn it_works() {
        let exp = DumpEntry {
            timestamp: Timestamp { seconds: 1547046014, nanos: 597158 },
            can_interface: "vcan0".to_string(),
            can_frame: CanFrame {
                frame_id: 123,
                frame_body: 455,
            }
        };
       assert_eq!(dump_entry("(1547046014.597158) vcan0 7B#1C7"), Ok(("", exp)));
    }
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Timestamp {
    pub seconds: u64,
    pub nanos: u64,
}

named!(timestamp<&str, Timestamp>,
    do_parse!(
                 tag!("(")                             >>
        seconds: map_res!(digit1, |d: &str| d.parse()) >>
                 tag!(".")                             >>
        nanos:   map_res!(digit1, |d: &str| d.parse()) >>
                 tag!(")")                             >>
        (Timestamp { seconds, nanos })
    )
);

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CanFrame {
    pub frame_id: u32,
    pub frame_body: u64,
}

named!(can_frame<&str, CanFrame>,
    do_parse!(
        frame_id:   map_res!(hex_digit1, |d| u32::from_str_radix(d, 16))  >>
                    tag!("#")                                             >>
        frame_body: map_res!(hex_digit1, |d| u64::from_str_radix(d, 16))  >>
        (CanFrame { frame_id, frame_body })
    )
);

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DumpEntry {
    timestamp: Timestamp,
    can_interface: String,
    can_frame: CanFrame,
}

impl DumpEntry {
    pub fn timestamp(&self) -> &Timestamp {
        &self.timestamp
    }

    pub fn can_interface(&self) -> &str {
        &self.can_interface
    }

    pub fn can_frame(&self) -> &CanFrame {
        &self.can_frame
    }
}

named!(pub dump_entry<&str, DumpEntry>,
    do_parse!(
        timestamp:     timestamp     >>
                       space0        >>
        can_interface: alphanumeric1 >>
                       space0        >>
        can_frame:     can_frame     >>
        (DumpEntry { timestamp, can_interface: can_interface.to_string(), can_frame })
    )
);