use std::path::{Path, PathBuf};
use crate::installer::{InstallProgress, InstallationFeedback, Installer, InstallerError};
use crate::update_source::Update;
use crate::update_workflow::UpdateError;
impl From<std::io::Error> for UpdateError {
fn from(e: std::io::Error) -> Self {
UpdateError::InstallerError(InstallerError::InstallationFailed(format!(
"io error: {}",
e
)))
}
}
pub struct DirectoryInstaller {
dir: String,
}
impl DirectoryInstaller {
pub fn new(dir: &str) -> Self {
DirectoryInstaller {
dir: dir.to_string(),
}
}
}
impl Installer for DirectoryInstaller {
fn install(&self, update: &impl Update) -> Result<impl InstallProgress, UpdateError> {
Ok(DirectoryInstallProgress {
update: Box::new(update.clone()),
installed: false,
dir: self.dir.clone(),
})
}
}
pub struct DirectoryInstallProgress<T>
where
T: Update,
{
update: Box<T>,
installed: bool,
dir: String,
}
impl<T> InstallProgress for DirectoryInstallProgress<T>
where
T: Update,
{
async fn next(&mut self) -> Result<Option<InstallationFeedback>, UpdateError> {
if self.installed {
return Ok(None);
}
tokio::fs::create_dir_all(Path::new(&self.dir)).await?;
self.update
.save_to_disk(Some(PathBuf::from(&self.dir)))
.await?;
self.installed = true;
Ok(Some(InstallationFeedback {
status: "success".to_string(),
message: format!("installed update to {}", self.dir),
progress: Some((1, 1)),
}))
}
}