use crate::tagname::TagName; use actix_web::HttpRequest; use anyhow::Result; use async_trait::async_trait; use quick_xml::events::BytesText; use rustical_auth::AuthInfo; use rustical_dav::{resource::Resource, xml_snippets::write_resourcetype}; use strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames}; pub struct RootResource { prefix: String, principal: String, path: String, } #[derive(EnumString, Debug, VariantNames, EnumProperty, IntoStaticStr)] #[strum(serialize_all = "kebab-case")] pub enum RootProp { Resourcetype, CurrentUserPrincipal, } #[async_trait(?Send)] impl Resource for RootResource { type UriComponents = (); type MemberType = Self; type PropType = RootProp; fn get_path(&self) -> &str { &self.path } async fn get_members(&self) -> Result> { Ok(vec![]) } async fn acquire_from_request( req: HttpRequest, auth_info: AuthInfo, _uri_components: Self::UriComponents, prefix: String, ) -> Result { Ok(Self { prefix, principal: auth_info.user_id, path: req.path().to_string(), }) } fn write_prop( &self, writer: &mut quick_xml::Writer, prop: Self::PropType, ) -> Result<()> { match prop { RootProp::Resourcetype => write_resourcetype(writer, vec!["collection"])?, RootProp::CurrentUserPrincipal => { writer .create_element(prop.tagname()) .write_inner_content(|writer| { writer .create_element("href") .write_text_content(BytesText::new(&format!( "{}/{}", self.prefix, self.principal )))?; Ok::<(), quick_xml::Error>(()) })?; } }; Ok(()) } }