airlab_lib/
envs.rs

1use crate::b64::b64u_decode;
2use std::env;
3use std::str::FromStr;
4
5pub fn get_env(name: &'static str) -> Result<String> {
6    env::var(name).map_err(|_| Error::MissingEnv(name))
7}
8
9pub fn get_env_parse<T: FromStr>(name: &'static str) -> Result<T> {
10    let val = get_env(name)?;
11    val.parse::<T>().map_err(|_| Error::WrongFormat(name))
12}
13
14pub fn get_env_b64u_as_u8s(name: &'static str) -> Result<Vec<u8>> {
15    b64u_decode(&get_env(name)?).map_err(|_| Error::WrongFormat(name))
16}
17
18pub type Result<T> = core::result::Result<T, Error>;
19
20#[derive(Debug)]
21pub enum Error {
22    MissingEnv(&'static str),
23    WrongFormat(&'static str),
24}
25
26impl core::fmt::Display for Error {
27    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
28        write!(fmt, "{self:?}")
29    }
30}
31
32impl std::error::Error for Error {}