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
use std::collections::HashMap;

#[derive(Debug, Default, Clone, PartialEq)]
pub struct PsfAst<'a> {
    pub header: Header<'a>,
    pub types: Types<'a>,
    pub sweeps: Vec<SignalRef<'a>>,
    pub traces: Vec<Trace<'a>>,
    pub values: SignalValues,
}

impl<'a> PsfAst<'a> {
    pub fn new() -> Self {
        Self::default()
    }
}

#[derive(Debug, Default, Clone, PartialEq)]
pub struct Header<'a> {
    pub values: HashMap<&'a str, Value<'a>>,
}

#[derive(Debug, Default, Clone, PartialEq)]
pub struct Types<'a> {
    pub types: HashMap<TypeId, TypeDef<'a>>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TypeId(pub u32);

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TraceId(pub u32);

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GroupId(pub u32);

impl From<GroupId> for TraceId {
    fn from(value: GroupId) -> Self {
        Self(value.0)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypeDef<'a> {
    pub id: TypeId,
    pub name: &'a str,
    pub data_type: DataType,
    pub properties: Properties<'a>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct NamedValue<'a> {
    pub name: &'a str,
    pub value: Value<'a>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u32)]
pub enum DataType {
    Int8 = 1,
    String = 2,
    Int32 = 5,
    Real = 11,
    Complex = 12,
    Struct = 16,
}

impl DataType {
    pub fn from_u32(value: u32) -> Self {
        match value {
            1 => Self::Int8,
            2 => Self::String,
            5 => Self::Int32,
            11 => Self::Real,
            12 => Self::Complex,
            16 => Self::Struct,
            _ => panic!("Unexpected data type: {value}"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Value<'a> {
    Int(i64),
    Real(f64),
    Str(&'a str),
    NaN,
}

impl<'a> Value<'a> {
    pub fn int(&self) -> i64 {
        use Value::*;
        match self {
            Int(v) => *v,
            _ => panic!("Failed to unwrap value as integer"),
        }
    }
    pub fn real(&self) -> f64 {
        use Value::*;
        match self {
            Real(v) => *v,
            _ => panic!("Failed to unwrap value as real"),
        }
    }
    pub fn str(&self) -> &'a str {
        use Value::*;
        match self {
            Str(v) => v,
            _ => panic!("Failed to unwrap value as str"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Sweep<'a> {
    pub name: &'a str,
    pub sweep_type: &'a str,
    pub kinds: Vec<Kind<'a>>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Kind<'a> {
    Float,
    Double,
    Complex,
    Int,
    Byte,
    Long,
    String,
    Array,
    Struct(Vec<TypeDef<'a>>),
    Prop(Prop<'a>),
    Star,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Trace<'a> {
    Group(TraceGroup<'a>),
    Signal(SignalRef<'a>),
}

impl<'a> Trace<'a> {
    pub fn group(&self) -> &TraceGroup {
        match self {
            Self::Group(g) => g,
            _ => panic!("Cannot unwrap signal as group"),
        }
    }

    pub fn signal(&self) -> &SignalRef {
        match self {
            Self::Signal(s) => s,
            _ => panic!("Cannot unwrap group trace as signal"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct TraceGroup<'a> {
    pub name: &'a str,
    pub count: u32,
    pub id: GroupId,
    pub signals: Vec<SignalRef<'a>>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Properties<'a> {
    pub values: Vec<NamedValue<'a>>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct SignalRef<'a> {
    pub id: TraceId,
    pub name: &'a str,
    pub type_id: TypeId,
    pub properties: Properties<'a>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Prop<'a> {
    pub values: Vec<NamedValue<'a>>,
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct SignalValues {
    pub values: HashMap<TraceId, Values>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Values {
    Complex(Vec<(f64, f64)>),
    Real(Vec<f64>),
}

impl Values {
    pub fn unwrap_real(self) -> Vec<f64> {
        match self {
            Self::Real(v) => v,
            _ => panic!("not a real value vector"),
        }
    }

    pub fn unwrap_complex(self) -> Vec<(f64, f64)> {
        match self {
            Self::Complex(v) => v,
            _ => panic!("not a complex value vector"),
        }
    }

    pub fn real(&self) -> &Vec<f64> {
        match self {
            Self::Real(ref v) => v,
            _ => panic!("not a real value vector"),
        }
    }

    pub fn complex(&self) -> &Vec<(f64, f64)> {
        match self {
            Self::Complex(ref v) => v,
            _ => panic!("not a complex value vector"),
        }
    }

    pub fn real_mut(&mut self) -> &mut Vec<f64> {
        match self {
            Self::Real(ref mut v) => v,
            _ => panic!("not a real value vector"),
        }
    }

    pub fn complex_mut(&mut self) -> &mut Vec<(f64, f64)> {
        match self {
            Self::Complex(ref mut v) => v,
            _ => panic!("not a complex value vector"),
        }
    }
}