mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 05:52:19 +00:00
74 lines
2.0 KiB
Rust
74 lines
2.0 KiB
Rust
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<Vec<Self::MemberType>> {
|
|
Ok(vec![])
|
|
}
|
|
|
|
async fn acquire_from_request(
|
|
req: HttpRequest,
|
|
auth_info: AuthInfo,
|
|
_uri_components: Self::UriComponents,
|
|
prefix: String,
|
|
) -> Result<Self> {
|
|
Ok(Self {
|
|
prefix,
|
|
principal: auth_info.user_id,
|
|
path: req.path().to_string(),
|
|
})
|
|
}
|
|
|
|
fn write_prop<W: std::io::Write>(
|
|
&self,
|
|
writer: &mut quick_xml::Writer<W>,
|
|
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(())
|
|
}
|
|
}
|