Line data Source code
1 : //! Wrapper around `std::env::var` for parsing environment variables.
2 :
3 : use std::{fmt::Display, str::FromStr};
4 :
5 1263259 : pub fn var<V, E>(varname: &str) -> Option<V>
6 1263259 : where
7 1263259 : V: FromStr<Err = E>,
8 1263259 : E: Display,
9 1263259 : {
10 1263259 : match std::env::var(varname) {
11 1262260 : Ok(s) => Some(
12 1262260 : s.parse()
13 1262260 : .map_err(|e| format!("failed to parse env var {varname}: {e:#}"))
14 1262260 : .unwrap(),
15 1262260 : ),
16 999 : Err(std::env::VarError::NotPresent) => None,
17 : Err(std::env::VarError::NotUnicode(_)) => {
18 0 : panic!("env var {varname} is not unicode")
19 : }
20 : }
21 1263259 : }
|