use crate::{
run::{
parser::composite,
saver::{self, livesplit::IoWrite},
},
Run, Timer,
};
use snafu::ResultExt;
pub use api::{run::UploadedRun, Client, Error as UploadError};
pub use splits_io_api as api;
#[derive(Debug, snafu::Snafu)]
#[snafu(context(suffix(false)))]
pub enum DownloadError {
Download {
source: api::Error,
},
Parse {
source: composite::Error,
},
}
pub async fn download_run(
client: &Client,
id: &str,
) -> Result<composite::ParsedRun<'static>, DownloadError> {
let bytes = api::run::download(client, id).await.context(Download)?;
let run = composite::parse(&bytes, None).context(Parse)?;
Ok(run.into_owned())
}
pub async fn upload_run(client: &Client, run: &Run) -> Result<UploadedRun, UploadError> {
api::run::upload_lazy(client, |writer| {
saver::livesplit::save_run(run, IoWrite(writer))
})
.await
}
pub async fn upload_timer(client: &Client, timer: &Timer) -> Result<UploadedRun, UploadError> {
api::run::upload_lazy(client, |writer| {
saver::livesplit::save_timer(timer, IoWrite(writer))
})
.await
}