use std::{fs, io, os::unix::fs::OpenOptionsExt, path::Path};
use serde::{Deserialize, Serialize};
use crate::Error;
#[derive(Clone, Deserialize, Serialize)]
pub struct Credentials {
pub user: String,
pub password: String,
}
impl std::fmt::Debug for Credentials {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Credentials")
.field("user", &self.user)
.finish_non_exhaustive()
}
}
#[derive(Deserialize, Serialize)]
pub struct State {
pub admin: Credentials,
pub user: Option<Credentials>,
pub database: Option<String>,
}
impl State {
pub fn load(data_dir: &Path) -> Result<Option<Self>, Error> {
let bytes = match fs::read(data_dir.join("pgdb.json")) {
Ok(bytes) => bytes,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(error) => return Err(Error::ReadSetup(error)),
};
serde_json::from_slice(&bytes)
.map(Some)
.map_err(Error::ParseSetup)
}
pub fn store(&self, data_dir: &Path) -> Result<(), Error> {
let file = fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o600)
.open(data_dir.join("pgdb.json"))
.map_err(Error::WriteSetup)?;
serde_json::to_writer_pretty(file, self).map_err(Error::SerializeSetup)
}
}