1use std::env;
2use std::process::Command;
3
4use camino::{Utf8Path, Utf8PathBuf};
5pub(crate) use shim_utils::ArgFileCommand;
6
7#[cfg(test)]
8mod tests;
9
10pub(crate) trait Utf8PathBufExt {
11 fn with_extra_extension(&self, extension: &str) -> Utf8PathBuf;
13}
14
15impl Utf8PathBufExt for Utf8PathBuf {
16 fn with_extra_extension(&self, extension: &str) -> Utf8PathBuf {
17 if extension.is_empty() {
18 self.clone()
19 } else {
20 let mut fname = self.file_name().unwrap().to_string();
21 if !extension.starts_with('.') {
22 fname.push_str(".");
23 }
24 fname.push_str(extension);
25 self.with_file_name(fname)
26 }
27 }
28}
29
30pub(crate) fn dylib_env_var() -> &'static str {
32 if cfg!(any(windows, target_os = "cygwin")) {
33 "PATH"
34 } else if cfg!(target_vendor = "apple") {
35 "DYLD_LIBRARY_PATH"
36 } else if cfg!(target_os = "haiku") {
37 "LIBRARY_PATH"
38 } else if cfg!(target_os = "aix") {
39 "LIBPATH"
40 } else {
41 "LD_LIBRARY_PATH"
42 }
43}
44
45pub(crate) fn add_dylib_path(
48 cmd: &mut Command,
49 paths: impl Iterator<Item = impl Into<std::path::PathBuf>>,
50) {
51 let path_env = env::var_os(dylib_env_var());
52 let old_paths = path_env.as_ref().map(env::split_paths);
53 let new_paths = paths.map(Into::into).chain(old_paths.into_iter().flatten());
54 cmd.env(dylib_env_var(), env::join_paths(new_paths).unwrap());
55}
56
57pub(crate) fn copy_dir_all(src: &Utf8Path, dst: &Utf8Path) -> std::io::Result<()> {
58 std::fs::create_dir_all(dst.as_std_path())?;
59 for entry in std::fs::read_dir(src.as_std_path())? {
60 let entry = entry?;
61 let path = Utf8PathBuf::try_from(entry.path()).unwrap();
62 let file_name = path.file_name().unwrap();
63 let ty = entry.file_type()?;
64 if ty.is_dir() {
65 copy_dir_all(&path, &dst.join(file_name))?;
66 } else {
67 std::fs::copy(path.as_std_path(), dst.join(file_name).as_std_path())?;
68 }
69 }
70 Ok(())
71}
72
73macro_rules! static_regex {
74 ($re:literal) => {{
75 static RE: ::std::sync::OnceLock<::regex::Regex> = ::std::sync::OnceLock::new();
76 RE.get_or_init(|| ::regex::Regex::new($re).unwrap())
77 }};
78}
79pub(crate) use static_regex;
80
81macro_rules! string_enum {
82 (
83 $(#[$meta:meta])*
84 $vis:vis enum $name:ident {
85 $(
86 $(#[$variant_meta:meta])*
87 $variant:ident => $repr:expr,
88 )*
89 }
90 ) => {
91 $(#[$meta])*
92 $vis enum $name {
93 $(
94 $(#[$variant_meta])*
95 $variant,
96 )*
97 }
98
99 impl $name {
100 #[allow(dead_code)]
101 $vis const VARIANTS: &'static [Self] = &[
102 $( Self::$variant, )*
103 ];
104 #[allow(dead_code)]
105 $vis const STR_VARIANTS: &'static [&'static str] = &[
106 $( Self::$variant.to_str(), )*
107 ];
108
109 $vis const fn to_str(&self) -> &'static str {
110 match self {
111 $( Self::$variant => $repr, )*
112 }
113 }
114 }
115
116 impl ::std::fmt::Display for $name {
117 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
118 ::std::fmt::Display::fmt(self.to_str(), f)
119 }
120 }
121
122 impl ::std::str::FromStr for $name {
123 type Err = String;
124
125 fn from_str(s: &str) -> Result<Self, Self::Err> {
126 match s {
127 $( $repr => Ok(Self::$variant), )*
128 _ => Err(format!(concat!("unknown `", stringify!($name), "` variant: `{}`"), s)),
129 }
130 }
131 }
132 }
133}
134
135pub(crate) use string_enum;