mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 11:42:25 +00:00
Replacing garbage code with less garbage code :)
This commit is contained in:
@@ -15,7 +15,6 @@ use std::sync::Arc;
|
|||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod proptypes;
|
|
||||||
pub mod resources;
|
pub mod resources;
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
pub mod tagname;
|
pub mod tagname;
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
use std::io::Write;
|
|
||||||
|
|
||||||
use quick_xml::{events::BytesText, Writer};
|
|
||||||
|
|
||||||
pub fn write_string_prop<'a, W: Write>(
|
|
||||||
writer: &'a mut Writer<W>,
|
|
||||||
propname: &'a str,
|
|
||||||
value: &str,
|
|
||||||
) -> Result<&'a mut Writer<W>, quick_xml::Error> {
|
|
||||||
let el = writer.create_element(propname);
|
|
||||||
if value.is_empty() {
|
|
||||||
el.write_empty()
|
|
||||||
} else {
|
|
||||||
el.write_text_content(BytesText::new(value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_href_prop<'a, W: Write>(
|
|
||||||
writer: &'a mut Writer<W>,
|
|
||||||
propname: &'a str,
|
|
||||||
href: &str,
|
|
||||||
) -> Result<&'a mut Writer<W>, quick_xml::Error> {
|
|
||||||
write_hrefs_prop(writer, propname, vec![href])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_hrefs_prop<'a, W: Write>(
|
|
||||||
writer: &'a mut Writer<W>,
|
|
||||||
propname: &'a str,
|
|
||||||
hrefs: Vec<&str>,
|
|
||||||
) -> Result<&'a mut Writer<W>, quick_xml::Error> {
|
|
||||||
writer
|
|
||||||
.create_element(propname)
|
|
||||||
.write_inner_content(|writer| {
|
|
||||||
for href in hrefs {
|
|
||||||
write_string_prop(writer, "href", href)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -3,17 +3,17 @@ use std::{io::Write, sync::Arc};
|
|||||||
use actix_web::{web::Data, HttpRequest};
|
use actix_web::{web::Data, HttpRequest};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use quick_xml::{events::BytesText, Writer};
|
use quick_xml::Writer;
|
||||||
use rustical_auth::AuthInfo;
|
use rustical_auth::AuthInfo;
|
||||||
use rustical_store::calendar::{Calendar, CalendarStore};
|
use rustical_store::calendar::{Calendar, CalendarStore};
|
||||||
use strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
|
use strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
use crate::{
|
use crate::tagname::TagName;
|
||||||
proptypes::{write_href_prop, write_string_prop},
|
use rustical_dav::{
|
||||||
tagname::TagName,
|
resource::Resource,
|
||||||
|
xml_snippets::{write_resourcetype, HrefElement, TextElement},
|
||||||
};
|
};
|
||||||
use rustical_dav::{resource::Resource, xml_snippets::write_resourcetype};
|
|
||||||
|
|
||||||
pub struct CalendarResource<C: CalendarStore + ?Sized> {
|
pub struct CalendarResource<C: CalendarStore + ?Sized> {
|
||||||
pub cal_store: Arc<RwLock<C>>,
|
pub cal_store: Arc<RwLock<C>>,
|
||||||
@@ -87,35 +87,22 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
|
|||||||
write_resourcetype(writer, vec!["C:calendar", "collection"])?
|
write_resourcetype(writer, vec!["C:calendar", "collection"])?
|
||||||
}
|
}
|
||||||
CalendarProp::CurrentUserPrincipal | CalendarProp::Owner => {
|
CalendarProp::CurrentUserPrincipal | CalendarProp::Owner => {
|
||||||
write_href_prop(
|
writer.write_serializable(
|
||||||
writer,
|
|
||||||
prop.tagname(),
|
prop.tagname(),
|
||||||
&format!("{}/{}/", self.prefix, self.principal),
|
&HrefElement::new(format!("{}/{}/", self.prefix, self.principal)),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
CalendarProp::Displayname => {
|
CalendarProp::Displayname => {
|
||||||
let el = writer.create_element(prop.tagname());
|
let name = self.calendar.name.clone();
|
||||||
if let Some(name) = self.calendar.clone().name {
|
writer.write_serializable(prop.tagname(), &TextElement(name))?;
|
||||||
el.write_text_content(BytesText::new(&name))?;
|
|
||||||
} else {
|
|
||||||
el.write_empty()?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
CalendarProp::CalendarColor => {
|
CalendarProp::CalendarColor => {
|
||||||
let el = writer.create_element(prop.tagname());
|
let color = self.calendar.color.clone();
|
||||||
if let Some(color) = self.calendar.clone().color {
|
writer.write_serializable(prop.tagname(), &TextElement(color))?;
|
||||||
el.write_text_content(BytesText::new(&color))?;
|
|
||||||
} else {
|
|
||||||
el.write_empty()?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
CalendarProp::CalendarDescription => {
|
CalendarProp::CalendarDescription => {
|
||||||
let el = writer.create_element(prop.tagname());
|
let description = self.calendar.description.clone();
|
||||||
if let Some(description) = self.calendar.clone().description {
|
writer.write_serializable(prop.tagname(), &TextElement(description))?;
|
||||||
el.write_text_content(BytesText::new(&description))?;
|
|
||||||
} else {
|
|
||||||
el.write_empty()?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
CalendarProp::SupportedCalendarComponentSet => {
|
CalendarProp::SupportedCalendarComponentSet => {
|
||||||
writer
|
writer
|
||||||
@@ -144,10 +131,16 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
|
|||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
CalendarProp::Getcontenttype => {
|
CalendarProp::Getcontenttype => {
|
||||||
write_string_prop(writer, prop.tagname(), "text/calendar")?;
|
writer.write_serializable(
|
||||||
|
prop.tagname(),
|
||||||
|
&TextElement(Some("text/calendar".to_owned())),
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
CalendarProp::MaxResourceSize => {
|
CalendarProp::MaxResourceSize => {
|
||||||
write_string_prop(writer, prop.tagname(), "10000000")?;
|
writer.write_serializable(
|
||||||
|
prop.tagname(),
|
||||||
|
&TextElement(Some("10000000".to_owned())),
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
CalendarProp::CurrentUserPrivilegeSet => {
|
CalendarProp::CurrentUserPrivilegeSet => {
|
||||||
writer
|
writer
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use crate::{proptypes::write_string_prop, tagname::TagName};
|
use crate::tagname::TagName;
|
||||||
use actix_web::{web::Data, HttpRequest};
|
use actix_web::{web::Data, HttpRequest};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use rustical_auth::AuthInfo;
|
use rustical_auth::AuthInfo;
|
||||||
use rustical_dav::resource::Resource;
|
use rustical_dav::{resource::Resource, xml_snippets::TextElement};
|
||||||
use rustical_store::calendar::CalendarStore;
|
use rustical_store::calendar::CalendarStore;
|
||||||
use rustical_store::event::Event;
|
use rustical_store::event::Event;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -69,13 +69,20 @@ impl<C: CalendarStore + ?Sized> Resource for EventResource<C> {
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
match prop {
|
match prop {
|
||||||
EventProp::Getetag => {
|
EventProp::Getetag => {
|
||||||
write_string_prop(writer, prop.tagname(), &self.event.get_etag())?;
|
writer.write_serializable(
|
||||||
|
prop.tagname(),
|
||||||
|
&TextElement(Some(self.event.get_etag())),
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
EventProp::CalendarData => {
|
EventProp::CalendarData => {
|
||||||
write_string_prop(writer, prop.tagname(), &self.event.get_ics())?;
|
writer
|
||||||
|
.write_serializable(prop.tagname(), &TextElement(Some(self.event.get_ics())))?;
|
||||||
}
|
}
|
||||||
EventProp::Getcontenttype => {
|
EventProp::Getcontenttype => {
|
||||||
write_string_prop(writer, prop.tagname(), "text/calendar;charset=utf-8")?;
|
writer.write_serializable(
|
||||||
|
prop.tagname(),
|
||||||
|
&TextElement(Some("text/calendar;charset=utf-8".to_owned())),
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
use crate::{proptypes::write_href_prop, tagname::TagName};
|
use crate::tagname::TagName;
|
||||||
use actix_web::{web::Data, HttpRequest};
|
use actix_web::{web::Data, HttpRequest};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use quick_xml::events::BytesText;
|
|
||||||
use rustical_auth::AuthInfo;
|
use rustical_auth::AuthInfo;
|
||||||
use rustical_dav::{resource::Resource, xml_snippets::write_resourcetype};
|
use rustical_dav::{
|
||||||
|
resource::Resource,
|
||||||
|
xml_snippets::{write_resourcetype, HrefElement},
|
||||||
|
};
|
||||||
use rustical_store::calendar::CalendarStore;
|
use rustical_store::calendar::CalendarStore;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
|
use strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
|
||||||
@@ -91,26 +93,15 @@ impl<C: CalendarStore + ?Sized> Resource for PrincipalCalendarsResource<C> {
|
|||||||
PrincipalProp::Resourcetype => {
|
PrincipalProp::Resourcetype => {
|
||||||
write_resourcetype(writer, vec!["principal", "collection"])?
|
write_resourcetype(writer, vec!["principal", "collection"])?
|
||||||
}
|
}
|
||||||
PrincipalProp::CurrentUserPrincipal | PrincipalProp::PrincipalUrl => {
|
PrincipalProp::CurrentUserPrincipal
|
||||||
write_href_prop(
|
| PrincipalProp::PrincipalUrl
|
||||||
writer,
|
| PrincipalProp::CalendarHomeSet
|
||||||
prop.into(),
|
| PrincipalProp::CalendarUserAddressSet => {
|
||||||
&format!("{}/{}/", self.prefix, self.principal),
|
writer.write_serializable(
|
||||||
|
prop.tagname(),
|
||||||
|
&HrefElement::new(format!("{}/{}/", self.prefix, self.principal)),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
PrincipalProp::CalendarHomeSet | PrincipalProp::CalendarUserAddressSet => {
|
|
||||||
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user