Replace common functionality with rust implementation

This commit is contained in:
2025-09-03 17:06:13 +02:00
parent d0d162f3e9
commit d31d39473b
38 changed files with 2871 additions and 204 deletions

28
mgr/src/env.rs Normal file
View File

@@ -0,0 +1,28 @@
use std::{env, ffi::OsString};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error(
"env variable \"{name}\" is not valid unicode: \"{value}\"",
value = value.to_string_lossy()
)]
EnvNotUnicode { name: &'static str, value: OsString },
#[error("env variable \"{name}\" not found")]
EnvNotFound { name: &'static str },
}
pub(crate) fn get(var: &'static str) -> Result<Option<String>, Error> {
match env::var(var) {
Ok(value) => Ok(Some(value)),
Err(e) => match e {
env::VarError::NotPresent => Ok(None),
env::VarError::NotUnicode(value) => Err(Error::EnvNotUnicode { name: var, value }),
},
}
}
pub(crate) fn require(var: &'static str) -> Result<String, Error> {
get(var)?.ok_or(Error::EnvNotFound { name: var })
}