1use crate::error::{LibErrorContext, LibError};
2use crate::model::{LibGroup, LibPin, LibPinDirection};
3
4use super::FromLibGroup;
5
6impl FromLibGroup for LibPin {
7 type Error = LibError;
8
9 fn from_group(group: LibGroup) -> Result<Self, LibError> {
10 let mut group = group;
11 let name = group.expect_name().context(|| "pin name")?;
12 let err_context = |msg: &str| format!("{} in `{}` pin", msg, &name);
13
14 let get_bool_opt =
16 |key: &str| group.attr_bool_opt(key, || err_context(&format!("semantic `{}` attr", key)));
17 let get_float_opt =
18 |key: &str| group.attr_float_opt(key, || err_context(&format!("semantic `{}` attr", key)));
19 let get_string_opt =
20 |key: &str| group.attr_string_opt(key, || err_context(&format!("semantic `{}` attr", key))).map(|v| v.map(String::from));
21 let get_ident_opt =
22 |key: &str| group.attr_ident_opt(key, || err_context(&format!("semantic `{}` attr", key))).map(|v| v.map(String::from));
23 let get_expr_opt =
24 |key: &str| group.attr_expr_opt(key, &|| err_context(&format!("semantic `{}` attr", key)));
25
26
27 let alive_during_power_up = get_bool_opt("alive_during_power_up")?;
28 let always_on = get_bool_opt("always_on")?;
29
30 let antenna_diode_type = group.attr_parse_opt("antenna_diode_type", &|| err_context("semantic `antenna_diode_type` attr"))?;
31 let nextstate_type = group.attr_parse_opt("nextstate_type", &|| err_context("semantic `nextstate_type` attr"))?;
32
33 let antenna_diode_related_ground_pins = get_string_opt("antenna_diode_related_ground_pins")?;
34 let antenna_diode_related_power_pins = get_string_opt("antenna_diode_related_power_pins")?;
35 let bit_width = get_float_opt("bit_width")?.map(|v| v as usize);
36
37 let capacitance = get_float_opt("capacitance")?;
38 let fall_capacitance = get_float_opt("fall_capacitance")?;
39 let fanout_load = get_float_opt("fanout_load")?;
40 let drive_current = get_float_opt("drive_current")?;
41 let fall_current_slope_after_threshold = get_float_opt("fall_current_slope_after_threshold")?;
42 let fall_current_slope_before_threshold = get_float_opt("fall_current_slope_before_threshold")?;
43 let fall_time_after_threshold = get_float_opt("fall_time_after_threshold")?;
44 let fall_time_before_threshold = get_float_opt("fall_time_before_threshold")?;
45
46 let max_capacitance = get_float_opt("max_capacitance")?;
47 let min_capacitance = get_float_opt("min_capacitance")?;
48 let max_fanout = get_float_opt("max_fanout")?;
49 let min_fanout = get_float_opt("min_fanout")?;
50 let max_transition = get_float_opt("max_transition")?;
51 let min_transition = get_float_opt("min_transition")?;
52 let min_period = get_float_opt("min_period")?;
53 let min_pulse_width_high = get_float_opt("min_pulse_width_high")?;
54 let min_pulse_width_low = get_float_opt("min_pulse_width_low")?;
55 let max_input_delta_overdrive_high = get_float_opt("max_input_delta_overdrive_high")?;
56 let max_input_delta_underdrive_high = get_float_opt("max_input_delta_underdrive_high")?;
57
58 let clamp_0_function = get_string_opt("clamp_0_function")?;
59 let clamp_1_function = get_string_opt("clamp_1_function")?;
60 let clamp_latch_function = get_string_opt("clamp_latch_function")?;
61 let clamp_z_function = get_string_opt("clamp_z_function")?;
62 let illegal_clamp_condition = get_string_opt("illegal_clamp_condition")?;
63 let complementary_pin = get_string_opt("complementary_pin")?;
64
65 let clock = get_bool_opt("clock")?;
66 let clock_gate_clock_pin = get_bool_opt("clock_gate_clock_pin")?;
67 let clock_gate_enable_pin = get_bool_opt("clock_gate_enable_pin")?;
68 let clock_gate_test_pin = get_bool_opt("clock_gate_test_pin")?;
69 let clock_gate_obs_pin = get_bool_opt("clock_gate_obs_pin")?;
70 let clock_gate_out_pin = get_bool_opt("clock_gate_out_pin")?;
71 let clock_isolation_cell_clock_pin = get_bool_opt("clock_isolation_cell_clock_pin")?;
72
73 let direction = group.attr_parse_opt("direction", &|| err_context("semantic `direction` attr"))?.unwrap_or(LibPinDirection::Input);
74
75 let driver_type = group.attr_parse_opt("driver_type", &|| err_context("semantic `driver_type` attr"))?;
76
77 let output_signal_level = get_string_opt("output_signal_level")?;
78 let output_signal_level_high = get_float_opt("output_signal_level_high")?;
79 let output_signal_level_low = get_float_opt("output_signal_level_low")?;
80 let output_voltage = get_string_opt("output_voltage")?;
81 let input_signal_level = get_string_opt("input_signal_level")?;
82 let input_voltage = get_string_opt("input_voltage")?;
83
84 let has_builtin_pad = get_bool_opt("has_builtin_pad")?;
85 let hysteresis = get_bool_opt("hysteresis")?;
86 let is_pad = get_bool_opt("is_pad")?;
87 let is_unconnected = get_bool_opt("is_unconnected")?;
88 let multicell_pad_pin = get_bool_opt("multicell_pad_pin")?;
89 let inverted_output = get_bool_opt("inverted_output")?;
90
91 let connection_class = get_string_opt("connection_class")?;
92 let fault_model = get_string_opt("fault_model")?;
93 let dont_fault = group.attr_parse_opt("dont_fault", &|| err_context("semantic `dont_fault` attr"))?;
94 let internal_node = get_ident_opt("internal_node")?;
95 let input_map = get_string_opt("input_map")?;
96
97 let function = get_expr_opt("function")?;
98
99 let pin_func_type = group.attr_parse_opt("pin_func_type", &|| err_context("semantic `pin_func_type` attr"))?;
101 let primary_output = get_bool_opt("primary_output")?;
102 let pulling_current = get_float_opt("pulling_current")?;
103 let pulling_resistance = get_float_opt("pulling_resistance")?;
104 let restore_action = group.attr_parse_opt("restore_action", &|| err_context("semantic `restore_action` attr"))?;
105 let restore_edge_type = group.attr_parse_opt("restore_edge_type", &|| err_context("semantic `restore_edge_type` attr"))?;
106
107 let rise_capacitance = get_float_opt("rise_capacitance")?;
109 let rise_current_slope_after_threshold = get_float_opt("rise_current_slope_after_threshold")?;
110 let rise_current_slope_before_threshold = get_float_opt("rise_current_slope_before_threshold")?;
111 let rise_time_after_threshold = get_float_opt("rise_time_after_threshold")?;
112 let rise_time_before_threshold = get_float_opt("rise_time_before_threshold")?;
113
114 let save_action = group.attr_parse_opt("save_action", &|| err_context("semantic `save_action` attr"))?;
115 let signal_type = group.attr_parse_opt("signal_type", &|| err_context("semantic `signal_type` attr"))?;
116 let slew_control = group.attr_parse_opt("slew_control", &|| err_context("semantic `slew_control` attr"))?;
117
118 let state_function = get_expr_opt("state_function")?;
120 let test_output_only = get_bool_opt("test_output_only")?;
121 let three_state = get_expr_opt("three_state")?;
122 let x_function = get_expr_opt("x_function")?;
123
124 let fall_capacitance_range = group.attr_floats_opt("fall_capacitance_range", &|| err_context("semantic `fall_capacitance_range` attr"))?;
125 let rise_capacitance_range = group.attr_floats_opt("rise_capacitance_range", &|| err_context("semantic `rise_capacitance_range` attr"))?;
126
127 Ok(Self {
128 name,
129
130 alive_during_power_up,
131 always_on,
132
133 antenna_diode_type,
134 antenna_diode_related_ground_pins,
135 antenna_diode_related_power_pins,
136
137 bit_width,
138
139 capacitance,
140 fall_capacitance,
141 fanout_load,
142 drive_current,
143 fall_current_slope_after_threshold,
144 fall_current_slope_before_threshold,
145 fall_time_after_threshold,
146 fall_time_before_threshold,
147
148 max_capacitance,
149 min_capacitance,
150 max_fanout,
151 min_fanout,
152 max_transition,
153 min_transition,
154 min_period,
155 min_pulse_width_high,
156 min_pulse_width_low,
157 max_input_delta_overdrive_high,
158 max_input_delta_underdrive_high,
159
160 clamp_0_function,
161 clamp_1_function,
162 clamp_latch_function,
163 clamp_z_function,
164 illegal_clamp_condition,
165 complementary_pin,
166 nextstate_type,
167
168 clock,
169 clock_gate_clock_pin,
170 clock_gate_enable_pin,
171 clock_gate_test_pin,
172 clock_gate_obs_pin,
173 clock_gate_out_pin,
174 clock_isolation_cell_clock_pin,
175
176 direction,
177 driver_type,
178 output_signal_level,
179 output_signal_level_high,
180 output_signal_level_low,
181 output_voltage,
182 input_signal_level,
183 input_voltage,
184
185 has_builtin_pad,
186 hysteresis,
187 is_pad,
188 is_unconnected,
189 multicell_pad_pin,
190 inverted_output,
191
192 connection_class,
193 fault_model,
194 dont_fault,
195 internal_node,
196 input_map,
197
198 function,
199
200 pin_func_type,
201 primary_output,
202 pulling_current,
203 pulling_resistance,
204 restore_action,
205 restore_edge_type,
206 rise_capacitance,
207 rise_current_slope_after_threshold,
208 rise_current_slope_before_threshold,
209 rise_time_after_threshold,
210 rise_time_before_threshold,
211 save_action,
212 signal_type,
213 slew_control,
214 state_function,
215 test_output_only,
216 three_state,
217 x_function,
218
219 fall_capacitance_range,
220 rise_capacitance_range,
221
222 attrs: group.attrs,
223 groups: group.groups,
224 })
225 }
226}
227
228#[cfg(test)]
229mod test {
230 use super::*;
231
232 #[test]
233 fn test_pin_into_group() {
234 let mut pin = LibPin::new("my_pin");
235 pin.groups.push(LibGroup::new("gtype", vec!["gname".to_string()]));
236 }
241
242 #[test]
243 fn test_pin_from_group() {
244 let mut group = LibGroup::new("pin", vec!["a".to_string()]);
245 group.groups.push(LibGroup::new("gtype", vec!["gname".to_string()]));
246 let pin = LibPin::from_group(group).unwrap();
247 assert_eq!(pin.name, "a");
248 assert_eq!(pin.groups.len(), 1);
249 }
250}