Skip to main content

rusty_tracks/messages/
train.rs

1use crate::value::Value;
2use track_rails::message_generated::protocol;
3use track_rails::message_generated::protocol::Payload;
4
5#[derive(Debug)]
6pub struct Train {
7    values: Vec<Value>
8}
9
10impl<'a> TryFrom<protocol::Message<'a>> for Train {
11    type Error = String;
12
13    fn try_from(msg: protocol::Message<'a>) -> Result<Self, Self::Error> {
14        match msg.data_type() {
15            Payload::Train => {
16                match msg.data_as_train() {
17                    None => Err(String::from("Train data type is not correct")),
18                    Some(t) => {
19                        let mut values = Vec::new();
20                        for value in t.values() {
21                            values.push(Value::from(value))
22                        }
23                        Ok(Train {values})
24                    }
25                }
26            }
27            _ => {
28                Err(format!("Unexpected message type: {:?}", msg.data_type()))?
29            }
30        }
31    }
32}