dav: refactor overwrite header

This commit is contained in:
Lennart
2025-09-23 11:43:42 +02:00
parent 602c511c90
commit 4919514d09
4 changed files with 52 additions and 17 deletions

View File

@@ -22,7 +22,7 @@ pub async fn route_import<C: CalendarStore, S: SubscriptionStore>(
Path((principal, cal_id)): Path<(String, String)>, Path((principal, cal_id)): Path<(String, String)>,
user: Principal, user: Principal,
State(resource_service): State<CalendarResourceService<C, S>>, State(resource_service): State<CalendarResourceService<C, S>>,
overwrite: Overwrite, Overwrite(overwrite): Overwrite,
body: String, body: String,
) -> Result<Response, Error> { ) -> Result<Response, Error> {
if !user.is_principal(&principal) { if !user.is_principal(&principal) {
@@ -103,7 +103,7 @@ pub async fn route_import<C: CalendarStore, S: SubscriptionStore>(
let cal_store = resource_service.cal_store; let cal_store = resource_service.cal_store;
cal_store cal_store
.import_calendar(new_cal, objects, overwrite.is_true()) .import_calendar(new_cal, objects, overwrite)
.await?; .await?;
Ok(StatusCode::OK.into_response()) Ok(StatusCode::OK.into_response())

View File

@@ -14,16 +14,12 @@ impl IntoResponse for InvalidOverwriteHeader {
} }
} }
#[derive(Debug, PartialEq, Default)] #[derive(Debug, PartialEq)]
pub enum Overwrite { pub struct Overwrite(pub bool);
#[default]
T,
F,
}
impl Overwrite { impl Default for Overwrite {
pub fn is_true(&self) -> bool { fn default() -> Self {
matches!(self, Self::T) Self(true)
} }
} }
@@ -47,9 +43,48 @@ impl TryFrom<&[u8]> for Overwrite {
fn try_from(value: &[u8]) -> Result<Self, Self::Error> { fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
match value { match value {
b"T" => Ok(Overwrite::T), b"T" => Ok(Self(true)),
b"F" => Ok(Overwrite::F), b"F" => Ok(Self(false)),
_ => Err(InvalidOverwriteHeader), _ => Err(InvalidOverwriteHeader),
} }
} }
} }
#[cfg(test)]
mod tests {
use axum::{extract::FromRequestParts, response::IntoResponse};
use http::Request;
use crate::header::Overwrite;
#[tokio::test]
async fn test_overwrite_default() {
let request = Request::put("asd").body(()).unwrap();
let (mut parts, _) = request.into_parts();
let overwrite = Overwrite::from_request_parts(&mut parts, &())
.await
.unwrap();
assert_eq!(
Overwrite(true),
overwrite,
"By default we want to overwrite!"
);
}
#[test]
fn test_overwrite() {
assert_eq!(
Overwrite(true),
Overwrite::try_from(b"T".as_slice()).unwrap()
);
assert_eq!(
Overwrite(false),
Overwrite::try_from(b"F".as_slice()).unwrap()
);
if let Err(err) = Overwrite::try_from(b"aslkdjlad".as_slice()) {
let _ = err.into_response();
} else {
unreachable!("should return error")
}
}
}

View File

@@ -17,7 +17,7 @@ pub(crate) async fn axum_route_copy<R: ResourceService>(
State(resource_service): State<R>, State(resource_service): State<R>,
depth: Option<Depth>, depth: Option<Depth>,
principal: R::Principal, principal: R::Principal,
overwrite: Overwrite, Overwrite(overwrite): Overwrite,
matched_path: MatchedPath, matched_path: MatchedPath,
header_map: HeaderMap, header_map: HeaderMap,
) -> Result<Response, R::Error> { ) -> Result<Response, R::Error> {
@@ -39,7 +39,7 @@ pub(crate) async fn axum_route_copy<R: ResourceService>(
.map_err(|_| crate::Error::Forbidden)?; .map_err(|_| crate::Error::Forbidden)?;
if resource_service if resource_service
.copy_resource(&path, &dest_path, &principal, overwrite.is_true()) .copy_resource(&path, &dest_path, &principal, overwrite)
.await? .await?
{ {
// Overwritten // Overwritten

View File

@@ -17,7 +17,7 @@ pub(crate) async fn axum_route_move<R: ResourceService>(
State(resource_service): State<R>, State(resource_service): State<R>,
depth: Option<Depth>, depth: Option<Depth>,
principal: R::Principal, principal: R::Principal,
overwrite: Overwrite, Overwrite(overwrite): Overwrite,
matched_path: MatchedPath, matched_path: MatchedPath,
header_map: HeaderMap, header_map: HeaderMap,
) -> Result<Response, R::Error> { ) -> Result<Response, R::Error> {
@@ -39,7 +39,7 @@ pub(crate) async fn axum_route_move<R: ResourceService>(
.map_err(|_| crate::Error::Forbidden)?; .map_err(|_| crate::Error::Forbidden)?;
if resource_service if resource_service
.copy_resource(&path, &dest_path, &principal, overwrite.is_true()) .copy_resource(&path, &dest_path, &principal, overwrite)
.await? .await?
{ {
// Overwritten // Overwritten