zshrs 0.11.29

The first compiled Unix shell — bytecode VM, worker pool, AOP intercept, Rkyv caching
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//! Zsh AST types — Rust-only, NOT in zsh C.
//!
//! zsh C does NOT have an AST tree. Its parser emits a flat wordcode
//! stream (`Wordcode ecbuf[]`) directly via `par_event` → `par_list` →
//! `par_sublist` → `par_pline` → `par_cmd` → `par_simple` / `par_redir`
//! (Src/parse.c:485-3000). The wordcode is consumed by `execlist` /
//! `execpline` / `execcmd` in `Src/exec.c` via `WC_KIND`/`wc_code`/
//! `wc_data` macros walking `ecbuf`.
//!
//! zshrs built an AST tree as an intermediate step on the way to
//! wordcode. This file holds those Rust-only AST node types.
//! Originally lived in `src/ported/parse.rs` but relocated here for
//! P9e of the PORT_PLAN.md migration to make their non-C-faithful
//! nature explicit.
//!
//! Phase 9c (par_* wordcode emission) + Phase 9d (vm_helper wordcode
//! consumer rewrite) will eventually retire these types entirely —
//! the parser will emit wordcode directly and the executor will read
//! wordcode directly, matching the C pipeline. Until then, the AST
//! tree is the working IR.

pub use crate::extensions::heredoc_ast::HereDocInfo;
use serde::{Deserialize, Serialize};

/// AST node for a complete program (list of commands)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshProgram {
    /// `lists` field.
    pub lists: Vec<ZshList>,
}

/// A list is a sequence of sublists separated by ; or & or newline
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshList {
    /// `sublist` field.
    pub sublist: ZshSublist,
    /// `flags` field.
    pub flags: ListFlags,
}
/// `ListFlags` — see fields for layout.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct ListFlags {
    /// Run asynchronously (&)
    pub async_: bool,
    /// Disown after running (&| or &!)
    pub disown: bool,
}

/// A sublist is pipelines connected by && or ||
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshSublist {
    /// `pipe` field.
    pub pipe: ZshPipe,
    /// `next` field.
    pub next: Option<(SublistOp, Box<ZshSublist>)>,
    /// `flags` field.
    pub flags: SublistFlags,
}
/// `SublistOp` — see variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SublistOp {
    /// `And` variant.
    And, // &&
    /// `Or` variant.
    Or, // ||
}
/// `SublistFlags` — see fields for layout.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct SublistFlags {
    /// Coproc
    pub coproc: bool,
    /// Negated with !
    pub not: bool,
}

/// A pipeline is commands connected by |
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshPipe {
    /// `cmd` field.
    pub cmd: ZshCommand,
    /// `next` field.
    pub next: Option<Box<ZshPipe>>,
    /// `lineno` field.
    pub lineno: u64,
    /// `|&` between this stage and the next — merge stderr into the
    /// pipe so the next stage's stdin sees both stdout AND stderr from
    /// this stage. When `next` is None this flag is meaningless.
    #[serde(default)]
    pub merge_stderr: bool,
}

/// A command
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ZshCommand {
    /// `Simple` variant.
    Simple(ZshSimple),
    /// `Subsh` variant.
    Subsh(Box<ZshProgram>), // (list)
    /// `Cursh` variant.
    Cursh(Box<ZshProgram>), // {list}
    /// `For` variant.
    For(ZshFor),
    /// `Case` variant.
    Case(ZshCase),
    /// `If` variant.
    If(ZshIf),
    /// `While` variant.
    While(ZshWhile),
    /// `Until` variant.
    Until(ZshWhile),
    /// `Repeat` variant.
    Repeat(ZshRepeat),
    /// `FuncDef` variant.
    FuncDef(ZshFuncDef),
    /// `Time` variant.
    Time(Option<Box<ZshSublist>>),
    /// `Cond` variant.
    Cond(ZshCond), // [[ ... ]]
    /// `Arith` variant.
    Arith(String), // (( ... ))
    /// `Try` variant.
    Try(ZshTry), // { ... } always { ... }
    /// Compound command with trailing redirects:
    /// `{ cmd } 2>&1`, `(...) >file`, `if ...; fi >file`, etc.
    /// Simple commands carry redirects in their own struct; this wrapper
    /// is only used for compound forms.
    Redirected(Box<ZshCommand>, Vec<ZshRedir>),
}

/// A simple command (assignments, words, redirections)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshSimple {
    /// `assigns` field.
    pub assigns: Vec<ZshAssign>,
    /// `words` field.
    pub words: Vec<String>,
    /// `redirs` field.
    pub redirs: Vec<ZshRedir>,
}

/// An assignment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshAssign {
    /// `name` field.
    pub name: String,
    /// `value` field.
    pub value: ZshAssignValue,
    pub append: bool, // +=
}
/// `ZshAssignValue` — see variants.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ZshAssignValue {
    /// `Scalar` variant.
    Scalar(String),
    /// `Array` variant.
    Array(Vec<String>),
}

/// A redirection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshRedir {
    /// `rtype` field.
    pub rtype: i32,
    /// `fd` field.
    pub fd: i32,
    /// `name` field.
    pub name: String,
    /// `heredoc` field.
    pub heredoc: Option<HereDocInfo>,
    pub varid: Option<String>, // {var}>file
    /// Index into the lexer-side `HEREDOCS` thread_local for body lookup. Filled in by
    /// `parse_redirection` for Heredoc/HeredocDash, then resolved into
    /// `heredoc.content` by `fill_heredoc_bodies` after process_heredocs
    /// has run for the line.
    #[serde(skip)]
    pub heredoc_idx: Option<usize>,
}

/// For loop
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshFor {
    /// `var` field.
    pub var: String,
    /// `list` field.
    pub list: ForList,
    /// `body` field.
    pub body: Box<ZshProgram>,
    /// True if this was parsed as `select` rather than `for`. Both share
    /// the same parser, so the compiler routes on this flag.
    #[serde(default)]
    pub is_select: bool,
}
/// `ForList` — see variants.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ForList {
    /// `Words` variant.
    Words(Vec<String>),
    /// `CStyle` variant.
    CStyle {
        init: String,
        cond: String,
        step: String,
    },
    /// `Positional` variant.
    Positional,
}

/// Case statement
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshCase {
    /// `word` field.
    pub word: String,
    /// `arms` field.
    pub arms: Vec<CaseArm>,
}
/// `CaseArm` — see fields for layout.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CaseArm {
    /// `patterns` field.
    pub patterns: Vec<String>,
    /// `body` field.
    pub body: ZshProgram,
    /// `terminator` field.
    pub terminator: CaseTerm,
}
/// `CaseTerm` — see variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CaseTerm {
    /// `Break` variant.
    Break, // ;;
    /// `Continue` variant.
    Continue, // ;&
    /// `TestNext` variant.
    TestNext, // ;|
}

/// If statement
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshIf {
    /// `cond` field.
    pub cond: Box<ZshProgram>,
    /// `then` field.
    pub then: Box<ZshProgram>,
    /// `elif` field.
    pub elif: Vec<(ZshProgram, ZshProgram)>,
    /// `else_` field.
    pub else_: Option<Box<ZshProgram>>,
}

/// While/Until loop
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshWhile {
    /// `cond` field.
    pub cond: Box<ZshProgram>,
    /// `body` field.
    pub body: Box<ZshProgram>,
    /// `until` field.
    pub until: bool,
}

/// Repeat loop
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshRepeat {
    /// `count` field.
    pub count: String,
    /// `body` field.
    pub body: Box<ZshProgram>,
}

/// Function definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshFuncDef {
    /// `names` field.
    pub names: Vec<String>,
    /// `body` field.
    pub body: Box<ZshProgram>,
    /// `tracing` field.
    pub tracing: bool,
    /// Anonymous-function call args. `() { body } a b` parses as a
    /// FuncDef (auto-named) with `auto_call_args = Some(vec!["a", "b"])`.
    /// compile_funcdef registers the function then emits a Simple call
    /// with these args.
    #[serde(default)]
    pub auto_call_args: Option<Vec<String>>,
    /// Original source text of the function body (the bytes between
    /// `{` and `}`, without the braces themselves), captured at parse
    /// time. Populated for `function name { body }` and `function name() { body }`
    /// forms; left None for the synthesized inline-funcdef recovery
    /// path. ZshCompiler::compile_funcdef forwards it to
    /// `BUILTIN_REGISTER_COMPILED_FN` so introspection (`whence`, `which`,
    /// `${functions[name]}`) has canonical source text.
    #[serde(default)]
    pub body_source: Option<String>,
}

/// Conditional expression [[ ... ]]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ZshCond {
    /// `Not` variant.
    Not(Box<ZshCond>),
    /// `And` variant.
    And(Box<ZshCond>, Box<ZshCond>),
    /// `Or` variant.
    Or(Box<ZshCond>, Box<ZshCond>),
    /// `Unary` variant.
    Unary(String, String), // -f file, -n str, etc.
    /// `Binary` variant.
    Binary(String, String, String), // str = pat, a -eq b, etc.
    /// `Regex` variant.
    Regex(String, String), // str =~ regex
}

/// Try/always block
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZshTry {
    /// `try_block` field.
    pub try_block: Box<ZshProgram>,
    /// `always` field.
    pub always: Box<ZshProgram>,
}

/// Zsh parameter expansion flags
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ZshParamFlag {
    /// `Lower` variant.
    Lower, // L - lowercase
    /// `Upper` variant.
    Upper, // U - uppercase
    /// `Capitalize` variant.
    Capitalize, // C - capitalize words
    /// `Join` variant.
    Join(String), // j:sep: - join array with separator
    /// `JoinNewline` variant.
    JoinNewline, // F - join with newlines
    /// `Split` variant.
    Split(String), // s:sep: - split string into array
    /// `SplitLines` variant.
    SplitLines, // f - split on newlines
    /// `SplitWords` variant.
    SplitWords, // z - split into words (shell parsing)
    /// `Type` variant.
    Type, // t - type of variable
    /// `Words` variant.
    Words, // w - word splitting
    /// `Quote` variant.
    Quote, // qq - single-quote always
    /// `QuoteIfNeeded` variant.
    QuoteIfNeeded, // q+ - single-quote only if needed
    /// `DoubleQuote` variant.
    DoubleQuote, // qqq - double-quote
    /// `DollarQuote` variant.
    DollarQuote, // qqqq - $'...' style
    /// `QuoteBackslash` variant.
    QuoteBackslash, // q / b / B - backslash-escape special chars
    /// `Unique` variant.
    Unique, // u - unique elements only
    /// `Reverse` variant.
    Reverse, // O - reverse sort
    /// `Sort` variant.
    Sort, // o - sort
    /// `NumericSort` variant.
    NumericSort, // n - numeric sort
    /// `IndexSort` variant.
    IndexSort, // a - sort in array index order
    /// `Keys` variant.
    Keys, // k - associative array keys
    /// `Values` variant.
    Values, // v - associative array values
    /// `Length` variant.
    Length, // # - length (character codes)
    /// `CountChars` variant.
    CountChars, // c - count total characters
    /// `Expand` variant.
    Expand, // e - perform shell expansions
    /// `PromptExpand` variant.
    PromptExpand, // % - expand prompt escapes
    /// `PromptExpandFull` variant.
    PromptExpandFull, // %% - full prompt expansion
    /// `Visible` variant.
    Visible, // V - make non-printable chars visible
    /// `Directory` variant.
    Directory, // D - substitute directory names
    /// `Head` variant.
    Head(usize), // [1,n] - first n elements
    /// `Tail` variant.
    Tail(usize), // [-n,-1] - last n elements
    /// `PadLeft` variant.
    PadLeft(usize, char), // l:len:fill: - pad left
    /// `PadRight` variant.
    PadRight(usize, char), // r:len:fill: - pad right
    /// `Width` variant.
    Width(usize), // m - use width for padding
    /// `Match` variant.
    Match, // M - include matched portion
    /// `Remove` variant.
    Remove, // R - include non-matched portion (complement of M)
    /// `Subscript` variant.
    Subscript, // S - subscript scanning
    /// `Parameter` variant.
    Parameter, // P - use value as parameter name (indirection)
    /// `Glob` variant.
    Glob, // ~ - glob patterns in pattern
    /// `@` flag — force array-context behavior even inside DQ. zsh's
    /// `"${(@o)arr}"` keeps the sort active and splices each element as
    /// its own word. Without this, the array-only flags became no-ops
    /// in DQ.
    At,
}

/// List operator (for shell command lists)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ListOp {
    /// `And` variant.
    And, // &&
    /// `Or` variant.
    Or, // ||
    /// `Semi` variant.
    Semi, // ;
    /// `Amp` variant.
    Amp, // &
    /// `Newline` variant.
    Newline, // \n
}

/// Shell word - can be simple literal or complex expansion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ShellWord {
    /// Plain text token. Most ZWC-decoded words land here. Goes through
    /// `expand_string` (plus glob/tilde/etc. as text-level transforms) for
    /// final output.
    Literal(String),
    /// Concatenation of sub-words. ZWC array decoding produces this with
    /// child Literals; nothing else constructs it now that the legacy
    /// hand-rolled parser is gone.
    Concat(Vec<ShellWord>),
}

/// Variable modifier for parameter expansion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum VarModifier {
    /// `Default` variant.
    Default(ShellWord),
    /// `DefaultAssign` variant.
    DefaultAssign(ShellWord),
    /// `Error` variant.
    Error(ShellWord),
    /// `Alternate` variant.
    Alternate(ShellWord),
    /// `Length` variant.
    Length,
    /// `Substring` variant.
    Substring(i64, Option<i64>),
    /// `RemovePrefix` variant.
    RemovePrefix(ShellWord),
    /// `RemovePrefixLong` variant.
    RemovePrefixLong(ShellWord),
    /// `RemoveSuffix` variant.
    RemoveSuffix(ShellWord),
    /// `RemoveSuffixLong` variant.
    RemoveSuffixLong(ShellWord),
    /// `Replace` variant.
    Replace(ShellWord, ShellWord),
    /// `ReplaceAll` variant.
    ReplaceAll(ShellWord, ShellWord),
    /// `${var/#pat/repl}` — anchored at start (prefix only).
    /// Per Src/subst.c paramsubst's `/`-arm with SUB_START.
    ReplacePrefix(ShellWord, ShellWord),
    /// `${var/%pat/repl}` — anchored at end (suffix only).
    /// Per Src/subst.c paramsubst's `/`-arm with SUB_END.
    ReplaceSuffix(ShellWord, ShellWord),
    /// `Upper` variant.
    Upper,
    /// `Lower` variant.
    Lower,
}

/// Shell command - the old shell_ast compatible type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ShellCommand {
    /// `Simple` variant.
    Simple(SimpleCommand),
    /// `Pipeline` variant.
    Pipeline(Vec<ShellCommand>, bool),
    List(Vec<(ShellCommand, ListOp)>),
    /// `Compound` variant.
    Compound(CompoundCommand),
    /// `FunctionDef` variant.
    FunctionDef(String, Box<ShellCommand>),
}

/// Simple command with assignments, words, and redirects
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SimpleCommand {
    /// `assignments` field.
    pub assignments: Vec<(String, ShellWord, bool)>,
    /// `words` field.
    pub words: Vec<ShellWord>,
    /// `redirects` field.
    pub redirects: Vec<Redirect>,
}

/// Redirect
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Redirect {
    /// `fd` field.
    pub fd: Option<i32>,
    /// `op` field.
    pub op: RedirectOp,
    /// `target` field.
    pub target: ShellWord,
    /// `heredoc_content` field.
    pub heredoc_content: Option<String>,
    /// `fd_var` field.
    pub fd_var: Option<String>,
}

/// Redirect operator
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RedirectOp {
    /// `Write` variant.
    Write,
    /// `Append` variant.
    Append,
    /// `Read` variant.
    Read,
    /// `ReadWrite` variant.
    ReadWrite,
    /// `Clobber` variant.
    Clobber,
    /// `DupRead` variant.
    DupRead,
    /// `DupWrite` variant.
    DupWrite,
    /// `HereDoc` variant.
    HereDoc,
    /// `HereString` variant.
    HereString,
    /// `WriteBoth` variant.
    WriteBoth,
    /// `AppendBoth` variant.
    AppendBoth,
}

/// Compound command
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CompoundCommand {
    /// `BraceGroup` variant.
    BraceGroup(Vec<ShellCommand>),
    /// `Subshell` variant.
    Subshell(Vec<ShellCommand>),
    /// `If` variant.
    If {
        conditions: Vec<(Vec<ShellCommand>, Vec<ShellCommand>)>,
        else_part: Option<Vec<ShellCommand>>,
    },
    /// `For` variant.
    For {
        var: String,
        words: Option<Vec<ShellWord>>,
        body: Vec<ShellCommand>,
    },
    /// `ForArith` variant.
    ForArith {
        init: String,
        cond: String,
        step: String,
        body: Vec<ShellCommand>,
    },
    /// `While` variant.
    While {
        condition: Vec<ShellCommand>,
        body: Vec<ShellCommand>,
    },
    /// `Until` variant.
    Until {
        condition: Vec<ShellCommand>,
        body: Vec<ShellCommand>,
    },
    /// `Case` variant.
    Case {
        word: ShellWord,
        cases: Vec<(Vec<ShellWord>, Vec<ShellCommand>, CaseTerminator)>,
    },
    /// `Select` variant.
    Select {
        var: String,
        words: Option<Vec<ShellWord>>,
        body: Vec<ShellCommand>,
    },
    /// `Coproc` variant.
    Coproc {
        name: Option<String>,
        body: Box<ShellCommand>,
    },
    /// repeat N do ... done
    Repeat {
        count: String,
        body: Vec<ShellCommand>,
    },
    /// { try-block } always { always-block }
    Try {
        try_body: Vec<ShellCommand>,
        always_body: Vec<ShellCommand>,
    },
    /// `Arith` variant.
    Arith(String),
    /// `WithRedirects` variant.
    WithRedirects(Box<ShellCommand>, Vec<Redirect>),
}

/// Case terminator
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum CaseTerminator {
    /// `Break` variant.
    Break,
    /// `Fallthrough` variant.
    Fallthrough,
    /// `Continue` variant.
    Continue,
}

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

    // === Default impls for flag structs (load-bearing for parser init) ===

    #[test]
    fn list_flags_default_all_false() {
        let f = ListFlags::default();
        assert!(!f.async_, "default ListFlags.async_ must be false");
        assert!(!f.disown, "default ListFlags.disown must be false");
    }

    #[test]
    fn sublist_flags_default_all_false() {
        let f = SublistFlags::default();
        assert!(!f.coproc);
        assert!(!f.not);
    }

    // === Serde round-trip: zshrs cache format must survive across versions ===

    #[test]
    fn zsh_program_empty_round_trips() {
        // Empty program is the parse result for an empty input.
        let p = ZshProgram { lists: vec![] };
        let json = serde_json::to_string(&p).expect("serialize");
        let back: ZshProgram = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.lists.len(), 0);
    }

    #[test]
    fn zsh_simple_round_trips_with_assigns_and_redirs() {
        // Simple command: FOO=bar BAZ=qux echo hi >out
        let simple = ZshSimple {
            assigns: vec![
                ZshAssign {
                    name: "FOO".to_string(),
                    value: ZshAssignValue::Scalar("bar".to_string()),
                    append: false,
                },
                ZshAssign {
                    name: "BAZ".to_string(),
                    value: ZshAssignValue::Scalar("qux".to_string()),
                    append: true,
                },
            ],
            words: vec!["echo".to_string(), "hi".to_string()],
            redirs: vec![ZshRedir {
                rtype: 0,
                fd: 1,
                name: "out".to_string(),
                heredoc: None,
                varid: None,
                heredoc_idx: None,
            }],
        };
        let json = serde_json::to_string(&simple).expect("serialize");
        let back: ZshSimple = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.assigns.len(), 2);
        assert_eq!(back.assigns[0].name, "FOO");
        assert!(back.assigns[1].append, "+= flag must round-trip");
        assert_eq!(back.words, vec!["echo", "hi"]);
        assert_eq!(back.redirs.len(), 1);
        assert_eq!(back.redirs[0].name, "out");
    }

    #[test]
    fn assign_value_array_variant_round_trips() {
        let v = ZshAssignValue::Array(vec!["a".to_string(), "b".to_string(), "c".to_string()]);
        let json = serde_json::to_string(&v).expect("serialize");
        let back: ZshAssignValue = serde_json::from_str(&json).expect("deserialize");
        match back {
            ZshAssignValue::Array(items) => assert_eq!(items, vec!["a", "b", "c"]),
            ZshAssignValue::Scalar(s) => panic!("expected Array, got Scalar({s:?})"),
        }
    }

    #[test]
    fn for_list_c_style_round_trips() {
        let fl = ForList::CStyle {
            init: "i=0".to_string(),
            cond: "i<10".to_string(),
            step: "i++".to_string(),
        };
        let json = serde_json::to_string(&fl).expect("serialize");
        let back: ForList = serde_json::from_str(&json).expect("deserialize");
        match back {
            ForList::CStyle { init, cond, step } => {
                assert_eq!(init, "i=0");
                assert_eq!(cond, "i<10");
                assert_eq!(step, "i++");
            }
            _ => panic!("expected CStyle variant"),
        }
    }

    #[test]
    fn for_list_positional_round_trips() {
        // Positional: `for x do ... done` (no in-words clause).
        let fl = ForList::Positional;
        let json = serde_json::to_string(&fl).expect("serialize");
        let back: ForList = serde_json::from_str(&json).expect("deserialize");
        assert!(matches!(back, ForList::Positional));
    }

    #[test]
    fn case_terminator_all_variants_round_trip() {
        // ;; / ;& / ;| terminator variants — each must survive serde.
        for t in [CaseTerm::Break, CaseTerm::Continue, CaseTerm::TestNext] {
            let json = serde_json::to_string(&t).expect("serialize");
            let back: CaseTerm = serde_json::from_str(&json).expect("deserialize");
            // CaseTerm is Copy + PartialEq, so equality is meaningful.
            assert_eq!(back, t);
        }
    }

    #[test]
    fn sublist_op_round_trips_both_variants() {
        for op in [SublistOp::And, SublistOp::Or] {
            let json = serde_json::to_string(&op).expect("serialize");
            let back: SublistOp = serde_json::from_str(&json).expect("deserialize");
            assert_eq!(back, op);
        }
    }

    #[test]
    fn list_op_round_trips_all_variants() {
        for op in [
            ListOp::And,
            ListOp::Or,
            ListOp::Semi,
            ListOp::Amp,
            ListOp::Newline,
        ] {
            let json = serde_json::to_string(&op).expect("serialize");
            let back: ListOp = serde_json::from_str(&json).expect("deserialize");
            assert_eq!(back, op);
        }
    }

    #[test]
    fn shell_word_concat_round_trips_nested() {
        // Concat is the AST shape used for word-internal sub-expansions —
        // verify nesting survives.
        let w = ShellWord::Concat(vec![
            ShellWord::Literal("foo".to_string()),
            ShellWord::Literal("bar".to_string()),
            ShellWord::Concat(vec![ShellWord::Literal("baz".to_string())]),
        ]);
        let json = serde_json::to_string(&w).expect("serialize");
        let back: ShellWord = serde_json::from_str(&json).expect("deserialize");
        match back {
            ShellWord::Concat(parts) => {
                assert_eq!(parts.len(), 3, "outer concat must preserve element count");
                match &parts[2] {
                    ShellWord::Concat(inner) => assert_eq!(inner.len(), 1),
                    _ => panic!("nested concat lost"),
                }
            }
            _ => panic!("expected Concat top-level"),
        }
    }

    #[test]
    fn zsh_cond_nested_serialization() {
        // [[ -f file && ! -d dir ]] type compound — verify nested
        // And/Not survive a round-trip.
        let c = ZshCond::And(
            Box::new(ZshCond::Unary("-f".to_string(), "file".to_string())),
            Box::new(ZshCond::Not(Box::new(ZshCond::Unary(
                "-d".to_string(),
                "dir".to_string(),
            )))),
        );
        let json = serde_json::to_string(&c).expect("serialize");
        let back: ZshCond = serde_json::from_str(&json).expect("deserialize");
        match back {
            ZshCond::And(lhs, rhs) => {
                assert!(matches!(*lhs, ZshCond::Unary(_, _)));
                assert!(matches!(*rhs, ZshCond::Not(_)));
            }
            _ => panic!("expected And at root"),
        }
    }

    #[test]
    fn redirect_op_all_variants_round_trip() {
        for op in [
            RedirectOp::Write,
            RedirectOp::Append,
            RedirectOp::Read,
            RedirectOp::ReadWrite,
            RedirectOp::Clobber,
            RedirectOp::DupRead,
            RedirectOp::DupWrite,
            RedirectOp::HereDoc,
            RedirectOp::HereString,
            RedirectOp::WriteBoth,
            RedirectOp::AppendBoth,
        ] {
            let json = serde_json::to_string(&op).expect("serialize");
            let back: RedirectOp = serde_json::from_str(&json).expect("deserialize");
            assert_eq!(back, op);
        }
    }

    #[test]
    fn zsh_funcdef_serde_default_fields() {
        // ZshFuncDef has #[serde(default)] on auto_call_args and
        // body_source — JSON missing those fields must still decode.
        let json = r#"{
            "names": ["myfn"],
            "body": { "lists": [] },
            "tracing": false
        }"#;
        let fd: ZshFuncDef = serde_json::from_str(json).expect("default fields must apply");
        assert_eq!(fd.names, vec!["myfn"]);
        assert!(fd.auto_call_args.is_none());
        assert!(fd.body_source.is_none());
        assert!(!fd.tracing);
    }

    #[test]
    fn zsh_pipe_merge_stderr_default_when_missing() {
        // `merge_stderr` defaults to false via #[serde(default)] —
        // older cache entries lacking the field must still decode.
        let json = r#"{
            "cmd": { "Simple": { "assigns": [], "words": ["x"], "redirs": [] } },
            "next": null,
            "lineno": 1
        }"#;
        let pipe: ZshPipe = serde_json::from_str(json).expect("default must apply");
        assert!(!pipe.merge_stderr);
        assert_eq!(pipe.lineno, 1);
    }
}