LCOV - code coverage report
Current view: top level - libs/utils/src - env.rs (source / functions) Coverage Total Hit
Test: 1b0a6a0c05cee5a7de360813c8034804e105ce1c.info Lines: 62.5 % 32 20
Test Date: 2025-03-12 00:01:28 Functions: 33.3 % 12 4

            Line data    Source code
       1              : //! Wrapper around `std::env::var` for parsing environment variables.
       2              : 
       3              : use std::fmt::Display;
       4              : use std::str::FromStr;
       5              : 
       6              : /// For types `V` that implement [`FromStr`].
       7          811 : pub fn var<V, E>(varname: &str) -> Option<V>
       8          811 : where
       9          811 :     V: FromStr<Err = E>,
      10          811 :     E: Display,
      11          811 : {
      12          811 :     match std::env::var(varname) {
      13            0 :         Ok(s) => Some(
      14            0 :             s.parse()
      15            0 :                 .map_err(|e| {
      16            0 :                     format!("failed to parse env var {varname} using FromStr::parse: {e:#}")
      17            0 :                 })
      18            0 :                 .unwrap(),
      19            0 :         ),
      20          811 :         Err(std::env::VarError::NotPresent) => None,
      21              :         Err(std::env::VarError::NotUnicode(_)) => {
      22            0 :             panic!("env var {varname} is not unicode")
      23              :         }
      24              :     }
      25            3 : }
      26              : 
      27              : /// For types `V` that implement [`serde::de::DeserializeOwned`].
      28           64 : pub fn var_serde_json_string<V>(varname: &str) -> Option<V>
      29           64 : where
      30           64 :     V: serde::de::DeserializeOwned,
      31           64 : {
      32           64 :     match std::env::var(varname) {
      33           64 :         Ok(s) => Some({
      34           64 :             let value = serde_json::Value::String(s);
      35           64 :             serde_json::from_value(value)
      36           64 :                 .map_err(|e| {
      37            0 :                     format!("failed to parse env var {varname} as a serde_json json string: {e:#}")
      38           64 :                 })
      39           64 :                 .unwrap()
      40           64 :         }),
      41            0 :         Err(std::env::VarError::NotPresent) => None,
      42              :         Err(std::env::VarError::NotUnicode(_)) => {
      43            0 :             panic!("env var {varname} is not unicode")
      44              :         }
      45              :     }
      46            0 : }
        

Generated by: LCOV version 2.1-beta