mirror of
https://github.com/neondatabase/neon.git
synced 2026-08-12 09:19:39 +00:00
env_config from PR #6125
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
use std::{fmt::Display, str::FromStr};
|
||||
|
||||
pub fn var<V, E, D>(varname: &str, default: D) -> V
|
||||
where
|
||||
V: FromStr<Err = E>,
|
||||
E: Display,
|
||||
D: FnOnce() -> V,
|
||||
{
|
||||
match std::env::var(varname) {
|
||||
Ok(s) => s
|
||||
.parse()
|
||||
.map_err(|e| format!("failed to parse env var {varname}: {e:#}"))
|
||||
.unwrap(),
|
||||
Err(std::env::VarError::NotPresent) => default(),
|
||||
Err(std::env::VarError::NotUnicode(_)) => {
|
||||
panic!("env var {varname} is not unicode")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Bool(bool);
|
||||
|
||||
impl Bool {
|
||||
pub const fn new_const<const V: bool>() -> Self {
|
||||
Bool(V)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Bool {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if let Ok(b) = s.parse() {
|
||||
return Ok(Bool(b));
|
||||
}
|
||||
Ok(Bool(match s {
|
||||
"0" => false,
|
||||
"1" => true,
|
||||
_ => return Err(format!("not a bool, accepting 0|1|{}|{}", false, true)),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<bool> for Bool {
|
||||
fn into(self) -> bool {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ pub mod auth;
|
||||
pub mod id;
|
||||
|
||||
mod hex;
|
||||
pub mod env_config;
|
||||
pub use hex::Hex;
|
||||
|
||||
// http endpoint utils
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
use std::str::FromStr;
|
||||
use std::{
|
||||
io::BufWriter,
|
||||
str::FromStr,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use anyhow::Context;
|
||||
use metrics::{IntCounter, IntCounterVec};
|
||||
use once_cell::sync::Lazy;
|
||||
use strum_macros::{EnumString, EnumVariantNames};
|
||||
|
||||
use super::env_config;
|
||||
|
||||
#[derive(EnumString, EnumVariantNames, Eq, PartialEq, Debug, Clone, Copy)]
|
||||
#[strum(serialize_all = "snake_case")]
|
||||
pub enum LogFormat {
|
||||
|
||||
Reference in New Issue
Block a user