Files
aho_corasick
atty
beef
bitflags
bstr
byteorder
cfg_if
clap
clap_derive
codespan
codespan_reporting
crc_any
crypto_hash
csv
csv_core
debug_helper
filepath
fixed
fixed_macro
fixed_macro_impl
fixed_macro_types
fnv
foreign_types
foreign_types_shared
getrandom
glob
hashbrown
heck
hex
indexmap
itoa
lazy_static
libc
linked_hash_map
linked_hash_set
logos
logos_derive
lrl_test_compiler
maplit
memchr
memoffset
once_cell
openssl
openssl_sys
os_str_bytes
paste
pest
pest_derive
pest_generator
pest_meta
phf
phf_generator
phf_macros
phf_shared
ppv_lite86
proc_macro2
proc_macro_error
proc_macro_error_attr
proc_macro_hack
quote
rand
rand_chacha
rand_core
regex
regex_automata
regex_syntax
remove_dir_all
ring
rowan
rustc_hash
ryu
semver
semver_parser
serde
serde_derive
serde_json
siphasher
smallvec
smawk
smol_str
spin
stable_deref_trait
strsim
syn
taplo
tempfile
termcolor
text_size
textwrap
toml
triomphe
typenum
ucd_trie
unicode_linebreak
unicode_segmentation
unicode_width
unicode_xid
untrusted
utf8_ranges
vec_map
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use serde::Deserialize;

use crate::result::{diagnostic::Location, CompilerResult};

/// Represents a test
#[derive(Clone, PartialEq, Debug)]
pub struct Test {
    pub test_body: TestBody,
    pub aborts: Vec<Abort>,
}

/// Main body of the test
#[derive(Clone, PartialEq, Debug)]
pub struct TestBody {
    pub statements: Vec<Statement>,
}

/// Single abort sequence
#[derive(Clone, PartialEq, Debug)]
pub struct Abort {
    pub name: String,
    pub statements: Vec<Statement>,
}

#[derive(Clone, PartialEq, Debug, Default)]
/// Timing value
pub struct Timing {
    pub minutes: Option<u128>,
    pub seconds: Option<u128>,
    pub milliseconds: Option<u128>,
}

#[derive(Clone, PartialEq, Debug)]
/// Relay or sensor statement
pub enum Statement {
    Section(SectionStatement),
    Relay(RelayStatement),
    Sensor(SensorStatement),
}

#[derive(Clone, PartialEq, Debug)]
pub struct SectionStatement {
    pub name: String,
    pub time: SectionTime,
    pub statements: Vec<Statement>,
    /// Metadata from .tdf file
    pub metadata: Location,
}

#[derive(Clone, PartialEq, Debug)]
pub struct SectionTime {
    pub start: u128,
    pub duration: u128,
}

#[derive(Clone, PartialEq, Debug)]
/// Relay statement
pub struct RelayStatement {
    /// Time of relay operation
    pub time: u128,
    /// ID of relay
    pub id: String,
    /// set or unset the relay
    pub op: RelayOp,
    /// Metadata from .tdf file
    pub metadata: Location,
}

#[derive(Clone, PartialEq, Debug)]
/// Relay operations
pub enum RelayOp {
    Set,
    Unset,
}

#[derive(Clone, PartialEq, Debug)]
/// Sensor Statement
pub struct SensorStatement {
    /// Time statement is active
    pub time: SensorTime,
    /// Constraints defined in statement
    pub constraints: Vec<SensorConstraint>,
    /// Abort name for all constraints
    pub abort: String,
    /// Metadata from .tdf file
    pub metadata: Location,
}

#[derive(Clone, PartialEq, Debug)]
/// Duration or instant time for sensor constraint
pub enum SensorTime {
    Instant { time: u128 },
    Interval { start: u128, end: u128 },
}

#[derive(Clone, PartialEq, Debug)]
/// Constraint for a sensor
pub struct SensorConstraint {
    /// ID of sensor
    pub id: String,
    /// bounds of the sensor constraint
    pub sensor_bound: SensorBound,
    /// Abort name
    pub abort: String,
    /// Metadata from .tdf file
    pub metadata: Location,
}

#[derive(Clone, PartialEq, Debug, Deserialize)]
pub enum SensorBound {
    Boolean(bool),
    Numeric(SensorBoundNumeric),
}

#[derive(Clone, PartialEq, Debug, Deserialize)]
/// Bound type for sensor constraint
pub struct SensorBoundNumeric {
    pub left: f64,
    pub right: f64,
    pub unit: String,
}

impl Abort {
    pub const HARD_ABORT: &'static str = "HARD_ABORT";
}

#[derive(Clone, PartialEq, Debug)]
/// Metadata for a statement
pub struct Metadata {
    pub contents: String,
    pub line: u16,
}

impl Metadata {
    pub fn to_error_msg(&self, error_msg: &str) -> String {
        return format!(
            "\tLine: {}\n\t{}\n\n\t{}",
            self.line, self.contents, error_msg
        );
    }
}

impl Timing {
    pub fn to_ms(&self) -> u128 {
        ((self.minutes.unwrap_or_default() * 60) + self.seconds.unwrap_or_default()) * 1000
            + self.milliseconds.unwrap_or_default()
    }

    pub fn can_be_simplified(&self) -> bool {
        self.seconds.unwrap_or_default() >= 60 || self.milliseconds.unwrap_or_default() >= 1000
    }
}

impl Statement {
    fn to_absolute(&self, offset: u128) -> CompilerResult<Vec<Statement>> {
        let mut res = CompilerResult::new("Convert statement to absolute time");
        let mut absolute_statements: Vec<Statement> = Vec::new();
        match self {
            Statement::Section(statement) => {
                let statements_abs: Vec<Statement> = check!(res, statement.to_absolute());
                for statement_abs in statements_abs {
                    let mut statement_abs =
                        check!(res, Statement::to_absolute(&statement_abs, offset));
                    absolute_statements.append(&mut statement_abs);
                }
            }
            Statement::Sensor(statement) => {
                let statement_abs = check!(res, SensorStatement::to_absolute(statement, offset));
                absolute_statements.push(statement_abs);
            }
            Statement::Relay(statement) => {
                let statement_abs = check!(res, RelayStatement::to_absolute(statement, offset));
                absolute_statements.push(statement_abs);
            }
        }
        res.with_value(absolute_statements)
    }
}

impl RelayStatement {
    fn to_absolute(&self, offset: u128) -> CompilerResult<Statement> {
        let res = CompilerResult::new("Convert relay statement to absolute time");
        let mut relay_statement = self.clone();
        relay_statement.time += offset;

        res.with_value(Statement::Relay(relay_statement))
    }
}

impl SensorStatement {
    fn to_absolute(&self, offset: u128) -> CompilerResult<Statement> {
        let res = CompilerResult::new("Convert sensor statement to absolute time");
        let mut sensor_statement = self.clone();
        match sensor_statement.time {
            SensorTime::Instant { time } => {
                let absolute_time = time + offset;
                sensor_statement.time = SensorTime::Instant {
                    time: absolute_time,
                };
            }
            SensorTime::Interval { start, end } => {
                let absolute_start = start + offset;
                let absolute_end = end + offset;
                sensor_statement.time = SensorTime::Interval {
                    start: absolute_start,
                    end: absolute_end,
                };
            }
        }
        res.with_value(Statement::Sensor(sensor_statement))
    }
}

/// Get start and end time for SensorTime
impl SensorTime {
    pub fn get_start(&self) -> u128 {
        match self {
            SensorTime::Instant { time } => *time as u128,
            SensorTime::Interval { start, end: _ } => *start as u128,
        }
    }
    pub fn get_end(&self) -> u128 {
        match self {
            SensorTime::Instant { time } => *time as u128,
            SensorTime::Interval { start: _, end } => *end as u128,
        }
    }
}

impl SectionStatement {
    /// Check that all statements within a section don't extend
    /// past the end duration of the section
    pub fn check_statements_within_bounds(&self) -> CompilerResult<()> {
        let mut res = CompilerResult::status_only(
            "Check all statements stay within start/end bounds of the section",
        );
        for statement in &self.statements {
            let end = self.time.duration;
            match statement {
                Statement::Section(statement) => {
                    res.require(statement.check_statements_within_bounds());
                    let statement_end = statement.time.start + statement.time.duration;
                    if end < statement_end {
                        res.error((statement.metadata, "This section statement extends past the end of the section it is within!"));
                    }
                }
                Statement::Sensor(statement) => {
                    let statement_end = match statement.time {
                        SensorTime::Instant { time } => time,
                        SensorTime::Interval { start: _, end } => end,
                    };
                    if end < statement_end {
                        res.error((statement.metadata, "This sensor statement extends past the end of the section it is within!"));
                    }
                }
                Statement::Relay(statement) => {
                    let statement_end = statement.time;
                    if end < statement_end {
                        res.error((statement.metadata, "This relay statement extends past the end of the section it is within!"));
                    }
                }
            }
        }
        res
    }
    /// Get a vector containing all statements in the section
    /// with relative time values replaced by absolute times
    pub fn to_absolute(&self) -> CompilerResult<Vec<Statement>> {
        let mut res = CompilerResult::new("Retrieve statements from section with absolute time");
        let mut absolute_statements: Vec<Statement> = Vec::new();
        for statement in &self.statements {
            absolute_statements.append(&mut check!(
                res,
                Statement::to_absolute(statement, self.time.start)
            ));
        }
        res.with_value(absolute_statements)
    }
}