Skip to content

Commit

Permalink
Fix Delete Item, Restore Edit Event
Browse files Browse the repository at this point in the history
  • Loading branch information
doylep committed May 9, 2023
1 parent 7b4ee9b commit 0a57a88
Show file tree
Hide file tree
Showing 18 changed files with 455 additions and 53 deletions.
176 changes: 176 additions & 0 deletions src/definitions/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,182 @@ pub enum EventAction {
},
}


/// An web-safe (JSON readable) enum with various action options for each event. FIXME change to camel case
///
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
pub enum WebEventAction {
/// A variant to adjust media on one of the media channels.
AdjustMedia {
adjustment: MediaAdjustment,
},

/// A variant that links to one or more events to cancel. All upcoming
/// events that match the specified id(s) will be cancelled.
CancelEvent {
event: ItemId,
},

// A variant to cue a DMX fade on one of the channels
CueDmx {
fade: DmxFade,
},

/// A variant that links to one event to add to the queue These events may
/// be triggered immediately when delay is None, or after a delay if delay
/// is Some(delay).
CueEvent {
event: EventDelay,
},

/// A variant to cue media on one of the media channels.
CueMedia {
cue: MediaCue,
},

/// A variant used to change current status of the target status.
ModifyStatus {
status_id: ItemId,
new_state: ItemId,
},

/// A variant indicating a complete change in scene.
NewScene {
new_scene: ItemId,
},

/// A variant which contains a vector of data to save in the current game
/// logging file.
SaveData {
data: DataType,
},

/// A variant which selects an event based on the state of the indicated
/// status.
SelectEvent {
status_id: ItemId,
event_map: FnvHashMap<u32, ItemId>,
},

/// A variant which contains a type of data to include with the event
/// when broadcast to the system
SendData {
data: DataType,
},
}

// Implement conversions to and from WebEventAction
impl From<EventAction> for WebEventAction {
fn from(event_action: EventAction) -> Self {
match event_action {
// Convert keys to u32 for Select Event
EventAction::SelectEvent {
status_id,
mut event_map,
} => {
// Remap the ItemIds as u32
let mut new_event_map = FnvHashMap::default();
for (key, value) in event_map.drain() {
new_event_map.insert(key.id(), value);
}

// Return the completed select event
WebEventAction::SelectEvent {
status_id,
event_map: new_event_map,
}
}

// Leave the rest untouched
EventAction::AdjustMedia { adjustment } => WebEventAction::AdjustMedia { adjustment },
EventAction::CancelEvent { event } => WebEventAction::CancelEvent { event },
EventAction::CueDmx { fade } => WebEventAction::CueDmx { fade },
EventAction::CueEvent { event } => WebEventAction::CueEvent { event },
EventAction::CueMedia { cue } => WebEventAction::CueMedia { cue },
EventAction::ModifyStatus {
status_id,
new_state,
} => WebEventAction::ModifyStatus {
status_id,
new_state,
},
EventAction::NewScene { new_scene } => WebEventAction::NewScene { new_scene },
EventAction::SaveData { data } => WebEventAction::SaveData { data },
EventAction::SendData { data } => WebEventAction::SendData { data },
}
}
}
impl From<WebEventAction> for EventAction {
fn from(web_event_action: WebEventAction) -> Self {
match web_event_action {
// Convert keys to ItemId for Select Event
WebEventAction::SelectEvent {
status_id,
mut event_map,
} => {
// Remap the ItemIds as u32
let mut new_event_map = FnvHashMap::default();
for (key, value) in event_map.drain() {
new_event_map.insert(ItemId::new_unchecked(key), value); // Possible injection attack surface (i.e. an id over 29 bits). Minimal consequenses of this attack
}

// Return the completed select event
EventAction::SelectEvent {
status_id,
event_map: new_event_map,
}
}

// Leave the rest untouched
WebEventAction::AdjustMedia { adjustment } => EventAction::AdjustMedia { adjustment },
WebEventAction::CancelEvent { event } => EventAction::CancelEvent { event },
WebEventAction::CueDmx { fade } => EventAction::CueDmx { fade },
WebEventAction::CueEvent { event } => EventAction::CueEvent { event },
WebEventAction::CueMedia { cue } => EventAction::CueMedia { cue },
WebEventAction::ModifyStatus {
status_id,
new_state,
} => EventAction::ModifyStatus {
status_id,
new_state,
},
WebEventAction::NewScene { new_scene } => EventAction::NewScene { new_scene },
WebEventAction::SaveData { data } => EventAction::SaveData { data },
WebEventAction::SendData { data } => EventAction::SendData { data },
}
}
}

/// A convenient web-safe structure definition to specify each event
/// (Rust doesn't allow trait definitions for two implicit types)
///
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
pub struct WebEvent {
actions: Vec<WebEventAction>,
}

// Implement conversions to and from WebEvent
impl From<Event> for WebEvent {
fn from(mut event: Event) -> Self {
// Convert each action to the opposite type and return the result
let mut actions = Vec::new();
for action in event.drain(..) {
actions.push(action.into());
}
WebEvent { actions }
}
}
impl From<WebEvent> for Event {
fn from(mut web_event: WebEvent) -> Self {
// Convert each action to the opposite type and return the result
let mut event = Event::new();
for action in web_event.actions.drain(..) {
event.push(action.into());
}
event
}
}

/// A convenient type definition to specify each event
///
pub type Event = Vec<EventAction>;
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl ItemDescription {
#[allow(dead_code)]
pub fn new_default() -> ItemDescription {
ItemDescription {
description: "No Description.".to_string(),
description: "No Description".to_string(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/definitions/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub enum Modification {
#[serde(rename_all = "camelCase")]
ModifyEvent {
item_id: ItemId,
event: Option<Event>,
event: Option<WebEvent>,
},

/// A modification to add a group, modify an existing one, or delete it
Expand Down Expand Up @@ -356,7 +356,7 @@ pub struct WebReply {
pub enum WebReplyData {
// A variant that contains event detail
#[serde(rename_all = "camelCase")]
Event (Option<Event>),
Event (Option<WebEvent>),

// A variant that contains item detail
#[serde(rename_all = "camelCase")]
Expand Down
4 changes: 2 additions & 2 deletions src/definitions/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl IndexAccess {
}

// Otherwise, try to remove the item
if let Some(_) = index.remove(&item_id) {
if index.remove(&item_id).is_some() {
// If the item exists
return true;
}
Expand Down Expand Up @@ -434,7 +434,7 @@ impl StyleAccess {
}

// Otherwise, try to remove the item
if let Some(_) = map.remove(&selector) {
if map.remove(&selector).is_some() {
// If the item exists
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/item_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl ItemIndex {
}

// Otherwise, try to remove the item
if let Some(_) = self.index.remove(&item_id) {
if self.index.remove(&item_id).is_some() {
// If the item exists
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/style_sheet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl StyleSheet {
}

// Otherwise, try to remove the item
if let Some(_) = self.style_map.remove(&selector) {
if self.style_map.remove(&selector).is_some() {
// If the item exists
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/system_interface/event_handler/backup_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ mod tests {
.await;

// Make sure there is no existing backup
if let Some(_) = backup_handler.reload_backup(Vec::new()) {
if backup_handler.reload_backup(Vec::new()).is_some() {
panic!("Backup already existed before beginning of the test.");
}

Expand Down
Loading

0 comments on commit 0a57a88

Please sign in to comment.