epics-base-rs 0.29.0

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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
use std::path::Path;

use crate::types::EpicsValue;
use chrono::Local;

use super::error::{AutosaveError, AutosaveResult};
use super::format::{ARRAY_MARKER, CompatMode, END_MARKER, VERSION};

/// A single PV entry in a .sav file.
#[derive(Debug, Clone)]
pub struct SaveEntry {
    pub pv_name: String,
    pub value: String,
    pub connected: bool,
}

/// A line of a `.sav` file that declares no entry this grammar
/// understands — a truncated write, a hand edit, a line whose
/// `@array@` braces do not close.
///
/// Kept rather than dropped: a restore that silently skips such a line
/// reports the PVs it did write and nothing else, so a partially
/// written file looks like a clean restore.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MalformedLine {
    /// 1-based line number within the file.
    pub line_no: usize,
    /// The line as it appeared, minus its terminator.
    pub text: String,
}

/// Everything a `.sav` file body says: the entries it declares, and the
/// lines it could not turn into one. Every reader gets both halves —
/// there is no entries-only parse that can lose the second.
#[derive(Debug, Clone, Default)]
pub struct SaveFileContents {
    pub entries: Vec<SaveEntry>,
    pub malformed: Vec<MalformedLine>,
    /// How many PVs the file itself declares it could not save, from its
    /// `!` line; 0 when it has none.
    ///
    /// C writes `! <n> channel(s) not connected - or not all gets were
    /// successful` under the header whenever a set saved with unreachable
    /// members (`save_restore.c` `write_it`), and its restore reads that
    /// line back — `dbrestore.c:994-1010` logs it, sets the set's status
    /// to WARN, and with `save_restoreIncompleteSetsOk` cleared refuses
    /// the restore outright. A file that omits the line tells a C IOC the
    /// save set was complete, so this is not decoration.
    pub not_connected: usize,
}

/// Write a .sav file atomically (tmp -> fsync -> rename).
///
/// Uses the autosave-rs native format ([`CompatMode::Native`]). For a
/// file a C IOC must be able to read, use [`write_save_file_with_mode`]
/// with [`CompatMode::CRead`].
pub async fn write_save_file(path: &Path, entries: &[SaveEntry]) -> AutosaveResult<()> {
    write_save_file_with_mode(path, entries, CompatMode::Native).await
}

/// Write a .sav file atomically (tmp -> fsync -> rename) in the given
/// [`CompatMode`].
///
/// In [`CompatMode::CRead`] the header banner is `save/restore` (the
/// banner a C IOC's `restore.c` / `asVerify` expects) instead of the
/// autosave-rs banner. The per-PV line format (`PVNAME value`,
/// arrays as `PVNAME @array@ { ... }`) is shared by both modes — the
/// `SaveEntry.value` text is already mode-encoded by the caller via
/// [`value_to_save_str`] / [`value_to_save_str_c`]. A C IOC can read
/// a `CRead`-written file because the line grammar and `<END>` marker
/// match C autosave's `dbrestore.c` parser.
pub async fn write_save_file_with_mode(
    path: &Path,
    entries: &[SaveEntry],
    mode: CompatMode,
) -> AutosaveResult<()> {
    let mut content = String::new();

    // Header. C autosave writes `# <banner>\t<datetime>` where the
    // banner begins with `save/restore` — restore.c only checks for a
    // leading `#` comment and skips it, but asVerify and operators
    // expect the canonical banner.
    let now = Local::now();
    let banner = match mode {
        CompatMode::Native => VERSION,
        CompatMode::CRead => "save/restore V1.7",
    };
    content.push_str(&format!(
        "# {}\t{}\n",
        banner,
        now.format("%Y-%m-%d %H:%M:%S")
    ));

    // C's incomplete-set declaration, in C's exact wording because a C
    // IOC parses it: `dbrestore.c` takes the count from the text after
    // the `!` and refuses the whole restore when the site has cleared
    // `save_restoreIncompleteSetsOk`. Written in both modes — this is
    // the file format, not the value encoding that `CompatMode` selects.
    let not_connected = entries.iter().filter(|e| !e.connected).count();
    if not_connected > 0 {
        content.push_str(&format!(
            "! {not_connected} channel(s) not connected - or not all gets were successful\n"
        ));
    }

    for entry in entries {
        if entry.connected {
            content.push_str(&entry.pv_name);
            content.push(' ');
            content.push_str(&entry.value);
            content.push('\n');
        } else {
            content.push_str(&format!("#{}\t(not connected)\n", entry.pv_name));
        }
    }

    content.push_str(END_MARKER);
    content.push('\n');

    // Atomic write: open RDWR → write → fsync the SAME fd → rename
    // → fsync parent dir. The previous sequence reopened RDONLY
    // before fsync; POSIX is silent on whether sync_all on a RDONLY
    // fd flushes data, and on some FS (older NFS, FUSE) it's a
    // no-op — silently dropping the write across a power loss.
    //
    // The whole sequence runs in ONE `runtime::fs::blocking` closure rather
    // than four awaits: the ordering above is the durability guarantee, and
    // keeping it in one closure keeps it readable as a sequence and costs one
    // hop through the blocking pool instead of four. It also has to leave
    // `tokio::fs` behind — that is a blocking call dressed as an async one and
    // it panics on any thread that is not a tokio runtime thread, which is
    // every callback thread under `exec_backend`.
    let tmp_path = path.with_extension("tmp");
    let final_path = path.to_path_buf();
    let parent = path.parent().map(|p| p.to_path_buf());
    crate::runtime::fs::blocking(move || {
        use std::io::Write as _;
        {
            let mut file = std::fs::OpenOptions::new()
                .create(true)
                .truncate(true)
                .write(true)
                .open(&tmp_path)?;
            file.write_all(content.as_bytes())?;
            file.sync_all()?;
        }
        std::fs::rename(&tmp_path, &final_path)?;
        // Sync parent directory to make the rename durable across power loss
        if let Some(parent) = parent
            && let Ok(dir) = std::fs::File::open(parent)
        {
            let _ = dir.sync_all();
        }
        Ok(())
    })
    .await?;

    Ok(())
}

/// Read a .sav file and validate `<END>` marker.
/// Returns None for corrupt files (no END marker).
pub async fn read_save_file(path: &Path) -> AutosaveResult<Option<SaveFileContents>> {
    let read = read_partial_save_file(path).await?;
    Ok(read.complete.then_some(read.contents))
}

/// A `.sav` body and whether the file that carried it was complete.
#[derive(Debug, Clone, Default)]
pub struct PartialSaveFile {
    pub contents: SaveFileContents,
    /// `false` when the file does not end in `<END>`, i.e. it was
    /// truncated or is still being written.
    pub complete: bool,
}

/// Parse a `.sav` body whether or not the file is complete, and say
/// which it was.
///
/// [`read_save_file`] is this with the incomplete case turned into
/// `None`, which is what restore needs: a truncated file must not be
/// pushed into the database as if it were the whole save set. `asVerify`
/// deliberately reads what restore refuses — C's `do_asVerify_fp`
/// (`verify.c` at `R6-0-20-g186f467`) warns that it cannot find the
/// `<END>` marker and then compares every PV line in the file anyway,
/// because which PVs a truncated file does hold is exactly what the
/// operator looking at it needs to know.
///
/// One parse and one marker test behind both, so the two views cannot
/// disagree about what a file contains.
pub async fn read_partial_save_file(path: &Path) -> AutosaveResult<PartialSaveFile> {
    let content = crate::runtime::fs::read_to_string(path)
        .await
        .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                e.into()
            } else {
                AutosaveError::CorruptSaveFile {
                    path: path.display().to_string(),
                    message: e.to_string(),
                }
            }
        })?;

    Ok(PartialSaveFile {
        complete: has_end_marker(&content),
        contents: parse_save_content(&content),
    })
}

/// Quick check if a .sav file has a valid `<END>` marker.
pub async fn validate_save_file(path: &Path) -> AutosaveResult<bool> {
    let content = crate::runtime::fs::read_to_string(path).await?;
    Ok(has_end_marker(&content))
}

fn has_end_marker(content: &str) -> bool {
    for line in content.lines().rev() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        return trimmed == END_MARKER;
    }
    false
}

fn parse_save_content(content: &str) -> SaveFileContents {
    let mut out = SaveFileContents::default();

    for (idx, raw) in content.lines().enumerate() {
        // Leading indentation is framing; a TRAILING space is not. The
        // writer emits `PVNAME<space>VALUE`, so the value of a PV whose
        // value is the empty string is the empty tail after that space —
        // trimming the line first is what used to erase the separator and
        // drop the entry.
        let line = raw.trim_start();
        let framing = line.trim_end();
        if framing.is_empty() {
            continue;
        }
        if framing == END_MARKER {
            break;
        }
        let malformed = |out: &mut SaveFileContents| {
            out.malformed.push(MalformedLine {
                line_no: idx + 1,
                text: raw.to_string(),
            });
        };

        // The file's own count of what it could not save. C emits it as
        // `! <n> channel(s) not connected - ...` and reads the number
        // back with `atol` on the text after the `!`, so a leading `!`
        // never names a PV — parsing it as one produced an entry called
        // `!` that the restore then looked for in the database.
        if let Some(rest) = framing.strip_prefix('!') {
            let digits: String = rest
                .trim_start()
                .chars()
                .take_while(char::is_ascii_digit)
                .collect();
            out.not_connected = digits.parse().unwrap_or(0);
            continue;
        }

        // Header/comment lines
        if framing.starts_with('#') {
            // A PV that had no value to save. `(not connected)` is what
            // this writer emits; `Search Issued` / `Search Failed` are
            // what C's `write_it` leaves in `pchannel->value` for a
            // channel that never connected, and C's own `asVerify` counts
            // the first of them. Matching both is what lets a C-written
            // file's unsaved PVs stay visible here instead of passing as
            // ordinary comments.
            let inner = &framing[1..];
            if inner.contains("(not connected)")
                || inner.contains("Search Issued")
                || inner.contains("Search Failed")
            {
                let pv_name = inner.split(['\t', ' ']).next().unwrap_or("").trim();
                if !pv_name.is_empty() {
                    out.entries.push(SaveEntry {
                        pv_name: pv_name.to_string(),
                        value: String::new(),
                        connected: false,
                    });
                }
            }
            continue;
        }

        // C autosave @array@ format. The marker settles what the line is,
        // so a line carrying it either parses as an array or is malformed —
        // falling through to the scalar rule would turn `PV @array@ { 1 2`
        // into a PV whose value is the literal text `@array@ { 1 2`.
        if framing.contains(ARRAY_MARKER) {
            match parse_c_array_line(framing) {
                Some(entry) => out.entries.push(entry),
                None => malformed(&mut out),
            }
            continue;
        }

        // Normal line: PV_NAME<space>VALUE
        match line.split_once(' ') {
            Some((pv_name, value)) if !pv_name.is_empty() => out.entries.push(SaveEntry {
                pv_name: pv_name.to_string(),
                value: value.to_string(),
                connected: true,
            }),
            _ => malformed(&mut out),
        }
    }

    out
}

/// The native array text `[e1,e2,e3]` — the one owner of that form, in
/// both directions.
///
/// An element is written verbatim unless it would be ambiguous against
/// the punctuation of the form itself — it contains `,`, `]`, `"` or
/// `\`, has surrounding whitespace, or is empty — in which case it is
/// double-quoted with `"` and `\` backslash-escaped. Numbers never
/// qualify, so numeric arrays keep the text they always had.
/// [`decode_array_text`] undoes exactly this, so any element survives
/// the round trip. Nothing else in this module may join or split array
/// text: an element carrying a separator is precisely the case an
/// ad-hoc `join(",")` / `split(',')` pair loses.
fn encode_array_text<I, S>(elements: I) -> String
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let mut out = String::from("[");
    for (i, elem) in elements.into_iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        let elem = elem.as_ref();
        let ambiguous = elem.is_empty()
            || elem.contains([',', ']', '"', '\\'])
            || elem.starts_with(char::is_whitespace)
            || elem.ends_with(char::is_whitespace);
        if ambiguous {
            out.push('"');
            for c in elem.chars() {
                if c == '"' || c == '\\' {
                    out.push('\\');
                }
                out.push(c);
            }
            out.push('"');
        } else {
            out.push_str(elem);
        }
    }
    out.push(']');
    out
}

/// Read back the text [`encode_array_text`] writes, unquoting and
/// unescaping the elements that were quoted. Unquoted elements keep the
/// old tolerance for surrounding whitespace, so hand-written `[1, 2]`
/// still reads as two elements.
fn decode_array_text(s: &str) -> Vec<String> {
    let inner = s.strip_prefix('[').unwrap_or(s);
    // `strip_suffix`, not `trim_end_matches`: only the form's own
    // closing bracket comes off, so `]` inside a quoted element cannot
    // be eaten from the end of the text.
    let inner = inner.strip_suffix(']').unwrap_or(inner);

    let mut out = Vec::new();
    let mut chars = inner.chars().peekable();
    loop {
        while chars.peek().is_some_and(|c| c.is_whitespace()) {
            chars.next();
        }
        match chars.peek() {
            None if out.is_empty() => break,
            // A trailing separator still declares a final empty element.
            None => {
                out.push(String::new());
                break;
            }
            Some('"') => {
                chars.next();
                let mut elem = String::new();
                loop {
                    match chars.next() {
                        Some('\\') => {
                            if let Some(c) = chars.next() {
                                elem.push(c);
                            }
                        }
                        Some('"') | None => break,
                        Some(c) => elem.push(c),
                    }
                }
                out.push(elem);
                // Skip to the separator that follows the closing quote.
                while chars.peek().is_some_and(|&c| c != ',') {
                    chars.next();
                }
            }
            Some(_) => {
                let mut elem = String::new();
                while chars.peek().is_some_and(|&c| c != ',') {
                    elem.push(chars.next().unwrap());
                }
                out.push(elem.trim_end().to_string());
            }
        }
        match chars.next() {
            Some(',') => continue,
            _ => break,
        }
    }
    out
}

/// Parse a C autosave @array@ line.
fn parse_c_array_line(line: &str) -> Option<SaveEntry> {
    // Format: PV_NAME @array@ { "e1" "e2" "e3" }
    let marker_pos = line.find(ARRAY_MARKER)?;
    let pv_name = line[..marker_pos].trim();
    let rest = line[marker_pos + ARRAY_MARKER.len()..].trim();

    if !rest.starts_with('{') || !rest.ends_with('}') {
        return None;
    }

    let inner = rest[1..rest.len() - 1].trim();
    let elements = parse_c_array_elements(inner);
    let value = encode_array_text(&elements);

    Some(SaveEntry {
        pv_name: pv_name.to_string(),
        value,
        connected: true,
    })
}

/// Parse C array elements: `"e1" "e2" "e3"` or `1.0 2.0 3.0`
fn parse_c_array_elements(s: &str) -> Vec<String> {
    let mut elements = Vec::new();
    let mut chars = s.chars().peekable();

    loop {
        // Skip whitespace
        while chars.peek().map_or(false, |c| c.is_whitespace()) {
            chars.next();
        }
        if chars.peek().is_none() {
            break;
        }

        if chars.peek() == Some(&'"') {
            // Quoted string element
            chars.next(); // skip opening quote
            let mut elem = String::new();
            loop {
                match chars.next() {
                    Some('\\') => {
                        if let Some(c) = chars.next() {
                            elem.push(c);
                        }
                    }
                    Some('"') => break,
                    Some(c) => elem.push(c),
                    None => break,
                }
            }
            elements.push(elem);
        } else {
            // Unquoted element (number)
            let mut elem = String::new();
            while chars.peek().map_or(false, |c| !c.is_whitespace()) {
                elem.push(chars.next().unwrap());
            }
            if !elem.is_empty() {
                elements.push(elem);
            }
        }
    }

    elements
}

/// Convert an EpicsValue to a save file string.
pub fn value_to_save_str(value: &EpicsValue) -> String {
    match value {
        EpicsValue::String(s) => format!(
            "\"{}\"",
            s.as_str_lossy().replace('\\', "\\\\").replace('"', "\\\"")
        ),
        EpicsValue::Double(v) => format!("{:.14e}", v),
        EpicsValue::Float(v) => format!("{:.7e}", v),
        EpicsValue::Short(v) => v.to_string(),
        EpicsValue::Long(v) => v.to_string(),
        EpicsValue::Int64(v) => v.to_string(),
        EpicsValue::Enum(v) => v.to_string(),
        // NTEnum carrier never reaches an autosave write (it is consumed
        // at the link-write boundary), but serialize the index for
        // exhaustiveness, identical to `Enum`.
        EpicsValue::EnumWithChoices { index, .. } => index.to_string(),
        EpicsValue::Char(v) => v.to_string(),
        EpicsValue::DoubleArray(arr) => {
            encode_array_text(arr.iter().map(|v| format!("{:.14e}", v)))
        }
        EpicsValue::LongArray(arr) => encode_array_text(arr.iter().map(|v| v.to_string())),
        EpicsValue::CharArray(arr) => encode_array_text(arr.iter().map(|v| v.to_string())),
        EpicsValue::ShortArray(arr) => encode_array_text(arr.iter().map(|v| v.to_string())),
        EpicsValue::FloatArray(arr) => encode_array_text(arr.iter().map(|v| format!("{:.7e}", v))),
        EpicsValue::EnumArray(arr) => encode_array_text(arr.iter().map(|v| v.to_string())),
        EpicsValue::Int64Array(arr) => encode_array_text(arr.iter().map(|v| v.to_string())),
        EpicsValue::UInt64(v) => v.to_string(),
        EpicsValue::UInt64Array(arr) => encode_array_text(arr.iter().map(|v| v.to_string())),
        EpicsValue::UShort(v) => v.to_string(),
        EpicsValue::ULong(v) => v.to_string(),
        EpicsValue::UShortArray(arr) => encode_array_text(arr.iter().map(|v| v.to_string())),
        EpicsValue::ULongArray(arr) => encode_array_text(arr.iter().map(|v| v.to_string())),
        EpicsValue::UChar(v) => v.to_string(),
        EpicsValue::UCharArray(arr) => encode_array_text(arr.iter().map(|v| v.to_string())),
        EpicsValue::StringArray(arr) => {
            encode_array_text(arr.iter().map(|s| s.as_str_lossy().into_owned()))
        }
    }
}

/// Convert an `EpicsValue` to a **C-autosave wire-compatible** save
/// string, so a C IOC (or `asVerify` in a C IOC) can read the file.
///
/// Differences from [`value_to_save_str`] (autosave-rs native):
///
/// * Scalar strings are written **unquoted** — C autosave's
///   `dbrestore.c` treats everything after the first space on a line
///   as the literal value; it does not strip quotes from a scalar.
/// * Scalar numbers are plain (same as native).
/// * Arrays use C's `@array@ { "v" "v" ... }` form (the form the
///   native reader already accepts via `parse_c_array_line`), with
///   every element double-quoted and `"`/`\` escaped — instead of the
///   native `[v,v,v]` form a C IOC cannot parse.
pub fn value_to_save_str_c(value: &EpicsValue) -> String {
    fn esc(s: &str) -> String {
        s.replace('\\', "\\\\").replace('"', "\\\"")
    }
    fn c_array<T, I>(iter: I) -> String
    where
        I: IntoIterator<Item = T>,
        T: ToString,
    {
        let parts: Vec<String> = iter
            .into_iter()
            .map(|v| format!("\"{}\"", esc(&v.to_string())))
            .collect();
        format!("{ARRAY_MARKER} {{ {} }}", parts.join(" "))
    }
    match value {
        // Scalars: plain printf form, strings unquoted.
        EpicsValue::String(s) => s.as_str_lossy().into_owned(),
        EpicsValue::Double(v) => format!("{:.14e}", v),
        EpicsValue::Float(v) => format!("{:.7e}", v),
        EpicsValue::Short(v) => v.to_string(),
        EpicsValue::Long(v) => v.to_string(),
        EpicsValue::Int64(v) => v.to_string(),
        EpicsValue::Enum(v) => v.to_string(),
        // NTEnum carrier never reaches an autosave write (it is consumed
        // at the link-write boundary), but serialize the index for
        // exhaustiveness, identical to `Enum`.
        EpicsValue::EnumWithChoices { index, .. } => index.to_string(),
        EpicsValue::Char(v) => v.to_string(),
        // Arrays: C `@array@ { "v" "v" }` form.
        EpicsValue::DoubleArray(arr) => c_array(arr.iter().map(|v| format!("{:.14e}", v))),
        EpicsValue::FloatArray(arr) => c_array(arr.iter().map(|v| format!("{:.7e}", v))),
        EpicsValue::LongArray(arr) => c_array(arr.iter()),
        EpicsValue::CharArray(arr) => c_array(arr.iter()),
        EpicsValue::ShortArray(arr) => c_array(arr.iter()),
        EpicsValue::EnumArray(arr) => c_array(arr.iter()),
        EpicsValue::Int64Array(arr) => c_array(arr.iter()),
        EpicsValue::UInt64(v) => v.to_string(),
        EpicsValue::UInt64Array(arr) => c_array(arr.iter()),
        EpicsValue::UShort(v) => v.to_string(),
        EpicsValue::ULong(v) => v.to_string(),
        EpicsValue::UShortArray(arr) => c_array(arr.iter()),
        EpicsValue::ULongArray(arr) => c_array(arr.iter()),
        EpicsValue::UChar(v) => v.to_string(),
        EpicsValue::UCharArray(arr) => c_array(arr.iter()),
        EpicsValue::StringArray(arr) => c_array(arr.iter().cloned()),
    }
}

/// Parse a save file value string back to EpicsValue, using template for type.
pub fn parse_save_value(s: &str, template: &EpicsValue) -> Option<EpicsValue> {
    let s = s.trim();
    match template {
        EpicsValue::String(_) => {
            if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
                let inner = &s[1..s.len() - 1];
                let unescaped = inner.replace("\\\"", "\"").replace("\\\\", "\\");
                Some(EpicsValue::String(unescaped.into()))
            } else {
                Some(EpicsValue::String(s.to_string().into()))
            }
        }
        EpicsValue::Double(_) => s.parse::<f64>().ok().map(EpicsValue::Double),
        EpicsValue::Float(_) => s.parse::<f32>().ok().map(EpicsValue::Float),
        EpicsValue::Long(_) => s.parse::<i32>().ok().map(EpicsValue::Long),
        EpicsValue::Int64(_) => s.parse::<i64>().ok().map(EpicsValue::Int64),
        EpicsValue::UInt64(_) => s.parse::<u64>().ok().map(EpicsValue::UInt64),
        EpicsValue::Short(_) => s.parse::<i16>().ok().map(EpicsValue::Short),
        EpicsValue::Enum(_) | EpicsValue::EnumWithChoices { .. } => {
            s.parse::<u16>().ok().map(EpicsValue::Enum)
        }
        EpicsValue::Char(_) => s.parse::<u8>().ok().map(EpicsValue::Char),
        EpicsValue::UChar(_) => s.parse::<u8>().ok().map(EpicsValue::UChar),
        EpicsValue::DoubleArray(_) => {
            parse_array_str(s, |v| v.parse::<f64>().ok()).map(EpicsValue::DoubleArray)
        }
        EpicsValue::LongArray(_) => {
            parse_array_str(s, |v| v.parse::<i32>().ok()).map(EpicsValue::LongArray)
        }
        EpicsValue::CharArray(_) => {
            parse_array_str(s, |v| v.parse::<u8>().ok()).map(EpicsValue::CharArray)
        }
        EpicsValue::ShortArray(_) => {
            parse_array_str(s, |v| v.parse::<i16>().ok()).map(EpicsValue::ShortArray)
        }
        EpicsValue::FloatArray(_) => {
            parse_array_str(s, |v| v.parse::<f32>().ok()).map(EpicsValue::FloatArray)
        }
        EpicsValue::EnumArray(_) => {
            parse_array_str(s, |v| v.parse::<u16>().ok()).map(EpicsValue::EnumArray)
        }
        EpicsValue::Int64Array(_) => {
            parse_array_str(s, |v| v.parse::<i64>().ok()).map(EpicsValue::Int64Array)
        }
        EpicsValue::UInt64Array(_) => {
            parse_array_str(s, |v| v.parse::<u64>().ok()).map(EpicsValue::UInt64Array)
        }
        EpicsValue::UShort(_) => s.parse::<u16>().ok().map(EpicsValue::UShort),
        EpicsValue::ULong(_) => s.parse::<u32>().ok().map(EpicsValue::ULong),
        EpicsValue::UShortArray(_) => {
            parse_array_str(s, |v| v.parse::<u16>().ok()).map(EpicsValue::UShortArray)
        }
        EpicsValue::ULongArray(_) => {
            parse_array_str(s, |v| v.parse::<u32>().ok()).map(EpicsValue::ULongArray)
        }
        EpicsValue::UCharArray(_) => {
            parse_array_str(s, |v| v.parse::<u8>().ok()).map(EpicsValue::UCharArray)
        }
        EpicsValue::StringArray(_) => Some(EpicsValue::StringArray(
            decode_array_text(s).into_iter().map(Into::into).collect(),
        )),
    }
}

fn parse_array_str<T, F>(s: &str, parse_elem: F) -> Option<Vec<T>>
where
    F: Fn(&str) -> Option<T>,
{
    decode_array_text(s).iter().map(|v| parse_elem(v)).collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::server::autosave::format::CompatMode;
    use crate::types::EpicsValue;

    /// M6: a C-format scalar string is written UNQUOTED (C autosave
    /// treats everything after the first space as the literal value).
    #[test]
    fn c_format_scalar_string_unquoted() {
        let v = EpicsValue::String("hello world".to_string().into());
        assert_eq!(value_to_save_str_c(&v), "hello world");
        // Native quotes it.
        assert_eq!(value_to_save_str(&v), "\"hello world\"");
    }

    /// M6: a C-format array uses the `@array@ { "v" "v" }` form a C
    /// IOC can parse — not the native `[v,v,v]` form.
    #[test]
    fn c_format_array_uses_at_array_form() {
        let v = EpicsValue::LongArray(vec![1, 2, 3]);
        assert_eq!(value_to_save_str_c(&v), "@array@ { \"1\" \"2\" \"3\" }");
        assert_eq!(value_to_save_str(&v), "[1,2,3]");
    }

    /// M6: a `.sav` written in `CompatMode::CRead` carries the
    /// `save/restore` banner (the banner a C IOC expects) and is
    /// still readable by the Rust reader — the array form
    /// round-trips through `parse_c_array_line`.
    #[epics_macros_rs::epics_test]
    async fn c_compat_save_file_has_c_banner_and_round_trips() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("c.sav");

        let entries = vec![
            SaveEntry {
                pv_name: "PV:SCALAR".to_string(),
                value: value_to_save_str_c(&EpicsValue::Long(42)),
                connected: true,
            },
            SaveEntry {
                pv_name: "PV:ARRAY".to_string(),
                value: value_to_save_str_c(&EpicsValue::LongArray(vec![10, 20])),
                connected: true,
            },
        ];
        write_save_file_with_mode(&path, &entries, CompatMode::CRead)
            .await
            .unwrap();

        let content = crate::runtime::fs::read_to_string(&path).await.unwrap();
        assert!(
            content.starts_with("# save/restore"),
            "C-compat file must carry the save/restore banner, got: {content}"
        );
        assert!(content.contains("PV:ARRAY @array@ { \"10\" \"20\" }"));

        // Reader accepts the C-format file.
        let read = read_save_file(&path)
            .await
            .unwrap()
            .expect("valid file")
            .entries;
        assert_eq!(read.len(), 2);
        let arr = read.iter().find(|e| e.pv_name == "PV:ARRAY").unwrap();
        assert_eq!(arr.value, "[10,20]");
        let parsed = parse_save_value(&arr.value, &EpicsValue::LongArray(vec![])).unwrap();
        assert_eq!(parsed, EpicsValue::LongArray(vec![10, 20]));
    }

    /// M6: native mode still writes the autosave-rs banner.
    #[epics_macros_rs::epics_test]
    async fn native_save_file_keeps_native_banner() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("native.sav");
        write_save_file(
            &path,
            &[SaveEntry {
                pv_name: "PV1".to_string(),
                value: "1".to_string(),
                connected: true,
            }],
        )
        .await
        .unwrap();
        let content = crate::runtime::fs::read_to_string(&path).await.unwrap();
        assert!(content.starts_with("# autosave-rs"));
    }
}