use std::io::Write;
use std::path::{Path, PathBuf};
use serde::Serialize;
use serde::de::DeserializeOwned;
#[derive(Debug)]
pub struct ConfigFileError {
path: PathBuf,
detail: String,
}
impl ConfigFileError {
#[must_use]
pub fn new(path: &Path, detail: String) -> Self {
Self {
path: path.to_path_buf(),
detail,
}
}
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
}
impl std::fmt::Display for ConfigFileError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let path = self.path.display();
write!(
f,
"{path} is not a valid profile: {detail}\n\n\
Nothing was changed. Fix the file, or move it aside and start again:\n \
mv {path} {path}.broken",
detail = self.detail.trim_end(),
)
}
}
impl std::error::Error for ConfigFileError {}
pub fn load_toml<T: DeserializeOwned + Default>(path: &Path) -> Result<T, ConfigFileError> {
let contents = match std::fs::read_to_string(path) {
Ok(c) => c,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(T::default()),
Err(e) => return Err(ConfigFileError::new(path, e.to_string())),
};
toml::from_str(&contents).map_err(|e| ConfigFileError::new(path, e.to_string()))
}
pub fn save_toml_atomic<T: Serialize>(
path: &Path,
value: &T,
) -> Result<(), Box<dyn std::error::Error>> {
let contents =
toml::to_string_pretty(value).map_err(|e| format!("failed to serialize config: {e}"))?;
write_atomic(path, contents.as_bytes())?;
Ok(())
}
pub fn write_atomic(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
let dir = path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let file_name = path
.file_name()
.ok_or_else(|| std::io::Error::other(format!("{} has no file name", path.display())))?;
let mut tmp_name = std::ffi::OsString::from(".");
tmp_name.push(file_name);
tmp_name.push(format!(".tmp.{}", std::process::id()));
let tmp = dir.join(tmp_name);
let result = (|| {
let mut opts = std::fs::OpenOptions::new();
opts.write(true).create(true).truncate(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
opts.mode(0o600);
}
let mut f = opts.open(&tmp)?;
f.write_all(bytes)?;
f.sync_all()?;
drop(f);
std::fs::rename(&tmp, path)?;
#[cfg(unix)]
if let Ok(d) = std::fs::File::open(dir) {
let _ = d.sync_all();
}
Ok(())
})();
if result.is_err() {
let _ = std::fs::remove_file(&tmp);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
use serde::Deserialize;
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
struct Profile {
default: Option<String>,
}
fn tmpdir(tag: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!(
"vta-cli-common-config-{tag}-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&d).unwrap();
d
}
#[test]
fn missing_file_is_the_default_profile() {
let d = tmpdir("missing");
let p: Profile = load_toml(&d.join("config.toml")).unwrap();
assert_eq!(p, Profile::default());
}
#[test]
fn a_corrupt_file_is_fatal_names_the_fix_and_is_left_alone() {
let d = tmpdir("corrupt");
let path = d.join("config.toml");
std::fs::write(&path, "default = \"a\"\n}garbage").unwrap();
let err = load_toml::<Profile>(&path).unwrap_err();
let msg = err.to_string();
assert!(msg.contains("is not a valid profile"), "{msg}");
assert!(
msg.contains(&format!("mv {} {}.broken", path.display(), path.display())),
"{msg}"
);
assert!(std::fs::read_to_string(&path).unwrap().contains("garbage"));
}
#[test]
fn save_round_trips_and_leaves_no_temporary() {
let d = tmpdir("save");
let path = d.join("config.toml");
std::fs::write(&path, "old").unwrap();
let p = Profile {
default: Some("x".into()),
};
save_toml_atomic(&path, &p).unwrap();
assert_eq!(load_toml::<Profile>(&path).unwrap(), p);
let names: Vec<_> = std::fs::read_dir(&d)
.unwrap()
.map(|e| e.unwrap().file_name())
.collect();
assert_eq!(names.len(), 1, "{names:?}");
}
}