Commit 364209299d
Unsigned
Layout: unified · split
src/global/functions/envs.rs added +27
| @@ -0,0 +1,27 @@ | |||
| 1 | use std::env; | ||
| 2 | |||
| 3 | /// removes all environment variables in a vector | ||
| 4 | pub fn clear_envs(modified: &mut Vec<(String, String)>) { | ||
| 5 | modified.iter().for_each(|(key, _)| env::remove_var(key)); | ||
| 6 | modified.clear(); | ||
| 7 | } | ||
| 8 | |||
| 9 | /// set all environment variables in an interator, while pushing changes into the modified vector | ||
| 10 | /// to keep track of changes | ||
| 11 | pub fn set_envs<T: Iterator<Item = (String, String)>>( | ||
| 12 | envs: T, | ||
| 13 | modified: &mut Vec<(String, String)>, | ||
| 14 | ) { | ||
| 15 | envs.for_each(|(key, value)| { | ||
| 16 | if !modified.iter().any(|(k, _)| k == &key) { | ||
| 17 | modified.push((key.clone(), value.clone())); | ||
| 18 | } | ||
| 19 | env::set_var(key, value); | ||
| 20 | }); | ||
| 21 | } | ||
| 22 | |||
| 23 | /// replace env placeholder in a string with the value of the env | ||
| 24 | pub fn apply_envs(mut s: String) -> String { | ||
| 25 | env::vars().for_each(|(key, value)| s = s.replace(&format!("${{{key}}}"), &value)); | ||
| 26 | s | ||
| 27 | } | ||
src/global/structs/state_env.rs added +10
| @@ -0,0 +1,10 @@ | |||
| 1 | use typemap::Key; | ||
| 2 | |||
| 3 | /// holds all modified envs that belongs to the page | ||
| 4 | /// stores (key, value) pairs | ||
| 5 | #[derive(Default, Clone)] | ||
| 6 | pub struct StateEnvs(pub Vec<(String, String)>); | ||
| 7 | |||
| 8 | impl Key for StateEnvs { | ||
| 9 | type Value = Self; | ||
| 10 | } | ||