Skip to main content

stuffr_cli/compat/compress/
flavour.rs

1//! Which reference the compress family behaves as, and how a run picks it.
2
3use std::ffi::OsString;
4
5/// Which reference the family behaves as.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Flavour {
8    /// Apple's `/usr/bin/compress` (`file_cmds-487.0.1`): the default on
9    /// macOS and the BSDs.
10    Apple,
11    /// ncompress 5.0 as Ubuntu 24.04 ships it (`5.0-1`): the default
12    /// elsewhere.
13    Ncompress,
14}
15
16/// The flavour a build uses when `STUFFR_COMPRESS_FLAVOR` is unset or
17/// empty: Apple on Apple platforms and the BSDs (the cfg of
18/// `gzip::default_flavour`), ncompress everywhere else, Windows included.
19pub fn default_flavour() -> Flavour {
20    if cfg!(any(
21        target_vendor = "apple",
22        target_os = "freebsd",
23        target_os = "netbsd",
24        target_os = "openbsd",
25        target_os = "dragonfly"
26    )) {
27        Flavour::Apple
28    } else {
29        Flavour::Ncompress
30    }
31}
32
33/// The flavour `STUFFR_COMPRESS_FLAVOR`'s value selects: `apple` or
34/// `ncompress`, the default when unset or empty, and `Err(value)` for
35/// anything else (another case, a blank).
36pub(crate) fn flavour_from(value: Option<OsString>) -> Result<Flavour, OsString> {
37    match value {
38        None => Ok(default_flavour()),
39        Some(v) if v.is_empty() => Ok(default_flavour()),
40        Some(v) if v == "apple" => Ok(Flavour::Apple),
41        Some(v) if v == "ncompress" => Ok(Flavour::Ncompress),
42        Some(v) => Err(v),
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn flavour_var_selects_and_rejects() {
52        let d = default_flavour();
53        assert_eq!(flavour_from(None), Ok(d));
54        assert_eq!(flavour_from(Some("".into())), Ok(d));
55        assert_eq!(flavour_from(Some("apple".into())), Ok(Flavour::Apple));
56        assert_eq!(
57            flavour_from(Some("ncompress".into())),
58            Ok(Flavour::Ncompress)
59        );
60        for bad in ["Apple", "NCOMPRESS", " apple", "bsd", "ubuntu", "gnu"] {
61            assert_eq!(flavour_from(Some(bad.into())), Err(bad.into()));
62        }
63    }
64
65    #[test]
66    fn default_flavour_is_apple_on_macos() {
67        let apple = cfg!(any(
68            target_os = "macos",
69            target_os = "ios",
70            target_os = "freebsd",
71            target_os = "netbsd",
72            target_os = "openbsd",
73            target_os = "dragonfly"
74        ));
75        assert_eq!(
76            default_flavour(),
77            if apple {
78                Flavour::Apple
79            } else {
80                Flavour::Ncompress
81            }
82        );
83        if cfg!(target_os = "macos") {
84            assert_eq!(default_flavour(), Flavour::Apple);
85        }
86    }
87}