evtc_parse/
log_transformed.rs

1use crate::{Agent, EventKind, Header, Log, Parse, ParseError, Skill};
2use std::io;
3
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7/// A transformed EVTC log.
8#[derive(Debug, Clone)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10pub struct LogTransformed {
11    /// The log header with meta information.
12    pub header: Header,
13
14    /// Agents (entities) present in the log.
15    pub agents: Vec<Agent>,
16
17    /// Information about skills used in the log.
18    pub skills: Vec<Skill>,
19
20    /// Every [`Event`](crate::Event) occurring in the log transformed as [`EventKind`].
21    pub events: Vec<EventKind>,
22}
23
24impl LogTransformed {
25    /// Returns the [`Agent`] with the given id.
26    #[inline]
27    pub fn agent(&self, id: u64) -> Option<&Agent> {
28        self.agents.iter().find(|agent| agent.id == id)
29    }
30
31    /// Returns a mutable reference to the [`Agent`] with the given id.
32    #[inline]
33    pub fn agent_mut(&mut self, id: u64) -> Option<&mut Agent> {
34        self.agents.iter_mut().find(|agent| agent.id == id)
35    }
36
37    /// Returns the name of the [`Agent`] with the given id.
38    #[inline]
39    pub fn agent_name(&self, id: u64) -> Option<&[String]> {
40        self.agent(id).map(|agent| agent.name.as_slice())
41    }
42
43    /// Returns the [`Skill`] with the given id.
44    #[inline]
45    pub fn skill(&self, id: u32) -> Option<&Skill> {
46        self.skills.iter().find(|skill| skill.id == id)
47    }
48
49    /// Returns a mutable reference to the [`Skill`] with the given id.
50    #[inline]
51    pub fn skill_mut(&mut self, id: u32) -> Option<&mut Skill> {
52        self.skills.iter_mut().find(|skill| skill.id == id)
53    }
54
55    /// Returns the name of the [`Skill`] with the given id.
56    #[inline]
57    pub fn skill_name(&self, id: u32) -> Option<&str> {
58        self.skill(id).map(|skill| skill.name.as_str())
59    }
60}
61
62impl From<Log> for LogTransformed {
63    #[inline]
64    fn from(log: Log) -> Self {
65        Self {
66            header: log.header,
67            agents: log.agents,
68            skills: log.skills,
69            events: log
70                .events
71                .into_iter()
72                .map(|event| event.into_kind())
73                .collect(),
74        }
75    }
76}
77
78impl Parse for LogTransformed {
79    type Error = ParseError;
80
81    fn parse(input: &mut impl io::Read) -> Result<Self, Self::Error> {
82        Log::parse(input).map(Into::into)
83    }
84}