birthday calendar, lots of refactoring

This commit is contained in:
Lennart
2025-01-12 16:04:38 +01:00
parent f031478786
commit b9af6c10a3
19 changed files with 284 additions and 210 deletions

View File

@@ -1,9 +1,11 @@
use std::str::FromStr;
use actix_web::dev::{AppService, HttpServiceFactory};
use actix_web::error::UrlGenerationError;
use actix_web::test::TestRequest;
use actix_web::web::Data;
use actix_web::{dev::ResourceMap, http::Method, web, ResponseError};
use async_trait::async_trait;
use serde::Deserialize;
use std::str::FromStr;
use super::methods::{route_delete, route_propfind, route_proppatch};
use super::Resource;
@@ -17,8 +19,7 @@ pub trait ResourceService: Sized + 'static {
async fn get_members(
&self,
_path: &Self::PathComponents,
_rmap: &ResourceMap,
_path_components: &Self::PathComponents,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
Ok(vec![])
}
@@ -42,17 +43,11 @@ pub trait ResourceService: Sized + 'static {
Err(crate::Error::Unauthorized.into())
}
#[inline]
fn resource_name() -> &'static str {
Self::Resource::resource_name()
}
#[inline]
fn actix_resource(self) -> actix_web::Resource {
Self::actix_additional_routes(
web::resource("")
.app_data(Data::new(self))
.name(Self::resource_name())
.route(
web::method(Method::from_str("PROPFIND").unwrap()).to(route_propfind::<Self>),
)
@@ -69,3 +64,30 @@ pub trait ResourceService: Sized + 'static {
res
}
}
pub trait NamedRoute {
fn route_name() -> &'static str;
fn get_url<U, I>(rmap: &ResourceMap, elements: U) -> Result<String, UrlGenerationError>
where
U: IntoIterator<Item = I>,
I: AsRef<str>,
{
Ok(rmap
.url_for(
&TestRequest::default().to_http_request(),
Self::route_name(),
elements,
)?
.path()
.to_owned())
}
}
pub struct ResourceServiceRoute<RS: ResourceService>(pub RS);
impl<RS: ResourceService> HttpServiceFactory for ResourceServiceRoute<RS> {
fn register(self, config: &mut AppService) {
self.0.actix_resource().register(config);
}
}