remove canonicalize

This commit is contained in:
Asger Juul Brunshøj 2025-01-22 00:24:57 +01:00
parent e925895eab
commit ad67d5997a
2 changed files with 7 additions and 13 deletions

View File

@ -36,6 +36,7 @@ pub mod cli {
ImportMiniMoonboardProblems {
file_path: PathBuf,
},
// TODO: idempotent state initialization to be used in systemd preStart hook
}
}
@ -156,7 +157,7 @@ fn file_service(path: impl AsRef<Path>) -> ServeDir {
async fn load_state() -> Result<State, Error> {
tracing::info!("Loading state");
let state = State {
persistent: Persistent::<PersistentState>::load(STATE_FILE.into()).await?,
persistent: Persistent::<PersistentState>::load(&Path::new(STATE_FILE)).await?,
};
Ok(state)
}

View File

@ -23,29 +23,25 @@ impl<T> Persistent<T> {
/// Instantiates state from file system
#[tracing::instrument]
pub async fn load(file_path: Utf8PathBuf) -> Result<Self, Error>
pub async fn load(file_path: &Path) -> Result<Self, Error>
where
T: DeserializeOwned,
{
let file_path = file_path.canonicalize().map_err(|source| Error::BadPath {
path: file_path.clone(),
source,
})?;
let content = tokio::fs::read_to_string(&file_path).await.map_err(|source| Error::Read {
file_path: file_path.clone(),
file_path: file_path.to_owned(),
source,
})?;
let t = ron::from_str(&content).map_err(|source| Error::Deserialize {
file_path: file_path.clone(),
file_path: file_path.to_owned(),
source,
})?;
let persistent = Self {
state: Arc::new(Mutex::new(t)),
file_path,
file_path: file_path.to_owned(),
};
Ok(persistent)
}
@ -116,9 +112,6 @@ impl<T> Persistent<T> {
#[derive(Debug, derive_more::Error, derive_more::Display)]
#[display("Persistent state error: {_variant}")]
pub enum Error {
#[display("Failed to operate on path: {path}")]
BadPath { path: Utf8PathBuf, source: std::io::Error },
#[display("Failed to read file: {}", file_path.display())]
Read { file_path: PathBuf, source: std::io::Error },