This repository has been archived by the owner on Dec 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove lazy_static and use sync::LazyLock
Also put macro in an own file
- Loading branch information
Showing
4 changed files
with
134 additions
and
136 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,71 @@ | ||
//////////////////////////////////////////////////////////////////////// | ||
// Macros | ||
//////////////////////////////////////////////////////////////////////// | ||
/// Help setting up static variables based on user environment. | ||
/// | ||
/// We allow the user to configure certain properties/behaviours of tm | ||
/// using environment variables. To reduce boilerplate in code, we use a | ||
/// macro for setting them. We use [mod@`lazy_static`] to define them as | ||
/// global variables, so they are available throughout the whole program - | ||
/// they aren't going to change during runtime, ever, anyways. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # fn main() { | ||
/// static ref TMPDIR: String = fromenvstatic!(asString "TMPDIR", "/tmp"); | ||
/// static ref TMSORT: bool = fromenvstatic!(asBool "TMSORT", true); | ||
/// static ref TMWIN: u8 = fromenvstatic!(asU32 "TMWIN", 1); | ||
/// # } | ||
/// ``` | ||
macro_rules! fromenvstatic { | ||
(asString $envvar:literal, $default:expr) => { | ||
match env::var($envvar) { | ||
Ok(val) => val, | ||
Err(_) => $default.to_string(), | ||
} | ||
}; | ||
(asBool $envvar:literal, $default:literal) => { | ||
match env::var($envvar) { | ||
Ok(val) => match val.to_ascii_lowercase().as_str() { | ||
"true" => true, | ||
"false" => false, | ||
&_ => { | ||
// Test run as "cargo test -- --nocapture" will print this | ||
if cfg!(test) { | ||
println!( | ||
"Variable {} expects true or false, not {}, assuming {}", | ||
$envvar, val, $default | ||
); | ||
} | ||
error!( | ||
"Variable {} expects true or false, not {}, assuming {}", | ||
$envvar, val, $default | ||
); | ||
return $default; | ||
} | ||
}, | ||
Err(_) => $default, | ||
} | ||
}; | ||
(asU32 $envvar:literal, $default:literal) => { | ||
match env::var($envvar) { | ||
Ok(val) => { | ||
return val.parse::<u32>().unwrap_or_else(|err| { | ||
if cfg!(test) { | ||
println!( | ||
"Couldn't parse variable {} (value: {}) as number (error: {}), assuming {}", | ||
$envvar, val, err, $default | ||
); | ||
} | ||
error!( | ||
"Couldn't parse variable {} (value: {}) as number (error: {}), assuming {}", | ||
$envvar, val, err, $default | ||
); | ||
$default | ||
}); | ||
} | ||
Err(_) => $default, | ||
} | ||
}; | ||
} |
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