epics-base-rs 0.18.2

Pure Rust EPICS IOC core — record system, database, iocsh, calc engine
Documentation
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
use crate::error::{CaError, CaResult};
use crate::server::record::{FieldDesc, ProcessOutcome, Record};
use crate::types::{DbFieldType, EpicsValue};

// printf record (EPICS 7).
// Evaluates FMT as a printf format string with up to 10 inputs (INP0-INP9, values A-J).
// Each format specifier in FMT consumes the next input in order. A `*`
// (variable width/precision) also consumes one input. VAL holds the
// resulting string (capped by SIZV).
//
// C `printfRecord.c::doPrintf` reads each input link with the DBR type
// implied by the conversion + length modifier: `%s`/`%ls` read a
// string, the numeric conversions read a number. The Rust framework
// pre-fetches every INPn link into the matching value field A..J; we
// therefore store the raw `EpicsValue` per slot so a `%s` conversion
// can recover the string content (H-6) instead of stringifying a
// coerced f64.
pub struct PrintfRecord {
    pub val: String,
    pub sizv: u16,
    pub fmt: String,
    /// INP0-INP9: input link strings.
    pub inp_links: [String; 10],
    /// A-J: current values from the input links, kept in their native
    /// type. `%s` reads the string form, numeric conversions read the
    /// numeric form.
    pub vals: [EpicsValue; 10],
}

impl Default for PrintfRecord {
    fn default() -> Self {
        Self {
            val: String::new(),
            sizv: 256,
            fmt: String::new(),
            inp_links: Default::default(),
            vals: std::array::from_fn(|_| EpicsValue::Double(0.0)),
        }
    }
}

/// One parsed printf conversion directive.
struct Directive {
    /// Width; `None` when given as `*` (read from the next input).
    width: Option<usize>,
    star_width: bool,
    /// Precision; `None` when absent, `Some(*)` flag handled separately.
    precision: Option<usize>,
    star_prec: bool,
    left_align: bool,
    zero_pad: bool,
    alt_form: bool, // '#'
    /// Final conversion character.
    conv: u8,
    /// `l`/`ll` long modifier present (selects `%ls` long string).
    long: bool,
    bad: bool,
}

impl PrintfRecord {
    /// Stringify an input value for the `%s` / `%ls` conversion.
    /// Mirrors C reading the link as `DBR_STRING` / `DBR_CHAR`.
    fn val_as_string(v: &EpicsValue) -> String {
        match v {
            EpicsValue::String(s) => s.clone(),
            EpicsValue::CharArray(bytes) => {
                let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
                String::from_utf8_lossy(&bytes[..end]).into_owned()
            }
            EpicsValue::Double(d) => format!("{d}"),
            EpicsValue::Float(f) => format!("{f}"),
            EpicsValue::Long(n) => format!("{n}"),
            EpicsValue::Short(n) => format!("{n}"),
            EpicsValue::Int64(n) => format!("{n}"),
            EpicsValue::Char(c) => format!("{c}"),
            EpicsValue::Enum(e) => format!("{e}"),
            other => format!("{other:?}"),
        }
    }

    fn val_as_f64(v: &EpicsValue) -> f64 {
        v.to_f64().unwrap_or(0.0)
    }

    /// Parse one `%...` directive starting at `bytes[i]` (which is `%`).
    /// Returns the directive and the index just past the conversion
    /// char. Consumes `*` width/precision values from `star_idx`.
    fn parse_directive(bytes: &[u8], mut i: usize) -> (Directive, usize) {
        let mut d = Directive {
            width: None,
            star_width: false,
            precision: None,
            star_prec: false,
            left_align: false,
            zero_pad: false,
            alt_form: false,
            conv: b's',
            long: false,
            bad: false,
        };
        i += 1; // skip '%'
        // Flags.
        loop {
            match bytes.get(i) {
                Some(b'-') => d.left_align = true,
                Some(b'+') | Some(b' ') => {}
                Some(b'#') => d.alt_form = true,
                Some(b'0') => d.zero_pad = true,
                _ => break,
            }
            i += 1;
        }
        // Width.
        if bytes.get(i) == Some(&b'*') {
            d.star_width = true;
            i += 1;
        } else {
            let mut w = 0usize;
            let mut any = false;
            while let Some(c) = bytes.get(i) {
                if c.is_ascii_digit() {
                    w = w * 10 + (c - b'0') as usize;
                    any = true;
                    i += 1;
                } else {
                    break;
                }
            }
            if any {
                d.width = Some(w);
            }
        }
        // Precision.
        if bytes.get(i) == Some(&b'.') {
            i += 1;
            if bytes.get(i) == Some(&b'*') {
                d.star_prec = true;
                i += 1;
            } else {
                let mut p = 0usize;
                while let Some(c) = bytes.get(i) {
                    if c.is_ascii_digit() {
                        p = p * 10 + (c - b'0') as usize;
                        i += 1;
                    } else {
                        break;
                    }
                }
                d.precision = Some(p);
            }
        }
        // Length modifiers: h, hh, l, ll.
        loop {
            match bytes.get(i) {
                Some(b'h') => {
                    i += 1;
                }
                Some(b'l') => {
                    d.long = true;
                    i += 1;
                }
                _ => break,
            }
        }
        // Conversion character.
        match bytes.get(i) {
            Some(&c) if b"diouxXeEfFgGcs".contains(&c) => {
                d.conv = c;
                i += 1;
            }
            Some(_) => {
                d.bad = true;
                i += 1;
            }
            None => {
                d.bad = true;
            }
        }
        (d, i)
    }

    fn apply_fmt(&self) -> String {
        let mut result = String::new();
        let bytes = self.fmt.as_bytes();
        let mut i = 0;
        let mut inp_idx = 0usize;

        // Fetch the next input value, advancing the cursor.
        let take = |idx: &mut usize| -> Option<&EpicsValue> {
            if *idx < 10 {
                let v = &self.vals[*idx];
                *idx += 1;
                Some(v)
            } else {
                *idx += 1;
                None
            }
        };

        while i < bytes.len() {
            if bytes[i] != b'%' {
                result.push(bytes[i] as char);
                i += 1;
                continue;
            }
            // %% escape.
            if bytes.get(i + 1) == Some(&b'%') {
                result.push('%');
                i += 2;
                continue;
            }
            let (d, next) = Self::parse_directive(bytes, i);
            i = next;
            if d.bad {
                // Bad format directive — C copies the directive text.
                continue;
            }

            // Resolve `*` width / precision from the next input(s).
            let width = if d.star_width {
                take(&mut inp_idx)
                    .map(|v| Self::val_as_f64(v) as i64)
                    .unwrap_or(0)
            } else {
                d.width.unwrap_or(0) as i64
            };
            let precision = if d.star_prec {
                take(&mut inp_idx)
                    .map(|v| Self::val_as_f64(v) as i64)
                    .unwrap_or(0)
                    .max(0) as usize
            } else {
                d.precision.unwrap_or(usize::MAX)
            };
            let (width, left_align) = if width < 0 {
                ((-width) as usize, true)
            } else {
                (width as usize, d.left_align)
            };

            let arg = take(&mut inp_idx);
            let conv_prec = if precision == usize::MAX {
                6
            } else {
                precision
            };

            let substituted = match d.conv {
                b'd' | b'i' => {
                    let v = arg.map(Self::val_as_f64).unwrap_or(0.0);
                    pad_string(format!("{}", v as i64), width, left_align, d.zero_pad)
                }
                b'u' => {
                    let v = arg.map(Self::val_as_f64).unwrap_or(0.0);
                    pad_string(
                        format!("{}", v as i64 as u64),
                        width,
                        left_align,
                        d.zero_pad,
                    )
                }
                b'o' => {
                    let v = arg.map(Self::val_as_f64).unwrap_or(0.0) as i64 as u64;
                    let s = if d.alt_form && v != 0 {
                        format!("0{v:o}")
                    } else {
                        format!("{v:o}")
                    };
                    pad_string(s, width, left_align, d.zero_pad)
                }
                b'x' => {
                    let v = arg.map(Self::val_as_f64).unwrap_or(0.0) as i64 as u64;
                    let s = if d.alt_form && v != 0 {
                        format!("0x{v:x}")
                    } else {
                        format!("{v:x}")
                    };
                    pad_string(s, width, left_align, d.zero_pad)
                }
                b'X' => {
                    let v = arg.map(Self::val_as_f64).unwrap_or(0.0) as i64 as u64;
                    let s = if d.alt_form && v != 0 {
                        format!("0X{v:X}")
                    } else {
                        format!("{v:X}")
                    };
                    pad_string(s, width, left_align, d.zero_pad)
                }
                b'e' | b'E' | b'f' | b'F' | b'g' | b'G' => {
                    let v = arg.map(Self::val_as_f64).unwrap_or(0.0);
                    let s = format_float_conv(d.conv, v, conv_prec, d.alt_form);
                    pad_string(s, width, left_align, d.zero_pad)
                }
                b'c' => {
                    // %c: the input value as a single character (C reads
                    // DBR_CHAR). Numeric value → its code point.
                    let ch = match arg {
                        Some(EpicsValue::String(s)) => s.chars().next().unwrap_or(' '),
                        Some(v) => {
                            let code = Self::val_as_f64(v) as u32;
                            char::from_u32(code).unwrap_or('\u{0}')
                        }
                        None => '\u{0}',
                    };
                    pad_string(ch.to_string(), width, left_align, false)
                }
                b's' => {
                    // %s / %ls: print the link's STRING content (H-6).
                    let mut s = arg.map(Self::val_as_string).unwrap_or_default();
                    // Precision caps the number of characters printed.
                    if precision != usize::MAX && s.chars().count() > precision {
                        s = s.chars().take(precision).collect();
                    }
                    pad_string(s, width, left_align, false)
                }
                _ => String::new(),
            };
            result.push_str(&substituted);
        }

        let max = (self.sizv as usize).saturating_sub(1);
        if result.len() > max {
            // Truncate on a UTF-8 boundary.
            let trunc = (0..=max)
                .rev()
                .find(|&n| result.is_char_boundary(n))
                .unwrap_or(0);
            result.truncate(trunc);
        }
        result
    }

    fn inp_index(name: &str) -> Option<usize> {
        // INP0-INP9
        let bytes = name.as_bytes();
        if bytes.len() == 4 && &bytes[0..3] == b"INP" {
            let digit = bytes[3];
            if digit.is_ascii_digit() {
                return Some((digit - b'0') as usize);
            }
        }
        None
    }

    fn val_index(name: &str) -> Option<usize> {
        // A-J
        if name.len() == 1 {
            let c = name.as_bytes()[0];
            if (b'A'..=b'J').contains(&c) {
                return Some((c - b'A') as usize);
            }
        }
        None
    }
}

// Apply width padding. Handles left-align, right-align, and zero-fill.
// For zero-fill on signed values the sign is placed before the zeros.
fn pad_string(s: String, width: usize, left_align: bool, zero_pad: bool) -> String {
    if width <= s.chars().count() {
        return s;
    }
    if left_align {
        format!("{s:<width$}")
    } else if zero_pad {
        if s.starts_with('-') || s.starts_with('+') {
            format!("{}{:0>width$}", &s[..1], &s[1..], width = width - 1)
        } else {
            format!("{s:0>width$}")
        }
    } else {
        format!("{s:>width$}")
    }
}

fn format_float_conv(conv: u8, val: f64, prec: usize, alt_form: bool) -> String {
    match conv {
        b'e' => format!("{val:.prec$e}"),
        b'E' => format!("{val:.prec$E}"),
        b'f' | b'F' => format!("{val:.prec$}"),
        b'g' => format_g_val(val, prec, false, alt_form),
        b'G' => format_g_val(val, prec, true, alt_form),
        _ => format!("{val:.prec$}"),
    }
}

fn format_g_val(val: f64, prec: usize, upper: bool, alt_form: bool) -> String {
    if val == 0.0 {
        // libc `%g` of 0.0 is "0"; `%#g` keeps trailing zeros.
        if alt_form {
            let p = if prec == 0 { 1 } else { prec };
            let decimals = p.saturating_sub(1);
            return format!("{:.*}", decimals, 0.0);
        }
        return "0".to_string();
    }
    let p = if prec == 0 { 1 } else { prec };
    let exp = val.abs().log10().floor() as i32;

    if exp < -4 || exp >= p as i32 {
        let sig_prec = p.saturating_sub(1);
        let raw = if upper {
            format!("{val:.sig_prec$E}")
        } else {
            format!("{val:.sig_prec$e}")
        };
        if alt_form {
            raw
        } else {
            strip_trailing_zeros_sci(&raw, upper)
        }
    } else {
        let decimal_places = (p as i32 - 1 - exp).max(0) as usize;
        let raw = format!("{val:.decimal_places$}");
        if alt_form {
            raw
        } else if raw.contains('.') {
            raw.trim_end_matches('0').trim_end_matches('.').to_string()
        } else {
            raw
        }
    }
}

fn strip_trailing_zeros_sci(s: &str, upper: bool) -> String {
    let sep = if upper { 'E' } else { 'e' };
    if let Some(pos) = s.find(sep) {
        let mantissa = &s[..pos];
        let exp_part = &s[pos..];
        let trimmed = if mantissa.contains('.') {
            mantissa.trim_end_matches('0').trim_end_matches('.')
        } else {
            mantissa
        };
        format!("{trimmed}{exp_part}")
    } else {
        s.to_string()
    }
}

static PRINTF_FIELDS: &[FieldDesc] = &[
    FieldDesc {
        name: "VAL",
        dbf_type: DbFieldType::Char,
        read_only: true,
    },
    FieldDesc {
        name: "SIZV",
        dbf_type: DbFieldType::Short,
        read_only: false,
    },
    FieldDesc {
        name: "FMT",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP0",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP1",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP2",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP3",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP4",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP5",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP6",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP7",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP8",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "INP9",
        dbf_type: DbFieldType::String,
        read_only: false,
    },
    FieldDesc {
        name: "A",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
    FieldDesc {
        name: "B",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
    FieldDesc {
        name: "C",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
    FieldDesc {
        name: "D",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
    FieldDesc {
        name: "E",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
    FieldDesc {
        name: "F",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
    FieldDesc {
        name: "G",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
    FieldDesc {
        name: "H",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
    FieldDesc {
        name: "I",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
    FieldDesc {
        name: "J",
        dbf_type: DbFieldType::Double,
        read_only: false,
    },
];

impl Record for PrintfRecord {
    fn record_type(&self) -> &'static str {
        "printf"
    }

    fn field_list(&self) -> &'static [FieldDesc] {
        PRINTF_FIELDS
    }

    fn uses_monitor_deadband(&self) -> bool {
        false
    }

    fn process(&mut self) -> CaResult<ProcessOutcome> {
        self.val = self.apply_fmt();
        Ok(ProcessOutcome::complete())
    }

    fn val(&self) -> Option<EpicsValue> {
        Some(EpicsValue::CharArray(self.val.as_bytes().to_vec()))
    }

    fn get_field(&self, name: &str) -> Option<EpicsValue> {
        match name {
            "VAL" => Some(EpicsValue::CharArray(self.val.as_bytes().to_vec())),
            "SIZV" => Some(EpicsValue::Short(self.sizv as i16)),
            "FMT" => Some(EpicsValue::String(self.fmt.clone())),
            _ => {
                if let Some(idx) = Self::inp_index(name) {
                    return Some(EpicsValue::String(self.inp_links[idx].clone()));
                }
                if let Some(idx) = Self::val_index(name) {
                    return Some(self.vals[idx].clone());
                }
                None
            }
        }
    }

    fn put_field(&mut self, name: &str, value: EpicsValue) -> CaResult<()> {
        match name {
            "SIZV" => {
                if let EpicsValue::Short(v) = value {
                    // C `init_record`: SIZV clamps to [16, 0x7fff].
                    let v = (v as i32).clamp(16, 0x7fff);
                    self.sizv = v as u16;
                }
            }
            "FMT" => {
                if let EpicsValue::String(s) = value {
                    self.fmt = s;
                } else {
                    return Err(CaError::TypeMismatch("FMT".into()));
                }
            }
            _ => {
                if let Some(idx) = Self::inp_index(name) {
                    if let EpicsValue::String(s) = value {
                        self.inp_links[idx] = s;
                    } else {
                        return Err(CaError::TypeMismatch(name.into()));
                    }
                } else if let Some(idx) = Self::val_index(name) {
                    // Store the raw value so `%s` can recover the
                    // string form of a string-typed input link.
                    self.vals[idx] = value;
                } else {
                    return Err(CaError::FieldNotFound(name.to_string()));
                }
            }
        }
        Ok(())
    }

    fn multi_input_links(&self) -> &[(&'static str, &'static str)] {
        &[
            ("INP0", "A"),
            ("INP1", "B"),
            ("INP2", "C"),
            ("INP3", "D"),
            ("INP4", "E"),
            ("INP5", "F"),
            ("INP6", "G"),
            ("INP7", "H"),
            ("INP8", "I"),
            ("INP9", "J"),
        ]
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn rec_with(fmt: &str) -> PrintfRecord {
        PrintfRecord {
            fmt: fmt.to_string(),
            ..Default::default()
        }
    }

    /// H-6: `%s` prints the input link's STRING content, not a number.
    #[test]
    fn percent_s_formats_string_input() {
        let mut rec = rec_with("name=%s");
        rec.vals[0] = EpicsValue::String("motor1".into());
        rec.process().unwrap();
        assert_eq!(rec.val, "name=motor1");
    }

    /// H-6: a string input survives even when other slots are numeric.
    #[test]
    fn percent_s_with_width_padding() {
        let mut rec = rec_with("[%8s]");
        rec.vals[0] = EpicsValue::String("ab".into());
        rec.process().unwrap();
        assert_eq!(rec.val, "[      ab]");
    }

    /// H-7: `%*d` reads the field width from the next input link.
    #[test]
    fn star_width_consumes_an_input() {
        let mut rec = rec_with("%*d");
        rec.vals[0] = EpicsValue::Long(6); // width
        rec.vals[1] = EpicsValue::Long(42); // value
        rec.process().unwrap();
        assert_eq!(rec.val, "    42");
    }

    /// H-7: `%ld` long modifier is consumed, not emitted literally.
    #[test]
    fn long_modifier_consumed() {
        let mut rec = rec_with("%ld");
        rec.vals[0] = EpicsValue::Long(99);
        rec.process().unwrap();
        assert_eq!(rec.val, "99");
    }

    /// H-7: `%ls` long-string conversion prints the string input.
    #[test]
    fn long_string_conversion() {
        let mut rec = rec_with("%ls");
        rec.vals[0] = EpicsValue::String("hello".into());
        rec.process().unwrap();
        assert_eq!(rec.val, "hello");
    }

    /// H-7: `%c` prints a single character.
    #[test]
    fn percent_c_formats_char() {
        let mut rec = rec_with("%c");
        rec.vals[0] = EpicsValue::Long(65); // 'A'
        rec.process().unwrap();
        assert_eq!(rec.val, "A");
    }

    /// L-2: `%#g` of zero keeps trailing zeros; plain `%g` is "0".
    #[test]
    fn g_zero_alt_form() {
        let mut rec = rec_with("%g");
        rec.vals[0] = EpicsValue::Double(0.0);
        rec.process().unwrap();
        assert_eq!(rec.val, "0");

        let mut rec = rec_with("%#.3g");
        rec.vals[0] = EpicsValue::Double(0.0);
        rec.process().unwrap();
        assert_eq!(rec.val, "0.00");
    }

    /// `%%` escapes a literal percent.
    #[test]
    fn percent_escape() {
        let mut rec = rec_with("100%%");
        rec.process().unwrap();
        assert_eq!(rec.val, "100%");
    }
}