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
#[macro_use]
pub(crate) mod macros {
#[macro_export]
macro_rules! attr {
( $src_name:ident, $struct_desc:literal, [
$( ($attr_name:ident, $attr_type:ty, $default:literal, $desc:literal) ),* $(,)?
]) => {
#[allow(dead_code)]
#[doc=$struct_desc]
#[derive(Clone)]
pub struct $src_name {
$( #[doc=$desc]
pub $attr_name : $attr_type ),*
}
impl $src_name {
#[allow(dead_code)]
fn getattr<S: Into<String>>(&self, key: S) -> Option<f64> {
let k: String = key.into();
match &k as &str {
$( stringify!($attr_name) => Some(self.$attr_name)),*,
_ => None,
}
}
}
}
}
#[macro_export]
macro_rules! from_opt_type {
( $dest_type:ident, $from_type:ident, $struct_desc:literal, [
$( ($attr_name:ident, $attr_type:ty, $default:literal, $desc:literal) ),* $(,)?
]) => {
#[allow(dead_code)]
#[doc=$struct_desc]
#[derive(Clone)]
pub struct $dest_type {
$( #[doc=$desc]
pub $attr_name : $attr_type ),*
}
impl From<$from_type> for $dest_type {
pub fn from(specs: $from_type) -> Self {
Self {
$($attr_name : if let Some(val) = specs.$attr_name { val } else { $default }; ),*,
}
}
}
impl Default for $dest_type {
fn default() -> Self {
Self {
$($attr_name : $default),*,
}
}
}
}
}
#[macro_export]
macro_rules! paramstruct {
( $src_name:ident, $struct_desc:literal, [
$( ($attr_name:ident, $attr_type:ty, $desc:literal) ),* $(,)?
]) => {
#[doc=$struct_desc]
#[derive(Clone, Copy, Default)]
pub struct $src_name {
$( #[doc=$desc]
pub $attr_name : $attr_type ),*
}
}
}
#[macro_export]
macro_rules! specgen {
( $specs_name:ident, $vals_name: ident, $struct_desc:literal, [
$( ($attr_name:ident, $attr_type:ty, $desc:literal) ),* $(,)?
], [
$( ($val_name:ident, $val_type:ty) ),* $(,)?
]$(,)? ) => {
#[doc=$struct_desc]
#[derive(Clone, Copy, Default, Serialize, Deserialize, Debug)]
pub struct $specs_name {
$( #[doc=$desc]
#[serde(default)]
pub $attr_name : Option<$attr_type> ),*
}
#[doc=$struct_desc]
#[derive(Clone, Copy, Default, Serialize, Deserialize, Debug)]
pub struct $vals_name {
$( pub $attr_name : $attr_type, )*
$( pub $val_name : $val_type, )*
}
}
}
#[cfg(test)]
mod tests {
use crate::assert::*;
use crate::spresult::TestResult;
attr!(
SampleModel,
"Doc-string for Sample Model",
[
(param1, f64, 1.1, "First Param"),
(param22, f64, 2.2, "Parameter #2"),
(par33, f64, 3.3, "3param"),
]
);
#[test]
fn test1() -> TestResult {
let s = SampleModel {
param1: 1.1,
param22: 22.22,
par33: 333.333,
};
assert(s.param1).eq(1.1)?;
assert(s.param22).eq(22.22)?;
assert(s.par33).eq(333.333)?;
assert(s.getattr("param1")).eq(Some(s.param1))?;
assert(s.getattr("param22")).eq(Some(s.param22))?;
assert(s.getattr("par33")).eq(Some(s.par33))?;
assert(s.getattr("fizzbuzz")).eq(None)?;
Ok(())
}
}
}
pub mod analysis;
pub mod circuit;
pub mod comps;
pub mod defs;
pub mod elab;
pub mod proto;
pub mod sparse21;
pub mod spresult;
pub use analysis::*;
pub use proto::*;
pub use spresult::*;
pub(crate) use spnum::*;
mod assert;
mod spnum;
#[cfg(test)]
mod tests;
#[macro_use]
extern crate spice21procs;