Skip to main content

isoloom_render/
powershell.rs

1//! PowerShell, as a Windows machine's set-up is written: string literals that hold any text,
2//! and the script that unpacks the project.
3
4/// Unpacks the project's tar.gz on Windows (`$archive`, `$dest`, `$strip` set before it): no
5/// `tar.exe` before Windows Server 2019.
6pub const UNTAR: &str = include_str!("powershell/untar.ps1");
7
8/// A PowerShell single-quoted string literal reading as `s`: nothing inside is expanded.
9/// PowerShell takes the typographic single quotes (U+2018 to U+201B) as quotes too, so each of
10/// those is doubled as well as `'`.
11#[must_use]
12pub fn quote(s: &str) -> String {
13    let mut out = String::with_capacity(s.len() + 2);
14    out.push('\'');
15    for c in s.chars() {
16        if is_quote(c) {
17            out.push(c);
18        }
19        out.push(c);
20    }
21    out.push('\'');
22    out
23}
24
25/// Whether PowerShell reads `c` as a single quote.
26fn is_quote(c: char) -> bool {
27    matches!(c, '\'' | '\u{2018}' | '\u{2019}' | '\u{201A}' | '\u{201B}')
28}
29
30#[cfg(test)]
31mod tests {
32    use super::{UNTAR, quote};
33
34    #[test]
35    fn every_kind_of_single_quote_is_doubled() {
36        assert_eq!(quote("it's"), "'it''s'");
37        assert_eq!(
38            quote("a\u{2018}b\u{2019}c\u{201A}d\u{201B}e"),
39            "'a\u{2018}\u{2018}b\u{2019}\u{2019}c\u{201A}\u{201A}d\u{201B}\u{201B}e'"
40        );
41        assert_eq!(quote("$x `n \""), "'$x `n \"'");
42    }
43
44    #[test]
45    fn the_untar_script_reads_the_archive_it_is_given() {
46        assert!(UNTAR.contains("$archive") && UNTAR.contains("$dest") && UNTAR.contains("$strip"));
47    }
48}