Make MultistatusElement responder, other refactoring

This commit is contained in:
Lennart
2024-06-21 18:03:58 +02:00
parent 584e325596
commit 12374926dd
10 changed files with 81 additions and 80 deletions

View File

@@ -0,0 +1,38 @@
use actix_web::{
body::BoxBody, http::header::ContentType, HttpRequest, HttpResponse, Responder, ResponseError,
};
use log::debug;
use serde::Serialize;
#[derive(Serialize)]
#[serde(rename = "multistatus")]
pub struct MultistatusElement<T1: Serialize, T2: Serialize> {
#[serde(rename = "response")]
pub responses: Vec<T1>,
#[serde(rename = "response")]
pub member_responses: Vec<T2>,
#[serde(rename = "@xmlns")]
pub ns_dav: &'static str,
#[serde(rename = "@xmlns:C")]
pub ns_caldav: &'static str,
#[serde(rename = "@xmlns:IC")]
pub ns_ical: &'static str,
}
impl<T1: Serialize, T2: Serialize> Responder for MultistatusElement<T1, T2> {
type Body = BoxBody;
fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body> {
let mut output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".to_owned();
let mut ser = quick_xml::se::Serializer::new(&mut output);
ser.indent(' ', 4);
if let Err(err) = self.serialize(ser) {
return crate::Error::from(err).error_response();
}
debug!("Return multistatus:\n{output}");
HttpResponse::MultiStatus()
.content_type(ContentType::xml())
.body(output)
}
}