Replies: 2 comments 5 replies
-
|
Shouldn't you write Also please post the error you're getting, it makes it easier to help you. |
Beta Was this translation helpful? Give feedback.
5 replies
-
|
Maybe you could something like: use std::sync::Arc;
use axum::extract::{FromRef, FromRequestParts};
use axum::http::request::Parts;
#[derive(Clone)]
pub struct Config;
pub struct Claims;
// traits
pub trait PermissionRepository {
fn get_permissions_by_role(&self, role_id: &str) -> Vec<String>;
// ... more methods
}
pub type DynPermissionRepository = Arc<dyn PermissionRepository + Send + Sync>;
pub trait UserRepository {
fn get(&self, user_id: &str) -> String;
// ... more methods
}
pub type DynUserRepository = Arc<dyn UserRepository + Send + Sync>;
// states
#[derive(Clone)]
pub struct AppState {
pub config: Config,
pub permission_repository: DynPermissionRepository,
}
pub struct UserState {
pub app_state: AppState,
pub user_repository: DynUserRepository,
}
pub struct PermissionScopesState {
pub app_state: AppState,
pub user_repository: DynUserRepository,
pub scopes: Vec<String>,
}
// from refs impl
impl FromRef<PermissionScopesState> for AppState {
fn from_ref(input: &PermissionScopesState) -> Self {
input.app_state.clone()
}
}
impl FromRef<PermissionScopesState> for UserState {
fn from_ref(input: &PermissionScopesState) -> Self {
Self {
app_state: input.app_state.clone(),
user_repository: input.user_repository.clone(),
}
}
}
// from request parts
impl FromRequestParts<AppState> for Claims {
type Rejection = ();
async fn from_request_parts(
parts: &mut Parts,
state: &AppState,
) -> Result<Self, Self::Rejection> {
println!("from request parts");
let _ = state
.permission_repository
.get_permissions_by_role("role_id");
todo!()
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hey Team, I'm trying to implement FromRequestParts for a struct when the state has generics. not sure if that is possible but I have tried few ways but it dont seem to work
I have these other state for different routes. each of that state having implementation for FromRef for AppState
The issue arises here when I try to add generic here in AppState
axum version
0.7.5
Beta Was this translation helpful? Give feedback.
All reactions