libits_client/analyse/
cause.rs

1// Software Name: its-client
2// SPDX-FileCopyrightText: Copyright (c) 2016-2022 Orange
3// SPDX-License-Identifier: MIT License
4//
5// This software is distributed under the MIT license, see LICENSE.txt file for more details.
6//
7// Author: Frédéric GARDES <frederic.gardes@orange.com> et al.
8// Software description: This Intelligent Transportation Systems (ITS) [MQTT](https://mqtt.org/) client based on the [JSon](https://www.json.org) [ETSI](https://www.etsi.org/committee/its) specification transcription provides a ready to connect project for the mobility (connected and autonomous vehicles, road side units, vulnerable road users,...).
9use crate::reception::exchange::message::Message;
10use crate::reception::exchange::Exchange;
11use std::fmt::Formatter;
12
13pub struct Cause {
14    pub m_type: String,
15    pub id: String,
16}
17
18impl std::fmt::Display for Cause {
19    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20        write!(f, "/cause_type:{}/cause_id:{}", self.m_type, self.id)
21    }
22}
23
24impl Cause {
25    fn new(m_type: String, id: String) -> Self {
26        Self { m_type, id }
27    }
28
29    pub fn from_exchange(exchange: &Exchange) -> Option<Cause> {
30        return match &exchange.message {
31            Message::CAM(message) => Some(Cause::new(
32                exchange.type_field.clone(),
33                format!("{}/{}", message.station_id, message.generation_delta_time),
34            )),
35            Message::CPM(message) => Some(Cause::new(
36                exchange.type_field.clone(),
37                format!("{}/{}", message.station_id, message.generation_delta_time),
38            )),
39            _ => None,
40        };
41    }
42}