thot_core/project/
script_association.rs

1use crate::types::ResourceId;
2use std::cmp::{Eq, Ordering, PartialEq, PartialOrd};
3use std::hash::Hash;
4
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8// **************************
9// *** Script Association ***
10// **************************
11
12/// Represents an association between a Script and a Container.
13/// Contains information on the script to be run,
14/// whether the Script should be run,
15/// and the order of its execution relative to the current Container.
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[derive(Debug, PartialEq, Eq, Hash, Clone)]
18pub struct ScriptAssociation {
19    pub script: ResourceId,
20    pub autorun: bool,
21    pub priority: i32,
22}
23
24impl ScriptAssociation {
25    pub fn new(script: ResourceId) -> Self {
26        ScriptAssociation {
27            script,
28            autorun: true,
29            priority: 0,
30        }
31    }
32}
33
34impl Into<RunParameters> for ScriptAssociation {
35    fn into(self) -> RunParameters {
36        RunParameters {
37            autorun: self.autorun,
38            priority: self.priority,
39        }
40    }
41}
42
43// **********************
44// *** Run Parameters ***
45// **********************
46
47#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
48#[derive(Debug, PartialEq, Eq, Hash, Clone)]
49pub struct RunParameters {
50    pub autorun: bool,
51    pub priority: i32,
52}
53
54impl RunParameters {
55    pub fn new() -> Self {
56        RunParameters {
57            autorun: true,
58            priority: 0,
59        }
60    }
61
62    /// Converts self into a script association for the given Script.
63    pub fn to_association(self, script: ResourceId) -> ScriptAssociation {
64        let mut assoc = ScriptAssociation::new(script);
65        assoc.autorun = self.autorun;
66        assoc.priority = self.priority;
67
68        assoc
69    }
70}
71
72impl PartialOrd for RunParameters {
73    /// Ordering is based on the `priority` field.
74    /// If the `priority` fields are equal and `autorun` state is equal,
75    /// results in the two objects being equal.
76    /// If the `priority` fields are equal, but the `autorun` fields are not
77    /// equal, results in the two being uncomparable.
78    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
79        let me = self.priority;
80        let you = other.priority;
81        if me < you {
82            return Some(Ordering::Less);
83        } else if me > you {
84            return Some(Ordering::Greater);
85        } else if self.autorun == other.autorun {
86            // priorities equal
87            return Some(Ordering::Equal);
88        }
89
90        // priorities equal, autorun not
91        // can not compare
92        None
93    }
94}
95
96#[cfg(test)]
97#[path = "./script_association_test.rs"]
98mod script_association_test;