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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
#![doc = include_str!("../docs/lib.md")]
#![cfg_attr(docsrs, feature(doc_cfg))]

use std::{
    borrow::{Borrow, Cow},
    collections::HashMap,
    ffi::{OsStr, OsString},
    fmt::Debug,
    fmt::{Display, Write},
    hash::Hash,
    io,
    ops::Deref,
    path::{Path, PathBuf},
    process::Command,
    sync::Arc,
};

use sealed::Sealed;

#[cfg(all(not(xscript_unstable), feature = "docker"))]
compile_error!("The `docker` feature requires `--cfg xscript_unstable`.");

#[cfg(all(not(xscript_unstable), any(features = "async", feature = "tokio")))]
compile_error!("The `async` and `tokio` features require `--cfg xscript_unstable`.");

#[cfg(feature = "docker")]
#[cfg_attr(docsrs, doc(cfg(feature = "docker")))]
pub mod docker;
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
#[cfg(feature = "tokio")]
pub mod tokio;

/// Module for sealing traits.
#[doc(hidden)]
mod sealed {
    use std::ffi::{OsStr, OsString};

    pub trait Sealed {}

    impl Sealed for str {}

    impl Sealed for OsStr {}

    impl Sealed for String {}

    impl Sealed for OsString {}
}

/// Lossy string conversion.
pub trait ToStringLossy: sealed::Sealed {
    /// Convert to string, potentially skipping invalid characters.
    fn to_string_lossy(&self) -> Cow<str>;
}

impl ToStringLossy for str {
    fn to_string_lossy(&self) -> Cow<str> {
        Cow::Borrowed(self)
    }
}

impl ToStringLossy for OsStr {
    fn to_string_lossy(&self) -> Cow<str> {
        OsStr::to_string_lossy(&self)
    }
}

/// A string type that can be used to construct commands.
pub trait CmdString: 'static + Debug + Clone + Default + Eq + Hash + Sealed
where
    Self: AsRef<Self::Str>,
    Self: AsRef<OsStr>,
    Self: Deref<Target = Self::Str>,
    Self: Borrow<Self::Str>,
{
    /// Unsized equivalent for references.
    type Str: ?Sized
        + ToOwned<Owned = Self>
        + AsRef<OsStr>
        + AsRef<Self::Str>
        + Eq
        + Hash
        + ToStringLossy;

    fn from_str(string: &str) -> &Self::Str;
}

impl CmdString for String {
    type Str = str;

    fn from_str(string: &str) -> &Self::Str {
        string
    }
}

impl CmdString for OsString {
    type Str = OsStr;

    fn from_str(string: &str) -> &Self::Str {
        string.as_ref()
    }
}

/// Shared inner data of a command.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct CmdData<S: CmdString> {
    /// The program to run.
    prog: S,
    /// The arguments to run the program with.
    args: Vec<S>,
    /// The directory in which to run the command.
    cwd: Option<S>,
    /// The environment variables to run the command with.
    vars: Option<Vars<S>>,
    /// The `stdin` input to provide to the command.
    stdin: Option<In>,
    /// Indicates what to do with the `stdout` output of the command.
    stdout: Option<Out>,
    /// Indicates what to do with the `stderr` output of the command.
    stderr: Option<Out>,
    /// Indicates whether the command may fail.
    may_fail: bool,
    /// Hints that the command may contain secret information.
    is_secret: bool,
}

impl<S: CmdString> CmdData<S> {
    fn new(prog: S) -> Self {
        Self {
            prog,
            args: Vec::new(),
            cwd: None,
            vars: None,
            stdin: None,
            stdout: None,
            stderr: None,
            may_fail: false,
            is_secret: false,
        }
    }
}

/// A command.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[must_use]
pub struct Cmd<S: CmdString = OsString>(Arc<CmdData<S>>);

impl<S: CmdString> Cmd<S> {
    /// Creates a new command for the given program.
    pub fn new<P: AsRef<S::Str>>(prog: P) -> Self {
        Cmd(Arc::new(CmdData::new(prog.as_ref().to_owned())))
    }

    /// The program to run.
    pub fn prog(&self) -> &S::Str {
        self.0.prog.as_ref()
    }

    /// The arguments to run the program with.
    pub fn args(&self) -> impl Iterator<Item = &S::Str> {
        self.0.args.iter().map(AsRef::as_ref)
    }

    /// The directory in which to run the command, if any.
    pub fn cwd(&self) -> Option<&S::Str> {
        self.0.cwd.as_deref()
    }

    /// The environment variables to run the command with.
    pub fn vars(&self) -> Option<&Vars<S>> {
        self.0.vars.as_ref()
    }

    /// The `stdin` input to provide to the command.
    pub fn stdin(&self) -> Option<&In> {
        self.0.stdin.as_ref()
    }

    /// Indicates what to do with the `stdout` output of the command.
    pub fn stdout(&self) -> Option<&Out> {
        self.0.stdout.as_ref()
    }

    /// Indicates what to do with the `stderr` output of the command.
    pub fn stderr(&self) -> Option<&Out> {
        self.0.stderr.as_ref()
    }

    /// Indicates whether the command may fail.
    pub fn may_fail(&self) -> bool {
        self.0.may_fail
    }

    /// Hints that the command may contain secret information.
    pub fn is_secret(&self) -> bool {
        self.0.is_secret
    }

    fn data_mut(&mut self) -> &mut CmdData<S> {
        Arc::make_mut(&mut self.0)
    }

    /// Adds an argument to the command.
    pub fn add_arg<A: AsRef<S::Str>>(&mut self, arg: A) -> &mut Self {
        self.data_mut().args.push(arg.as_ref().to_owned());
        self
    }

    /// Extends the arguments of the command.
    pub fn extend_args<A: AsRef<S::Str>, I: IntoIterator<Item = A>>(
        &mut self,
        args: I,
    ) -> &mut Self {
        self.data_mut()
            .args
            .extend(args.into_iter().map(|arg| arg.as_ref().to_owned()));
        self
    }

    /// Sets the directory in which to run the command.
    pub fn with_cwd<P: AsRef<S::Str>>(mut self, cwd: P) -> Self {
        self.data_mut().cwd = Some(cwd.as_ref().to_owned());
        self
    }

    /// Sets the environment variables to run the command with.
    pub fn with_vars(mut self, vars: Vars<S>) -> Self {
        self.data_mut().vars = Some(vars);
        self
    }

    /// Sets an environment variable.
    pub fn with_var<N: AsRef<S::Str>, V: AsRef<S::Str>>(mut self, name: N, value: V) -> Self {
        self.data_mut()
            .vars
            .get_or_insert_with(Vars::new)
            .set(name, value);
        self
    }

    /// Sets the `stdin` input to provide to the command.
    pub fn with_stdin<T: Into<In>>(mut self, stdin: T) -> Self {
        self.data_mut().stdin = Some(stdin.into());
        self
    }

    /// Sets what to do with the `stdout` output of the command.
    pub fn with_stdout(mut self, stdout: Out) -> Self {
        self.data_mut().stdout = Some(stdout);
        self
    }

    /// Sets what to do with the `stderr` output of the command.
    pub fn with_stderr(mut self, stderr: Out) -> Self {
        self.data_mut().stderr = Some(stderr);
        self
    }

    /// Do not return an error when the command fails.
    pub fn allow_failures(mut self) -> Self {
        self.data_mut().may_fail = true;
        self
    }

    /// Mark the command as secret.
    pub fn make_secret(mut self) -> Self {
        self.data_mut().is_secret = true;
        self
    }
}

impl<S: CmdString> AsRef<Cmd<S>> for Cmd<S> {
    fn as_ref(&self) -> &Cmd<S> {
        self
    }
}

impl<S: CmdString> std::fmt::Display for Cmd<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.0.is_secret {
            f.write_str("<secret command redacted>")?
        } else {
            write_escaped(f, &self.0.prog.to_string_lossy())?;
            for arg in &self.0.args {
                f.write_char(' ')?;
                write_escaped(f, &AsRef::<S::Str>::as_ref(arg).to_string_lossy())?;
            }
        }
        Ok(())
    }
}

fn write_escaped(f: &mut dyn std::fmt::Write, string: &str) -> std::fmt::Result {
    let quote = string.contains(char::is_whitespace);
    if quote {
        f.write_char('"')?;
    }
    for char in string.chars() {
        match char {
            '\\' => f.write_str("\\\\")?,
            '"' => f.write_str("\\\"")?,
            _ => f.write_char(char)?,
        }
    }
    if quote {
        f.write_char('"')?;
    }
    Ok(())
}

/// Private auxiliary macro. **Not part of the public API!**
#[macro_export]
#[doc(hidden)]
macro_rules! __private_extend_args {
    ($cmd:ident, ) => {};
    ($cmd:ident, $(, $($args:tt)*)?) => {
        $crate::__private_extend_args!($cmd, $($($args)*)*);
    };
    ($cmd:ident, ...$arg:expr $(, $($args:tt)*)?) => {
        $cmd.extend_args($arg);
        $crate::__private_extend_args!($cmd, $($($args)*)*);
    };
    ($cmd:ident, $value:literal $(, $($args:tt)*)?) => {
        $cmd.add_arg(format!($value));
        $crate::__private_extend_args!($cmd, $($($args)*)*);
    };
    ($cmd:ident, $arg:expr $(, $($args:tt)*)?) => {
        $cmd.add_arg($arg);
        $crate::__private_extend_args!($cmd, $($($args)*)*);
    }
}

/// Constructs a command.
///
/// See [crate] documentation for details and examples.
#[macro_export]
macro_rules! cmd {
    ($prog:literal $(, $($args:tt)*)?) => {{
        #[allow(unused_mut)]
        let mut cmd = $crate::Cmd::new(format!($prog));
        $crate::__private_extend_args!(cmd, $($($args)*)*);
        cmd
    }};
    ($prog:expr $(, $($args:tt)*)?) => {{
        #[allow(unused_mut)]
        let mut cmd = $crate::Cmd::new($prog);
        $crate::__private_extend_args!(cmd, $($($args)*)*);
        cmd
    }};
}

/// Constructs a command using [`OsString`] as string type.
#[macro_export]
macro_rules! cmd_os {
    ($($cmd:tt)*) => {{
        let cmd: $crate::Cmd::<::std::ffi::OsString> = $crate::cmd!($($cmd)*);
        cmd
    }};
}

/// Constructs a command using [`String`] as string type.
#[macro_export]
macro_rules! cmd_str {
    ($($cmd:tt)*) => {{
        let cmd: $crate::Cmd::<::std::string::String> = $crate::cmd!($($cmd)*);
        cmd
    }};
}

/// Indicates what to do with the output of a command.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Out {
    /// Discard the output.
    Discard,
    /// Inherit the output stream from the parent process.
    Inherit,
    /// Capture the output.
    Capture,
}

impl Out {
    fn stdio(&self) -> std::process::Stdio {
        match self {
            Out::Discard => std::process::Stdio::null(),
            Out::Inherit => std::process::Stdio::inherit(),
            Out::Capture => std::process::Stdio::piped(),
        }
    }
}

/// An input provided to a command.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum In {
    /// Do not provide any input, i.e., `/dev/null`.
    Null,
    /// Inherit the input stream from the parent process.
    Inherit,
    /// Provide the given bytes as input.
    Bytes(Vec<u8>),
}

impl In {
    fn stdio(&self) -> std::process::Stdio {
        match self {
            In::Null => std::process::Stdio::null(),
            In::Inherit => std::process::Stdio::inherit(),
            In::Bytes(_) => std::process::Stdio::piped(),
        }
    }
}

impl From<&[u8]> for In {
    fn from(value: &[u8]) -> Self {
        Self::Bytes(value.to_vec())
    }
}

impl From<&str> for In {
    fn from(value: &str) -> Self {
        value.as_bytes().into()
    }
}

impl From<&String> for In {
    fn from(value: &String) -> Self {
        value.as_bytes().into()
    }
}

impl From<String> for In {
    fn from(value: String) -> Self {
        Self::Bytes(value.into())
    }
}

#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
struct VarsData<S: CmdString> {
    /// Indicates that all other environment variables shall be discarded.
    is_clean: bool,
    /// The values of the variables.
    values: HashMap<S, Option<S>>,
}

/// A set of environment variables.
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vars<S: CmdString = OsString>(Arc<VarsData<S>>);

impl<S: CmdString> Vars<S> {
    /// Constructs an empty set of environment variables.
    pub fn new() -> Self {
        Self(Default::default())
    }

    /// Indicates that all other environment variables shall be discarded.
    pub fn is_clean(&self) -> bool {
        self.0.is_clean
    }

    /// The values of the environment variables.
    pub fn values(&self) -> impl Iterator<Item = (&S::Str, Option<&S::Str>)> {
        self.0
            .values
            .iter()
            .map(|(k, v)| (k.as_ref(), v.as_ref().map(AsRef::as_ref)))
    }

    fn data_mut(&mut self) -> &mut VarsData<S> {
        Arc::make_mut(&mut self.0)
    }

    /// Sets the value of an environment variable.
    pub fn set<N: AsRef<S::Str>, V: AsRef<S::Str>>(&mut self, name: N, value: V) -> &mut Self {
        self.data_mut()
            .values
            .insert(name.as_ref().to_owned(), Some(value.as_ref().to_owned()));
        self
    }

    /// Discards the value of an environment variable.
    pub fn unset<N: AsRef<S::Str>>(&mut self, name: N) -> &mut Self {
        self.data_mut()
            .values
            .insert(name.as_ref().to_owned(), None);
        self
    }

    /// Inherits the environment variable from the parent process.
    pub fn inherit<N: AsRef<S::Str>>(&mut self, name: N) -> Result<&mut Self, std::env::VarError> {
        let name = name.as_ref();
        let os_name = AsRef::<OsStr>::as_ref(name);
        match std::env::var(os_name) {
            Ok(value) => {
                self.set(name, S::from_str(value.as_str()));
            }
            Err(std::env::VarError::NotPresent) => {
                self.unset(name);
            }
            Err(error) => {
                return Err(error);
            }
        }
        Ok(self)
    }

    /// Resets a variable.
    pub fn reset<N: AsRef<S::Str>>(&mut self, name: N) -> &mut Self {
        self.data_mut().values.remove(name.as_ref());
        self
    }
}

/// Private auxiliary macro. **Not part of the public API!**
#[macro_export]
#[doc(hidden)]
macro_rules! __private_populate_vars {
    ($env_vars:ident,) => {};
    ($env_vars:ident, $name:ident = $value:literal $(, $($vars:tt)*)?) => {
        $env_vars.set(stringify!($name), format!($value));
        $crate::__private_populate_vars!($env_vars, $($($vars)*)*);
    };
    ($env_vars:ident, $name:ident = $value:expr $(, $($vars:tt)*)?) => {
        $env_vars.set(stringify!($name), $value);
        $crate::__private_populate_vars!($env_vars, $($($vars)*)*);
    };
    ($env_vars:ident, $name:literal = $value:literal $(, $($vars:tt)*)?) => {
        $env_vars.set(format!($name), format!($value));
        $crate::__private_populate_vars!($env_vars, $($($vars)*)*);
    };
    ($env_vars:ident, $name:literal = $value:expr $(, $($vars:tt)*)?) => {
        $env_vars.set(format!($name), $value);
        $crate::__private_populate_vars!($env_vars, $($($vars)*)*);
    };
}

/// Convenience macro for constructing sets of variables.
///
/// ```rust
/// # use xscript::{vars_os as vars};
/// vars! {
///     RUSTDOCFLAGS = "--cfg docsrs --cfg xscript_unstable",
///     RUSTFLAGS = "--cfg xscript_unstable",
/// };
/// ```
#[macro_export]
macro_rules! vars {
    ($($vars:tt)*) => {{
        #[allow(unused_mut)]
        let mut env_vars = $crate::Vars::new();
        $crate::__private_populate_vars!(env_vars, $($vars)*);
        env_vars
    }};
}

/// Constructs environment variables using [`OsString`] as string type.
#[macro_export]
macro_rules! vars_os {
    ($($vars:tt)*) => {{
        let vars: $crate::Vars<::std::ffi::OsString> = $crate::vars!($($vars)*);
        vars
    }};
}

/// Constructs environment variables using [`String`] as string type.
#[macro_export]
macro_rules! vars_str {
    ($($vars:tt)*) => {{
        let vars: $crate::Vars<::std::string::String> = $crate::vars!($($vars)*);
        vars
    }};
}

/// Output produced when running a command.
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct RunOutput {
    /// The exit code, if any.
    pub code: Option<i32>,
    /// The `stdout` output, if captured.
    pub stdout: Option<Vec<u8>>,
    /// The `stderr` output, if captured.
    pub stderr: Option<Vec<u8>>,
}

impl RunOutput {
    /// Constructs a new [`RunOutput`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the exit code of the command.
    pub fn with_code(mut self, code: i32) -> Self {
        self.code = Some(code);
        self
    }

    /// Sets the `stdout` output of the command.
    pub fn with_stdout(mut self, stdout: Vec<u8>) -> Self {
        self.stdout = Some(stdout);
        self
    }

    /// Sets the `stderr` output of the command.
    pub fn with_stderr(mut self, stderr: Vec<u8>) -> Self {
        self.stderr = Some(stderr);
        self
    }

    /// Tries to transform the `stdout` output to a string.
    fn try_into_stdout_str(self) -> Result<String, RunErrorKind> {
        self.stdout
            .ok_or_else(|| "no `stdout` output found".into())
            .and_then(|stdout| {
                String::from_utf8(stdout).map_err(|_| "`stdout` output is not valid UTF-8".into())
            })
            .map(|mut stdout| {
                while stdout.ends_with(|c: char| c.is_whitespace()) {
                    stdout.pop();
                }
                stdout
            })
    }
}

/// Error running a command.
#[derive(Debug)]
pub struct RunError<S: CmdString> {
    /// The command that failed.
    cmd: Cmd<S>,
    /// The kind of error.
    kind: RunErrorKind,
}

impl<S: CmdString> RunError<S> {
    /// Creates a new [`RunError`].
    pub fn new(cmd: Cmd<S>, kind: RunErrorKind) -> Self {
        Self { cmd, kind }
    }

    /// Transforms a [`RunErrorKind`] of a closure to [`RunError`].
    pub fn catch<F, U>(cmd: &Cmd<S>, func: F) -> RunResult<U, S>
    where
        F: FnOnce() -> Result<U, RunErrorKind>,
    {
        func().map_err(|kind| RunError::new(cmd.clone(), kind))
    }

    /// Transforms a [`RunErrorKind`] of a closure to [`RunError`].
    #[cfg(feature = "async")]
    #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
    pub async fn catch_async<F, U, Fut>(cmd: &Cmd<S>, func: F) -> RunResult<U, S>
    where
        Fut: std::future::Future<Output = Result<U, RunErrorKind>>,
        F: FnOnce() -> Fut,
    {
        func()
            .await
            .map_err(|kind| RunError::new(cmd.clone(), kind))
    }
}

impl<S: CmdString> std::error::Error for RunError<S> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.kind {
            RunErrorKind::Failed { .. } => None,
            RunErrorKind::Io(error) => Some(error),
            RunErrorKind::Other(error) => Some(error.as_ref()),
            RunErrorKind::Custom(_) => None,
        }
    }
}

impl<S: CmdString> Display for RunError<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("error running command `{}`: ", self.cmd))?;
        match &self.kind {
            RunErrorKind::Failed(output) => {
                f.write_str("command failed with non-zero exit code")?;
                if let Some(code) = output.code {
                    f.write_char(' ')?;
                    Display::fmt(&code, f)?;
                }
                if let Some(stderr) = &output.stderr {
                    f.write_str("\n=== STDERR ===\n")?;
                    if let Ok(stderr) = std::str::from_utf8(stderr) {
                        f.write_str(stderr.trim())?;
                    } else {
                        f.write_str("<invalid utf-8>")?;
                    }
                }
            }
            RunErrorKind::Other(error) => {
                Display::fmt(&error, f)?;
            }
            RunErrorKind::Io(error) => {
                Display::fmt(&error, f)?;
            }
            RunErrorKind::Custom(message) => {
                Display::fmt(&message, f)?;
            }
        }
        Ok(())
    }
}

/// The result of running a command.
pub type RunResult<T, S> = Result<T, RunError<S>>;

/// Error while running a command.
#[derive(Debug)]
pub enum RunErrorKind {
    /// The command failed with a non-zero exit code.
    Failed(RunOutput),
    /// There was an [`io::Error`].
    Io(io::Error),
    /// A custom error message.
    Custom(String),
    /// The was some other error.
    Other(Box<dyn 'static + Sync + Send + std::error::Error>),
}

impl RunErrorKind {
    /// Constructs a [`RunErrorKind`] from some error.
    pub fn other<E: 'static + Sync + Send + std::error::Error>(error: E) -> Self {
        Self::Other(Box::new(error))
    }
}

impl From<RunOutput> for RunErrorKind {
    fn from(value: RunOutput) -> Self {
        Self::Failed(value)
    }
}

impl From<io::Error> for RunErrorKind {
    fn from(value: std::io::Error) -> Self {
        Self::Io(value)
    }
}

impl From<&str> for RunErrorKind {
    fn from(value: &str) -> Self {
        Self::Custom(value.to_owned())
    }
}

impl From<String> for RunErrorKind {
    fn from(value: String) -> Self {
        Self::Custom(value)
    }
}

/// Runs a command in a given environment (see [`Run::run`]).
#[macro_export]
macro_rules! run {
    ($env:expr, [$($cmd_args:tt)*] $($cmd_methods:tt)*) => {
        $env.run($crate::cmd!($($cmd_args)*)$($cmd_methods)*)
    };
    ([$($cmd_args:tt)*] $($cmd_methods:tt)*) => {
        $crate::ParentEnv.run($crate::cmd!($($cmd_args)*)$($cmd_methods)*)
    };
}

/// Runs a command in a given environment reading `stdout` as a string (see
/// [`Run::read_str`]).
#[macro_export]
macro_rules! read_str {
    ($env:expr, [$($cmd_args:tt)*] $($cmd_methods:tt)*) => {
        $env.read_str($crate::cmd!($($cmd_args)*)$($cmd_methods)*)
    };
    ([$($cmd_args:tt)*] $($cmd_methods:tt)*) => {
        $crate::ParentEnv.read_str($crate::cmd!($($cmd_args)*)$($cmd_methods)*)
    };
}

/// Runs a command in a given environment reading `stdout` as bytes (see
/// [`Run::read_bytes`])).
#[macro_export]
macro_rules! read_bytes {
    ($env:expr, [$($cmd_args:tt)*] $($cmd_methods:tt)*) => {
        $env.read_bytes($crate::cmd!($($cmd_args)*)$($cmd_methods)*)
    };
    ([$($cmd_args:tt)*] $($cmd_methods:tt)*) => {
        $crate::ParentEnv.read_bytes($crate::cmd!($($cmd_args)*)$($cmd_methods)*)
    };
}

/// Shared inner data of an execution environment.
#[derive(Debug, Clone)]
struct EnvInner {
    /// The working directory of the environment.
    cwd: PathBuf,
    /// The environment variables of the environment, if any.
    vars: Vars<OsString>,
    /// The default input provided to commands, if any.
    default_stdin: In,
    /// Indicates what to do with the `stdout` output by default.
    default_stdout: Out,
    /// Indicates what to do with the `stderr` output by default.
    default_stderr: Out,
    /// Replay any captured `stdout` output.
    replay_stdout: bool,
    /// Replay any captured `stderr` output.
    replay_stderr: bool,
    /// Echo commands before they are executed.
    echo_commands: bool,
}

impl EnvInner {
    fn new(cwd: PathBuf) -> Self {
        Self {
            cwd,
            vars: Vars::new(),
            default_stdin: In::Null,
            default_stdout: Out::Capture,
            default_stderr: Out::Capture,
            replay_stdout: false,
            replay_stderr: true,
            echo_commands: false,
        }
    }
}

/// Execution environment of the parent process.
pub struct ParentEnv;

impl Run<OsString> for ParentEnv {
    fn run(&self, cmd: Cmd<OsString>) -> Result<RunOutput, RunError<OsString>> {
        // TODO: This is inefficient, we should factor out the actual launch code.
        let env = RunError::catch(&cmd, || LocalEnv::current_dir().map_err(RunErrorKind::from))?;
        Run::run(&env, cmd)
    }
}

/// A local execution environment.
#[derive(Debug, Clone)]
pub struct LocalEnv(Arc<EnvInner>);

impl LocalEnv {
    /// Creates an execution environment with the given working directory.
    pub fn new<P: AsRef<Path>>(cwd: P) -> Self {
        Self(Arc::new(EnvInner::new(cwd.as_ref().to_path_buf())))
    }

    /// Creates an execution environment with the current working directory.
    pub fn current_dir() -> Result<Self, io::Error> {
        Ok(Self::new(std::env::current_dir()?))
    }

    fn inner_mut(&mut self) -> &mut EnvInner {
        Arc::make_mut(&mut self.0)
    }

    /// The working directory of the environment.
    pub fn cwd(&self) -> &Path {
        &self.0.cwd
    }

    /// Sets the working directory of the environment.
    pub fn set_cwd<P: AsRef<Path>>(&mut self, cwd: P) -> &mut Self {
        self.inner_mut().cwd = cwd.as_ref().to_path_buf();
        self
    }

    /// Sets the working directory of the environment.
    pub fn with_cwd<P: AsRef<Path>>(mut self, cwd: P) -> Self {
        self.set_cwd(cwd);
        self
    }

    /// The environment variables of the environment.
    pub fn vars(&self) -> &Vars {
        &self.0.vars
    }

    /// Sets the environment variables of the environment.
    pub fn set_vars(&mut self, vars: Vars) -> &mut Self {
        self.inner_mut().vars = vars;
        self
    }

    /// Sets the environment variables of the environment.
    pub fn with_vars(mut self, vars: Vars) -> Self {
        self.set_vars(vars);
        self
    }

    /// Sets an environment variable.
    pub fn set_var<N: AsRef<OsStr>, V: AsRef<OsStr>>(&mut self, name: N, value: V) -> &mut Self {
        self.inner_mut().vars.set(name, value);
        self
    }

    /// Sets an environment variable.
    pub fn with_var<N: AsRef<OsStr>, V: AsRef<OsStr>>(mut self, name: N, value: V) -> Self {
        self.set_var(name, value);
        self
    }

    /// The default `stdin` input to provide to commands.
    pub fn default_stdin(&self) -> &In {
        &self.0.default_stdin
    }

    /// Sets the default `stdin` input to provide to commands.
    pub fn with_default_stdin(mut self, stdin: In) -> Self {
        self.inner_mut().default_stdin = stdin;
        self
    }

    /// Indicates what to do with the `stdout` output of commands by default.
    pub fn default_stdout(&self) -> &Out {
        &self.0.default_stdout
    }

    /// Sets what to do with the `stdout` output of commands by default.
    pub fn with_default_stdout(mut self, stdout: Out) -> Self {
        self.inner_mut().default_stdout = stdout;
        self
    }

    /// Indicates what to do with the `stderr` output of commands by default.
    pub fn default_stderr(&self) -> &Out {
        &self.0.default_stderr
    }

    // Sets what to do with the `stderr` output of commands by default.
    pub fn with_default_stderr(mut self, stderr: Out) -> Self {
        self.inner_mut().default_stderr = stderr;
        self
    }

    /// Enables the echoing of commands.
    pub fn with_echo(mut self) -> Self {
        self.inner_mut().echo_commands = true;
        self
    }

    /// Disables the echoing of commands.
    pub fn without_echo(mut self) -> Self {
        self.inner_mut().echo_commands = false;
        self
    }

    /// Changes the working directory of the environment.
    pub fn change_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<&mut Self, io::Error> {
        Ok(self.set_cwd(self.resolve_path(path).canonicalize()?))
    }

    /// Resolves a path relative to the working directory of the environment.
    pub fn resolve_path<P: AsRef<Path>>(&self, path: P) -> PathBuf {
        self.0.cwd.join(path.as_ref())
    }

    fn resolve_prog<'p>(&self, prog: &'p OsStr) -> Cow<'p, Path> {
        if prog.to_string_lossy().contains(std::path::is_separator) {
            Cow::Owned(self.resolve_path(prog))
        } else {
            Cow::Borrowed(Path::new(prog))
        }
    }

    fn echo_cmd(&self, cmd: &Cmd<OsString>) {
        if self.0.echo_commands {
            eprintln!("+ {cmd}");
        }
    }

    fn command(&self, cmd: &Cmd<OsString>) -> Command {
        let mut command = Command::new(&*self.resolve_prog(cmd.prog()));
        command.args(cmd.args());
        if let Some(cwd) = cmd.cwd() {
            command.current_dir(self.resolve_path(cwd));
        } else {
            command.current_dir(&self.0.cwd);
        }
        // Populate the environment variables.
        if self.vars().is_clean() || cmd.vars().map(|vars| vars.is_clean()).unwrap_or(false) {
            command.env_clear();
        }
        update_vars(&mut command, self.vars());
        if let Some(vars) = cmd.vars() {
            update_vars(&mut command, vars);
        }
        // Configure IO.
        command.stdin(cmd.stdin().unwrap_or_else(|| self.default_stdin()).stdio());
        command.stdout(
            cmd.stdout()
                .unwrap_or_else(|| self.default_stdout())
                .stdio(),
        );
        command.stderr(
            cmd.stderr()
                .unwrap_or_else(|| self.default_stderr())
                .stdio(),
        );
        command
    }
}

fn update_vars(command: &mut Command, vars: &Vars) {
    for (name, value) in vars.values() {
        if let Some(value) = value {
            command.env(name, value);
        } else {
            command.env_remove(name);
        }
    }
}

/// Trait for running commands in an execution environment.
pub trait Run<S: CmdString> {
    /// Runs a command returning its output.
    fn run(&self, cmd: Cmd<S>) -> Result<RunOutput, RunError<S>>;

    /// Runs a command returning its `stdout` output as a string.
    fn read_str(&self, cmd: Cmd<S>) -> Result<String, RunError<S>> {
        let cmd = cmd.with_stdout(Out::Capture);
        self.run(cmd.clone())
            .and_then(|output| RunError::catch(&cmd, || output.try_into_stdout_str()))
    }

    /// Runs a command returning its `stderr` output as a string.
    fn read_bytes(&self, cmd: Cmd<S>) -> Result<Vec<u8>, RunError<S>> {
        let cmd = cmd.with_stdout(Out::Capture);
        self.run(cmd).map(|output| output.stdout.unwrap())
    }
}

impl Run<OsString> for LocalEnv {
    fn run(&self, cmd: Cmd<OsString>) -> Result<RunOutput, RunError<OsString>> {
        RunError::catch(&cmd, || {
            use io::Write;

            let cmd = &cmd;

            let mut command = self.command(cmd);
            self.echo_cmd(cmd);
            let mut child = command.spawn()?;
            let capture_stdout = child.stdout.is_some();
            let capture_stderr = child.stderr.is_some();
            let child_output = std::thread::scope(|scope| {
                if let Some(mut child_stdin) = child.stdin.take() {
                    scope.spawn(move || {
                        if let Some(In::Bytes(stdin)) = cmd.stdin() {
                            let _ = child_stdin.write_all(stdin);
                            let _ = child_stdin.flush();
                        }
                    });
                }
                child.wait_with_output()
            })?;
            if self.0.replay_stdout {
                let _ = io::stdout().write_all(&child_output.stdout);
            }
            if self.0.replay_stderr {
                let _ = io::stderr().write_all(&child_output.stderr);
            }
            let output = RunOutput {
                code: child_output.status.code(),
                stdout: if capture_stdout {
                    Some(child_output.stdout)
                } else {
                    None
                },
                stderr: if capture_stderr {
                    Some(child_output.stderr)
                } else {
                    None
                },
            };
            if child_output.status.success() || cmd.may_fail() {
                Ok(output)
            } else {
                Err(RunErrorKind::Failed(output))
            }
        })
    }
}

#[cfg(feature = "async")]
type BoxedFuture<'fut, T> = std::pin::Pin<Box<dyn 'fut + std::future::Future<Output = T>>>;

/// Trait for running commands asynchronously in an execution environment.
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
#[cfg(feature = "async")]
pub trait RunAsync<S: CmdString> {
    fn run(&self, cmd: Cmd<S>) -> BoxedFuture<RunResult<RunOutput, S>>;

    fn read_str(&self, cmd: Cmd<S>) -> BoxedFuture<RunResult<String, S>> {
        // Force capture the output.
        let cmd = cmd.with_stdout(Out::Capture);
        Box::pin(async move {
            self.run(cmd.clone())
                .await
                .and_then(|output| RunError::catch(&cmd, || output.try_into_stdout_str()))
        })
    }

    fn read_bytes(&self, cmd: Cmd<S>) -> BoxedFuture<Result<Vec<u8>, RunError<S>>> {
        let cmd = cmd.with_stdout(Out::Capture);
        Box::pin(async move { self.run(cmd).await.map(|output| output.stdout.unwrap()) })
    }
}

#[cfg(test)]
mod tests {
    use std::error::Error;

    use crate::{write_escaped, Run};

    #[test]
    fn test_write_escaped() {
        fn escape(string: &str) -> String {
            let mut buf = String::new();
            write_escaped(&mut buf, string).unwrap();
            buf
        }
        assert_eq!(escape("xyz"), "xyz");
        assert_eq!(escape("xyz abc"), "\"xyz abc\"");
        assert_eq!(escape("x\"yz\""), "x\\\"yz\\\"");
        assert_eq!(escape("\\x"), "\\\\x");
    }

    #[test]
    #[cfg(target_family = "unix")]
    fn test_io() -> Result<(), Box<dyn Error>> {
        use crate::LocalEnv;

        let env = LocalEnv::current_dir()?;
        assert!(read_str!(env, ["cat"])?.is_empty());
        assert_eq!(
            read_str!(env, ["cat"].with_stdin("Hello World!"))?,
            "Hello World!"
        );
        Ok(())
    }
}