#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Shell {
Posix,
Windows,
}
pub(crate) const PLACEHOLDER: &str = "FILE";
impl Shell {
pub(crate) const HOST: Self = if cfg!(windows) {
Self::Windows
} else {
Self::Posix
};
fn shown(self, path: &str) -> &str {
match self {
Self::Windows if path.contains(['$', '%', '`']) => PLACEHOLDER,
_ => path,
}
}
pub(crate) fn placeholder_note(self, path: &str) -> &'static str {
if self.shown(path) == PLACEHOLDER {
" The path holds a dollar sign, a percent sign or a backtick, which \
PowerShell or cmd.exe would expand inside quotes, so the command says \
FILE where the path goes."
} else {
""
}
}
pub(crate) fn quote(self, path: &str) -> String {
match self {
Self::Posix => format!("'{}'", path.replace('\'', r"'\''")),
Self::Windows => format!("\"{path}\""),
}
}
pub(crate) fn gunzip(self, path: &str) -> String {
let path = self.shown(path);
match self {
Self::Posix => format!("gunzip -k {}", self.quote(path)),
Self::Windows => format!(
"python -c \"import gzip,shutil,sys;p=sys.argv[1];shutil.copyfileobj(gzip.open(p),open(p.removesuffix('.gz'),'xb'))\" {}",
self.quote(path)
),
}
}
pub(crate) fn unzstd(self, path: &str) -> String {
let path = self.shown(path);
format!("zstd -d {}", self.quote(path))
}
pub(crate) fn unzip(self, path: &str) -> String {
let path = self.shown(path);
match self {
Self::Posix => format!("unzip -n {}", self.quote(path)),
Self::Windows => format!(
"python -m zipfile -e {} {}",
self.quote(path),
self.quote(&format!("{path}.contents"))
),
}
}
pub(crate) fn reencode(self, path: &str, encoding: &str) -> String {
let encoding = match encoding {
"latin-1" => "ISO-8859-1",
other => other,
};
let path = self.shown(path);
let output = format!("{path}.utf8.csv");
match self {
Self::Posix => format!(
"(set -C; iconv -f {encoding} -t UTF-8 {} > {})",
self.quote(path),
self.quote(&output)
),
Self::Windows => format!(
"python -c \"import shutil,sys;p=sys.argv[1];shutil.copyfileobj(open(p,encoding='{encoding}',newline=''),open(p+'.utf8.csv','x',encoding='utf-8',newline=''))\" {}",
self.quote(path)
),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const PATH: &str = "hint dir/o'brien.csv";
#[test]
fn posix_quotes_close_and_reopen_around_an_apostrophe() {
assert_eq!(Shell::Posix.quote(PATH), r"'hint dir/o'\''brien.csv'");
assert_eq!(
Shell::Posix.gunzip("a b/o'b.csv.gz"),
r"gunzip -k 'a b/o'\''b.csv.gz'"
);
}
#[test]
fn windows_quotes_are_double_and_leave_an_apostrophe_alone() {
assert_eq!(Shell::Windows.quote(PATH), "\"hint dir/o'brien.csv\"");
assert_eq!(
Shell::Windows.unzip(r"C:\a b\o'b.zip"),
r#"python -m zipfile -e "C:\a b\o'b.zip" "C:\a b\o'b.zip.contents""#
);
}
#[test]
fn windows_names_a_placeholder_for_a_path_its_shells_would_expand() {
for path in [
r"C:\d\$(Start-Process calc).gz",
r"C:\d\%TEMP%.zip",
r"C:\d\a`b.csv",
] {
for command in [
Shell::Windows.gunzip(path),
Shell::Windows.unzstd(path),
Shell::Windows.unzip(path),
Shell::Windows.reencode(path, "latin-1"),
] {
assert!(!command.contains(r"C:\d"), "{command}");
assert!(command.contains("\"FILE"), "{command}");
}
assert!(!Shell::Windows.placeholder_note(path).is_empty());
assert!(Shell::Posix.gunzip(path).contains(path));
assert!(Shell::Posix.placeholder_note(path).is_empty());
}
assert!(
Shell::Windows
.placeholder_note(r"C:\a b\o'b.zip")
.is_empty()
);
}
#[test]
fn latin_1_is_named_as_iconv_knows_it() {
for shell in [Shell::Posix, Shell::Windows] {
let command = shell.reencode("x.csv", "latin-1");
assert!(command.contains("ISO-8859-1"), "{command}");
assert!(!command.contains("latin-1"), "{command}");
let command = shell.reencode("x.csv", "windows-1252");
assert!(command.contains("windows-1252"), "{command}");
}
}
}