diff options
author | Daniel Schadt <kingdread@gmx.de> | 2020-04-29 13:54:49 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2020-04-29 13:54:49 +0200 |
commit | c59b4a0d769ba4887604ae66e3d5edf3b8e387f4 (patch) | |
tree | d6c9385ca8e1fefe114ec259df85b822347dc1c9 /src/lib.rs | |
parent | 52f20f5fa0e27e4687c868ec684a0af5a4fca62f (diff) | |
download | evtclib-c59b4a0d769ba4887604ae66e3d5edf3b8e387f4.tar.gz evtclib-c59b4a0d769ba4887604ae66e3d5edf3b8e387f4.tar.bz2 evtclib-c59b4a0d769ba4887604ae66e3d5edf3b8e387f4.zip |
replace own from_raw with TryFrom implementation
Hooking into the standard Rust system is probably better in the long-run
than having those separate from_raw methods on all of our objects.
Most end users probably won't even need them, as they will use the
higher level functionality provided by evtclib::process.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 61 |
1 files changed, 36 insertions, 25 deletions
@@ -17,6 +17,7 @@ //! (Look at the note on "Buffering" in the [parser //! module](raw/parser/index.html#buffering)) +use std::convert::TryFrom; use std::marker::PhantomData; use getset::{CopyGetters, Getters}; @@ -176,26 +177,6 @@ impl AgentKind { }) } - /// Extract the correct `AgentKind` from the given [raw agent][raw::Agent]. - /// - /// This automatically discerns between player, gadget and characters. - /// - /// Note that in most cases, you probably want to use [`Agent::from_raw`][Agent::from_raw] or - /// even [`process`][process] instead of this function. - pub fn from_raw(raw_agent: &raw::Agent) -> Result<AgentKind, EvtcError> { - if raw_agent.is_character() { - Ok(AgentKind::Character(AgentKind::from_raw_character( - raw_agent, - )?)) - } else if raw_agent.is_gadget() { - Ok(AgentKind::Gadget(AgentKind::from_raw_gadget(raw_agent)?)) - } else if raw_agent.is_player() { - Ok(AgentKind::Player(AgentKind::from_raw_player(raw_agent)?)) - } else { - Err(EvtcError::InvalidData) - } - } - /// Accesses the inner [`Player`][Player] struct, if available. pub fn as_player(&self) -> Option<&Player> { if let AgentKind::Player(ref player) = *self { @@ -239,6 +220,30 @@ impl AgentKind { } } +impl TryFrom<&raw::Agent> for AgentKind { + type Error = EvtcError; + + /// Extract the correct `AgentKind` from the given [raw agent][raw::Agent]. + /// + /// This automatically discerns between player, gadget and characters. + /// + /// Note that in most cases, you probably want to use `Agent::try_from` or even + /// [`process`][process] instead of this function. + fn try_from(raw_agent: &raw::Agent) -> Result<Self, Self::Error> { + if raw_agent.is_character() { + Ok(AgentKind::Character(AgentKind::from_raw_character( + raw_agent, + )?)) + } else if raw_agent.is_gadget() { + Ok(AgentKind::Gadget(AgentKind::from_raw_gadget(raw_agent)?)) + } else if raw_agent.is_player() { + Ok(AgentKind::Player(AgentKind::from_raw_player(raw_agent)?)) + } else { + Err(EvtcError::InvalidData) + } + } +} + /// An agent. /// /// Agents in arcdps are very versatile, as a lot of things end up being an "agent". This includes: @@ -348,10 +353,12 @@ pub struct Agent<Kind = ()> { phantom_data: PhantomData<Kind>, } -impl Agent { +impl TryFrom<&raw::Agent> for Agent { + type Error = EvtcError; + /// Parse a raw agent. - pub fn from_raw(raw_agent: &raw::Agent) -> Result<Agent, EvtcError> { - let kind = AgentKind::from_raw(raw_agent)?; + fn try_from(raw_agent: &raw::Agent) -> Result<Self, Self::Error> { + let kind = AgentKind::try_from(raw_agent)?; Ok(Agent { addr: raw_agent.addr, kind, @@ -630,7 +637,11 @@ pub fn process(data: &raw::Evtc) -> Result<Log, EvtcError> { // Set the master addr field set_agent_masters(data, &mut agents)?; - let events = data.events.iter().filter_map(Event::from_raw).collect(); + let events = data + .events + .iter() + .filter_map(|e| Event::try_from(e).ok()) + .collect(); Ok(Log { agents, @@ -643,7 +654,7 @@ fn setup_agents(data: &raw::Evtc) -> Result<Vec<Agent>, EvtcError> { let mut agents = Vec::with_capacity(data.agents.len()); for raw_agent in &data.agents { - agents.push(Agent::from_raw(raw_agent)?); + agents.push(Agent::try_from(raw_agent)?); } Ok(agents) } |