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
use crate::ext::anyhow::{bail, Context, Result};
use camino::Utf8PathBuf;
use std::borrow::Cow;

pub fn os_arch() -> Result<(&'static str, &'static str)> {
    let target_os = if cfg!(target_os = "windows") {
        "windows"
    } else if cfg!(target_os = "macos") {
        "macos"
    } else if cfg!(target_os = "linux") {
        "linux"
    } else {
        bail!("unsupported OS")
    };

    let target_arch = if cfg!(target_arch = "x86_64") {
        "x86_64"
    } else if cfg!(target_arch = "aarch64") {
        "aarch64"
    } else {
        bail!("unsupported target architecture")
    };
    Ok((target_os, target_arch))
}

pub fn is_linux_musl_env() -> bool {
    cfg!(target_os = "linux") && cfg!(target_env = "musl")
}

pub trait StrAdditions {
    fn with(&self, append: &str) -> String;
    fn pad_left_to(&self, len: usize) -> Cow<str>;
    /// returns the string as a canonical path (creates the dir if necessary)
    fn to_created_dir(&self) -> Result<Utf8PathBuf>;
}

impl StrAdditions for str {
    fn with(&self, append: &str) -> String {
        let mut s = self.to_string();
        s.push_str(append);
        s
    }

    fn pad_left_to(&self, len: usize) -> Cow<str> {
        let chars = self.chars().count();
        if chars < len {
            Cow::Owned(format!("{}{self}", " ".repeat(len - chars)))
        } else {
            Cow::Borrowed(self)
        }
    }

    fn to_created_dir(&self) -> Result<Utf8PathBuf> {
        let path = Utf8PathBuf::from(self);
        if !path.exists() {
            std::fs::create_dir_all(&path).context(format!("Could not create dir {self:?}"))?;
        }
        Ok(path)
    }
}

impl StrAdditions for String {
    fn with(&self, append: &str) -> String {
        let mut s = self.clone();
        s.push_str(append);
        s
    }

    fn pad_left_to(&self, len: usize) -> Cow<str> {
        self.as_str().pad_left_to(len)
    }

    fn to_created_dir(&self) -> Result<Utf8PathBuf> {
        self.as_str().to_created_dir()
    }
}