use std::borrow::Cow;
use std::{ops::Deref, ops::DerefMut};
use serde::Deserialize;
use serde::Serialize;
use crate::Registry;
use crate::RegistryError;
use super::McIdentifier;
use super::McText;
#[derive(Debug, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct McEvent {
pub name: McIdentifier,
pub index: u16,
pub id: u16,
pub target_cycle_time_ns: u32,
pub function: Option<McText>,
pub unit: Option<usize>,
pub cfa: i32,
}
impl McEvent {
pub fn new<T: Into<McIdentifier>>(name: T, index: u16, id: u16, target_cycle_time_ns: u32) -> Self {
let name: McIdentifier = name.into();
McEvent {
name,
index,
id,
target_cycle_time_ns,
function: None,
unit: None,
cfa: 0,
}
}
pub fn get_name(&self) -> &'static str {
self.name.as_str()
}
pub fn get_id(&self) -> u16 {
self.id
}
pub fn set_id(&mut self, id: u16) {
self.id = id;
}
pub fn get_unique_name(&self, registry: &Registry) -> Cow<'static, str> {
if self.index > 0 {
if registry.get_prefix_names_mode() {
Cow::Owned(format!("{}.{}_{}", registry.application.get_name(), self.name, self.index))
} else {
Cow::Owned(format!("{}_{}", self.name, self.index))
}
} else {
if registry.get_prefix_names_mode() {
Cow::Owned(format!("{}_{}", self.name, self.index))
} else {
Cow::Borrowed(self.name.as_str())
}
}
}
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct McEventList(Vec<McEvent>);
impl Deref for McEventList {
type Target = [McEvent];
fn deref(&self) -> &[McEvent] {
&self.0
}
}
impl DerefMut for McEventList {
fn deref_mut(&mut self) -> &mut [McEvent] {
&mut self.0
}
}
impl McEventList {
pub fn new() -> Self {
McEventList(Vec::with_capacity(100))
}
pub fn add_event(&mut self, event: McEvent) -> Result<(), RegistryError> {
log::debug!("Registry add_event: {:?} ", event);
if self.find_event(&event.name, event.index).is_some() {
return Err(RegistryError::Duplicate(event.name.to_string()));
}
if self.find_event_id(event.id).is_some() {
return Err(RegistryError::Duplicate(event.name.to_string()));
}
self.push(event);
Ok(())
}
pub fn push(&mut self, object: McEvent) {
self.0.push(object);
}
pub fn sort_by_name(&mut self) {
self.0.sort_by_key(|a| a.name);
}
pub fn sort_by_id(&mut self) {
self.0.sort_by_key(|a| a.id);
}
pub fn find_event(&self, name: &str, index: u16) -> Option<&McEvent> {
self.0.iter().find(|e| e.index == index && e.name == name)
}
pub fn find_event_id(&self, id: u16) -> Option<&McEvent> {
self.0.iter().find(|e| e.id == id)
}
pub fn find_event_by_location(&self, unit_idx: usize, function: &str) -> Option<&McEvent> {
self.0.iter().find(|e| e.unit == Some(unit_idx) && e.function.as_deref() == Some(function))
}
pub fn set_event_location(&mut self, name: &str, unit_idx: usize, function: &str, cfa: i32) -> Result<(), RegistryError> {
if let Some(event) = self.0.iter_mut().find(|e| e.name == name) {
event.unit = Some(unit_idx);
event.function = Some(function.to_string().into());
event.cfa = cfa;
Ok(())
} else {
Err(RegistryError::NotFound(name.to_string()))
}
}
}
impl<'a> IntoIterator for &'a McEventList {
type Item = &'a McEvent;
type IntoIter = std::slice::Iter<'a, McEvent>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl<'a> IntoIterator for &'a mut McEventList {
type Item = &'a mut McEvent;
type IntoIter = std::slice::IterMut<'a, McEvent>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter_mut()
}
}