use std::borrow::Cow;
use std::{fs::read_to_string, io, net::SocketAddr, path::Path};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone)]
pub struct Config {
pub listen_address: SocketAddr,
pub auth_cookie_name: Cow<'static, str>,
pub secret: Cow<'static, str>,
pub socket_path: Cow<'static, str>,
pub rcs_image: Cow<'static, str>,
}
impl Default for Config {
fn default() -> Self {
Self {
listen_address: SocketAddr::from(([127, 0, 0, 1], 8000)),
auth_cookie_name: Cow::Borrowed("access_token"),
secret: Cow::Borrowed("demosecret"),
socket_path: Cow::Borrowed("unix:///run/user/1000/podman/podman.sock"),
rcs_image: Cow::Borrowed(
"registry.gitlab.com/otafablab/rust-code-server/rust-code-server:latest",
),
}
}
}
#[derive(Debug)]
pub enum OpenConfigError {
ReadError(io::Error),
ParseError(toml::de::Error),
}
impl Config {
pub fn open(path: impl AsRef<Path>) -> Result<Self, OpenConfigError> {
let contents = read_to_string(path).map_err(OpenConfigError::ReadError)?;
toml::from_str(&contents).map_err(OpenConfigError::ParseError)
}
}