use alloc::string::String;
use log::trace;
use url::Url;
use crate::{
coroutine::*,
rfc4918::{
Multistatus, Property, WebdavAuth, parse_multistatus, propfind_body,
request::WebdavRequest,
send::{SendError, SendRaw},
},
webdav_try,
};
#[derive(Debug)]
pub struct Propfind {
state: State,
}
impl Propfind {
pub fn new(
base_url: &Url,
auth: &WebdavAuth,
user_agent: &str,
path: &str,
depth: u8,
props: &[Property],
) -> Self {
let request = WebdavRequest::propfind(base_url, auth, user_agent, path)
.depth(depth)
.content_type_xml()
.body(propfind_body(props));
Self {
state: State::Send(SendRaw::new(request)),
}
}
}
impl WebdavCoroutine for Propfind {
type Yield = WebdavYield;
type Return = Result<Multistatus, SendError>;
fn resume(&mut self, arg: Option<&[u8]>) -> WebdavCoroutineState<Self::Yield, Self::Return> {
trace!("sending request");
match &mut self.state {
State::Send(send) => {
let ok = webdav_try!(send, arg);
let xml = String::from_utf8_lossy(&ok.body);
WebdavCoroutineState::Complete(Ok(parse_multistatus(&xml)))
}
}
}
}
#[derive(Debug)]
enum State {
Send(SendRaw),
}