diff --git a/crates/ascend/src/server.rs b/crates/ascend/src/server.rs index 5e8958e..3ea039f 100644 --- a/crates/ascend/src/server.rs +++ b/crates/ascend/src/server.rs @@ -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) -> ServeDir { async fn load_state() -> Result { tracing::info!("Loading state"); let state = State { - persistent: Persistent::::load(STATE_FILE.into()).await?, + persistent: Persistent::::load(&Path::new(STATE_FILE)).await?, }; Ok(state) } diff --git a/crates/ascend/src/server/persistence.rs b/crates/ascend/src/server/persistence.rs index f6869a7..deab2f4 100644 --- a/crates/ascend/src/server/persistence.rs +++ b/crates/ascend/src/server/persistence.rs @@ -23,29 +23,25 @@ impl Persistent { /// Instantiates state from file system #[tracing::instrument] - pub async fn load(file_path: Utf8PathBuf) -> Result + pub async fn load(file_path: &Path) -> Result 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 Persistent { #[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 },