-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): windows_call_cmd_with_env
- Loading branch information
Showing
7 changed files
with
184 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ impl Opts { | |
} else { | ||
None | ||
}, | ||
windows_call_cmd_with_env: Default::default(), | ||
} | ||
.into(), | ||
)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod command; | ||
mod run; | ||
mod win_cmd; | ||
|
||
pub use command::*; | ||
pub use run::*; | ||
pub use win_cmd::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::{convert::TryFrom, fmt::Display}; | ||
|
||
pub struct InvalidEnvName(String); | ||
|
||
impl Display for InvalidEnvName { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "Invalid env name {}", self.0) | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug, Clone)] | ||
#[serde(try_from = "String")] | ||
#[serde(into = "String")] | ||
pub struct EnvName(String); | ||
|
||
impl TryFrom<String> for EnvName { | ||
type Error = InvalidEnvName; | ||
|
||
fn try_from(value: String) -> Result<Self, Self::Error> { | ||
if EnvName::check_str(&value) { | ||
Ok(Self(value)) | ||
} else { | ||
Err(InvalidEnvName(value)) | ||
} | ||
} | ||
} | ||
|
||
impl Into<String> for EnvName { | ||
fn into(self) -> String { | ||
self.0 | ||
} | ||
} | ||
|
||
impl EnvName { | ||
pub fn to_string(self) -> String { | ||
self.0 | ||
} | ||
|
||
pub fn check_str(s: &str) -> bool { | ||
s.len() > 0 | ||
&& s.chars().enumerate().all(|(i, c)| match c { | ||
'a'..='z' | 'A'..='Z' => true, | ||
'0'..='9' | '_' => i > 0, | ||
_ => false, | ||
}) | ||
} | ||
} | ||
|
||
#[non_exhaustive] | ||
#[derive(Deserialize, Serialize, Debug, Clone)] | ||
pub enum WindowsCallCmdWithEnv { | ||
Random, | ||
EnvName(EnvName), | ||
Disable, | ||
} | ||
|
||
#[cfg(windows)] | ||
fn get_random_env_name() -> String { | ||
use rand::{thread_rng, Rng}; | ||
|
||
let mut rng = thread_rng(); | ||
|
||
(0..8).map(|_| rng.gen_range('A'..='Z')).collect() | ||
} | ||
|
||
#[cfg(not(windows))] | ||
fn get_random_env_name() -> String { | ||
"RANDNAME".to_string() | ||
} | ||
|
||
impl WindowsCallCmdWithEnv { | ||
pub fn try_into_env_name(self) -> Option<String> { | ||
match self { | ||
Self::Random => Some(format!("RUNCC_WIN_CMD__{}", get_random_env_name())), | ||
Self::EnvName(env_name) => Some(env_name.to_string()), | ||
Self::Disable => None, | ||
} | ||
} | ||
} | ||
|
||
impl Default for WindowsCallCmdWithEnv { | ||
fn default() -> Self { | ||
Self::Random | ||
} | ||
} | ||
|
||
impl Display for WindowsCallCmdWithEnv { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
todo!() | ||
} | ||
} | ||
|
||
/// strictly validate env name [a-zA-Z][a-zA-Z0-9_]* | ||
#[cfg(test)] | ||
mod tests { | ||
use super::EnvName; | ||
|
||
#[test] | ||
fn test_is_valid_env_name() { | ||
assert!(EnvName::check_str("MY_ENV_0123_")); | ||
|
||
assert!(!EnvName::check_str("")); | ||
assert!(!EnvName::check_str(" ")); | ||
assert!(!EnvName::check_str("123abc")); | ||
assert!(!EnvName::check_str("_MY_ENV")); | ||
} | ||
} |