use std::io::SeekFrom;
use ::tokio::io::{AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt, AsyncWrite, AsyncWriteExt};
use crate::{
execute::Action,
plan::WritePlan,
task::{Progress, Task},
HandlerRegistry, Item, Result, Sink, Structure, Updates,
};
pub async fn run<T, R>(mut task: Task<'_, T>, src: &mut R) -> Result<T>
where
R: AsyncRead + AsyncSeek + Unpin + ?Sized,
{
let mut at = None;
loop {
match task.resume()? {
Progress::Read(mut req) => {
if at != Some(req.offset()) {
src.seek(SeekFrom::Start(req.offset())).await?;
}
let offset = req.offset();
let window = req.window_len() as u64;
let buf = req.buffer();
buf.clear();
(&mut *src).take(window).read_to_end(buf).await?;
at = Some(offset + buf.len() as u64);
}
Progress::Done(value) => return Ok(value),
}
}
}
pub async fn parse<R>(
registry: &HandlerRegistry,
src: &mut R,
format: Option<&str>,
) -> Result<Structure>
where
R: AsyncRead + AsyncSeek + Unpin + ?Sized,
{
let total = src.seek(SeekFrom::End(0)).await?;
run(Structure::parse_task(registry, total, format), src).await
}
pub async fn read_item<R>(structure: &Structure, src: &mut R, item: &Item) -> Result<Vec<u8>>
where
R: AsyncRead + AsyncSeek + Unpin + ?Sized,
{
run(
Task::new(structure.total_size(), |mut io| async move {
structure.read_item_async(&mut io, item).await
}),
src,
)
.await
}
pub async fn read_content<R>(structure: &Structure, src: &mut R, item: &Item) -> Result<Vec<u8>>
where
R: AsyncRead + AsyncSeek + Unpin + ?Sized,
{
run(
Task::new(structure.total_size(), |mut io| async move {
structure.read_content_async(&mut io, item).await
}),
src,
)
.await
}
pub async fn c2pa_item<'s, R>(structure: &'s Structure, src: &mut R) -> Result<Option<&'s Item>>
where
R: AsyncRead + AsyncSeek + Unpin + ?Sized,
{
run(
Task::new(structure.total_size(), |mut io| async move {
structure.c2pa_item_async(&mut io).await
}),
src,
)
.await
}
pub async fn c2pa_store<R>(structure: &Structure, src: &mut R) -> Result<Option<Vec<u8>>>
where
R: AsyncRead + AsyncSeek + Unpin + ?Sized,
{
run(
Task::new(structure.total_size(), |mut io| async move {
structure.c2pa_store_async(&mut io).await
}),
src,
)
.await
}
pub async fn write_plan<'a, R>(
structure: &Structure,
src: &mut R,
updates: &'a Updates,
) -> Result<WritePlan<'a>>
where
R: AsyncRead + AsyncSeek + Unpin + ?Sized,
{
run(structure.write_plan_task(updates), src).await
}
pub async fn execute<R, W, S>(
plan: &WritePlan<'_>,
src: &mut R,
out: &mut W,
sink: &mut S,
) -> Result<()>
where
R: AsyncRead + AsyncSeek + Unpin + ?Sized,
W: AsyncWrite + Unpin + ?Sized,
S: Sink + ?Sized,
{
let mut execution = plan.start();
let mut at = None;
loop {
match execution.step(sink)? {
Action::Read { offset, buf } => {
if at != Some(offset) {
src.seek(SeekFrom::Start(offset)).await?;
}
src.read_exact(buf).await?;
at = Some(offset + buf.len() as u64);
}
Action::Write(bytes) => out.write_all(bytes).await?,
Action::Done => break,
}
}
out.flush().await?;
Ok(())
}
pub async fn write<R, W, S>(
structure: &Structure,
src: &mut R,
out: &mut W,
updates: &Updates,
sink: &mut S,
) -> Result<Structure>
where
R: AsyncRead + AsyncSeek + Unpin + ?Sized,
W: AsyncWrite + Unpin + ?Sized,
S: Sink + ?Sized,
{
let plan = write_plan(structure, src, updates).await?;
execute(&plan, src, out, sink).await?;
Ok(plan.into_structure())
}