Add axum-extra integration

This commit is contained in:
Diggory Blake 2023-08-14 12:31:02 +01:00
parent 7cd323a0fa
commit bf9ed6ae36
No known key found for this signature in database
GPG key ID: E6BDFA83146ABD40
2 changed files with 18 additions and 2 deletions

View file

@ -14,12 +14,16 @@ all-features = true
default = [] default = []
async = ["async-trait"] async = ["async-trait"]
axum = ["dep:axum", "async", "tracing", "thiserror"] axum = ["dep:axum", "async", "tracing", "thiserror"]
axum-extra = ["axum", "dep:axum-extra"]
[dependencies] [dependencies]
parking_lot = "0.12.1" parking_lot = "0.12.1"
anymap = { version = "1.0.0-beta.2", features = ["hashbrown"] } anymap = { version = "1.0.0-beta.2", features = ["hashbrown"] }
async-trait = { version = "0.1", optional = true } async-trait = { version = "0.1", optional = true }
axum = { version = "0.6", optional = true } axum = { version = "0.6", optional = true }
axum-extra = { version = "0.7.7", optional = true, features = [
"cookie-private",
] }
tracing = { version = "0.1", optional = true } tracing = { version = "0.1", optional = true }
thiserror = { version = "1.0", optional = true } thiserror = { version = "1.0", optional = true }
anyhow = { version = "1.0" } anyhow = { version = "1.0" }

View file

@ -16,7 +16,7 @@ use axum::{
}; };
use frunk::HCons; use frunk::HCons;
use crate::{Aero, AsyncConstructibleResource, ConstructibleResource, Resource, ResourceList}; use crate::{Aero, AsyncConstructibleResource, Resource, ResourceList};
/// Type of axum Rejection returned when a resource cannot be acquired /// Type of axum Rejection returned when a resource cannot be acquired
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
@ -64,7 +64,7 @@ impl DependencyError {
pub struct Dep<T: Resource>(pub T); pub struct Dep<T: Resource>(pub T);
#[async_trait] #[async_trait]
impl<T: ConstructibleResource, S: Send + Sync> FromRequestParts<S> for Dep<T> impl<T: Resource, S: Send + Sync> FromRequestParts<S> for Dep<T>
where where
Aero: FromRef<S>, Aero: FromRef<S>,
{ {
@ -103,3 +103,15 @@ impl<H: Resource, T: ResourceList> FromRef<Aero<HCons<H, T>>> for Aero {
input.clone().into() input.clone().into()
} }
} }
#[cfg(feature = "axum-extra")]
mod extra_impls {
use axum::extract::FromRef;
use crate::{Aero, ResourceList};
impl<R: ResourceList> FromRef<Aero<R>> for axum_extra::extract::cookie::Key {
fn from_ref(input: &Aero<R>) -> Self {
input.try_get().expect("Missing cookie key")
}
}
}