vnj/src/cfg.rs
2024-11-17 18:43:15 +03:00

65 lines
1.3 KiB
Rust

use std::{fs, net::SocketAddr, path::PathBuf};
use eyre::Context;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Http {
pub listen: SocketAddr,
}
#[derive(Debug, Clone, Copy, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LogLevel {
Info,
Debug,
// Error, // there's no error logs.
Off,
}
impl From<LogLevel> for Option<tracing::Level> {
fn from(value: LogLevel) -> Self {
use tracing::Level as L;
Some(match value {
LogLevel::Info => L::INFO,
LogLevel::Debug => L::DEBUG,
LogLevel::Off => return None,
})
}
}
#[derive(Debug, Deserialize)]
#[serde(untagged, rename_all = "snake_case")]
pub enum Secret {
Path { path: PathBuf },
Plain(String),
}
impl Secret {
pub fn into_string(&self) -> eyre::Result<String> {
match self {
Self::Path { path } => fs::read_to_string(path).wrap_err("failed to read secret"),
Self::Plain(s) => Ok(s.clone()),
}
}
}
#[derive(Debug, Deserialize)]
pub struct App {
pub secret: Secret,
pub log_level: LogLevel,
pub journal: PathBuf,
}
#[derive(Debug, Deserialize)]
pub struct Journal {
pub root: PathBuf,
}
#[derive(Debug, Deserialize)]
pub struct Root {
pub app: App,
pub http: Http,
}