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
//! This module contains all of the data/model for a test program, including all flows,
//! test templates, test instances, etc.

mod bin;
mod flow_id;
mod limit;
mod model;
mod template_loader;
mod test;

use crate::Result as OrigenResult;
pub use bin::Bin;
pub use flow_id::FlowID;
pub use limit::Limit;
pub use model::Model;
use std::fmt;
use std::str::FromStr;
pub use test::Test;

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum PatternGroupType {
    Patset,
    Patgroup,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum GroupType {
    Flow,
    Test,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum BinType {
    Good,
    Bad,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum FlowCondition {
    IfJob(Vec<String>),
    UnlessJob(Vec<String>),
    IfEnable(Vec<String>),
    UnlessEnable(Vec<String>),
    IfPassed(Vec<String>),
    UnlessPassed(Vec<String>),
    IfFailed(Vec<String>),
    UnlessFailed(Vec<String>),
    IfRan(Vec<String>),
    UnlessRan(Vec<String>),
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum ParamValue {
    String(String),
    Int(i64),
    UInt(u64),
    Float(f64),
    Current(f64),
    Voltage(f64),
    Time(f64),
    Frequency(f64),
    Bool(bool),
    // Like a string, but any value assigned to such an attribute will be accepted and converted into a string representation
    Any(String),
}

impl ParamValue {
    pub fn is_type(&self, kind: &ParamType) -> bool {
        match self {
            ParamValue::String(_) => kind == &ParamType::String,
            ParamValue::Int(_) => kind == &ParamType::Int,
            ParamValue::UInt(_) => kind == &ParamType::UInt,
            ParamValue::Float(_) => kind == &ParamType::Float,
            ParamValue::Current(_) => kind == &ParamType::Current,
            ParamValue::Voltage(_) => kind == &ParamType::Voltage,
            ParamValue::Time(_) => kind == &ParamType::Time,
            ParamValue::Frequency(_) => kind == &ParamType::Frequency,
            ParamValue::Bool(_) => kind == &ParamType::Bool,
            ParamValue::Any(_) => true,
        }
    }
}

impl fmt::Display for ParamValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParamValue::String(v) => write!(f, "{}", v),
            ParamValue::Int(v) => write!(f, "{}", v),
            ParamValue::UInt(v) => write!(f, "{}", v),
            ParamValue::Float(v) => write!(f, "{}", v),
            ParamValue::Current(v) => write!(f, "{}A", v),
            ParamValue::Voltage(v) => write!(f, "{}V", v),
            ParamValue::Time(v) => write!(f, "{}s", v),
            ParamValue::Frequency(v) => write!(f, "{}Hz", v),
            ParamValue::Bool(v) => write!(f, "{}", v),
            ParamValue::Any(v) => write!(f, "{}", v),
        }
    }
}

#[derive(Debug, PartialEq, Clone, Serialize)]
pub enum ParamType {
    String,
    Int,
    UInt,
    Float,
    Current,
    Voltage,
    Time,
    Frequency,
    Bool,
    Any,
}

impl FromStr for ParamType {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Accept any case and with or without underscores
        let str = s.to_lowercase();
        match str.trim() {
            "string" => Ok(ParamType::String),
            "int" | "integer" => Ok(ParamType::Int),
            "uint" | "uinteger" => Ok(ParamType::UInt),
            "float" | "number" | "num" => Ok(ParamType::Float),
            "current" | "curr" | "i" => Ok(ParamType::Current),
            "voltage" | "volt" | "v" => Ok(ParamType::Voltage),
            "time" | "t" | "s" => Ok(ParamType::Time),
            "frequency" | "freq" | "hz" => Ok(ParamType::Frequency),
            "boolean" | "bool" => Ok(ParamType::Bool),
            "any" => Ok(ParamType::Any),
            _ => Err(format!("'{}' is not a valid parameter type, the available types are: String, Int, UInt, Number, Current, Voltage, Time, Frequency, Bool, Any", str)),
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub enum Constraint {
    In(Vec<ParamValue>),
    GT(ParamValue),
    GTE(ParamValue),
    LT(ParamValue),
    LTE(ParamValue),
}

impl Constraint {
    pub fn is_satisfied(&self, value: &ParamValue) -> OrigenResult<()> {
        match self {
            Constraint::In(values) => {
                if values.iter().any(|v| v == value) {
                    Ok(())
                } else {
                    error!(
                        "'{}' is not one of the permitted values: {}",
                        value,
                        values
                            .iter()
                            .map(|v| format!("'{}'", v))
                            .collect::<Vec<String>>()
                            .join(", ")
                    )
                }
            }
            // Unimplemented for now, but placeholders in case such contraints are supported in future
            Constraint::GT(_) => Ok(()),
            Constraint::GTE(_) => Ok(()),
            Constraint::LT(_) => Ok(()),
            Constraint::LTE(_) => Ok(()),
        }
    }
}