mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 10:32:19 +00:00
@@ -1,4 +1,8 @@
|
||||
use axum::{body::Body, extract::FromRequestParts, response::IntoResponse};
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{FromRequestParts, OptionalFromRequestParts},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use rustical_xml::{ValueDeserialize, ValueSerialize, XmlError};
|
||||
use thiserror::Error;
|
||||
|
||||
@@ -59,6 +63,21 @@ impl TryFrom<&[u8]> for Depth {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Send + Sync> OptionalFromRequestParts<S> for Depth {
|
||||
type Rejection = InvalidDepthHeader;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut axum::http::request::Parts,
|
||||
_state: &S,
|
||||
) -> Result<Option<Self>, Self::Rejection> {
|
||||
if let Some(depth_header) = parts.headers.get("Depth") {
|
||||
Ok(Some(depth_header.as_bytes().try_into()?))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Send + Sync> FromRequestParts<S> for Depth {
|
||||
type Rejection = InvalidDepthHeader;
|
||||
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
use axum::{body::Body, extract::FromRequestParts, response::IntoResponse};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("Invalid Overwrite header")]
|
||||
pub struct InvalidOverwriteHeader;
|
||||
|
||||
impl IntoResponse for InvalidOverwriteHeader {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
axum::response::Response::builder()
|
||||
.status(axum::http::StatusCode::BAD_REQUEST)
|
||||
.body(Body::new("Invalid Overwrite header".to_string()))
|
||||
.expect("this always works")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Default)]
|
||||
pub enum Overwrite {
|
||||
#[default]
|
||||
@@ -17,6 +27,21 @@ impl Overwrite {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Send + Sync> FromRequestParts<S> for Overwrite {
|
||||
type Rejection = InvalidOverwriteHeader;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut axum::http::request::Parts,
|
||||
_state: &S,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
if let Some(overwrite_header) = parts.headers.get("Overwrite") {
|
||||
overwrite_header.as_bytes().try_into()
|
||||
} else {
|
||||
Ok(Self::default())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for Overwrite {
|
||||
type Error = InvalidOverwriteHeader;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user