use alloc::{string::String, vec::Vec};
use log::trace;
use url::Url;
use crate::{
coroutine::*,
rfc4791::item::join_path,
rfc4918::{
WebdavAuth,
get::Get,
read_etag,
send::{SendError, SendOk},
},
webdav_try,
};
#[derive(Debug)]
pub struct ReadItem {
state: State,
}
impl ReadItem {
pub fn new(
base_url: &Url,
auth: &WebdavAuth,
user_agent: &str,
calendar_path: &str,
item_id: &str,
) -> Self {
let path = join_path(calendar_path, item_id);
Self {
state: State::Get(Get::new(base_url, auth, user_agent, &path)),
}
}
}
impl WebdavCoroutine for ReadItem {
type Yield = WebdavYield;
type Return = Result<ItemBody, SendError>;
fn resume(&mut self, arg: Option<&[u8]>) -> WebdavCoroutineState<Self::Yield, Self::Return> {
trace!("sending request");
match &mut self.state {
State::Get(get) => {
let SendOk { response, body, .. } = webdav_try!(get, arg);
let etag = read_etag(&response);
WebdavCoroutineState::Complete(Ok(ItemBody { data: body, etag }))
}
}
}
}
#[derive(Debug)]
enum State {
Get(Get),
}
#[derive(Clone, Debug)]
pub struct ItemBody {
pub data: Vec<u8>,
pub etag: Option<String>,
}