make applying edits work correctly

turns out they weren't quite working due to me being a dummy and not awaiting my asyncs
This commit is contained in:
liquidex 2024-11-23 22:18:10 +01:00
parent 55f12caf22
commit 8461812935

View file

@ -1,6 +1,6 @@
use std::{error::Error, fmt, future::Future, path::PathBuf}; use std::{error::Error, fmt, future::Future, path::PathBuf};
use log::{error, info}; use log::{debug, error, info};
use tokio::task::JoinSet; use tokio::task::JoinSet;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
@ -36,26 +36,35 @@ impl Edit {
pub fn apply(self) -> impl Future<Output = Result<(), ApplyFailed>> + Send { pub fn apply(self) -> impl Future<Output = Result<(), ApplyFailed>> + Send {
async { async {
match self { match self {
Edit::NoOp => (), Edit::NoOp => debug!("no op edit"),
Edit::Write(edit_path, content) => { Edit::Write(edit_path, content) => {
tokio::fs::write(&edit_path.path, &content) tokio::fs::write(&edit_path.path, &content)
.await .await
.inspect_err(|err| error!("write to {edit_path:?} failed: {err:?}")) .inspect_err(|err| error!("write to {edit_path:?} failed: {err:?}"))
.map_err(|_| ApplyFailed)?; .map_err(|_| ApplyFailed)?;
debug!("wrote {} bytes to {edit_path:?}", content.len())
} }
Edit::Seq(vec) => { Edit::Seq(vec) => {
debug!("begin sequence of {} edits", vec.len());
for edit in vec { for edit in vec {
Box::pin(edit.apply()).await?; Box::pin(edit.apply()).await?;
} }
debug!("end sequence");
} }
Edit::All(vec) => { Edit::All(vec) => {
debug!("begin parallel {} edits", vec.len());
let mut set = JoinSet::new(); let mut set = JoinSet::new();
for edit in vec { for edit in vec {
set.spawn(edit.apply()); set.spawn(edit.apply());
} }
while let Some(result) = set.try_join_next() { while let Some(result) = set.join_next().await {
result.map_err(|_| ApplyFailed)??; result.map_err(|_| ApplyFailed)??;
} }
debug!("end parallel");
} }
Edit::Dry(edit) => edit.dry(), Edit::Dry(edit) => edit.dry(),
} }