Expand description
axum 0.8 集成。
实现 FromRequestParts,于是处理器可以直接把 Guard 写在参数里:
use axum::{routing::get, Router};
use encryption::guard::Guard;
use encryption::integrations::Guarded;
#[derive(Clone)]
struct AppState { encryption: Guard }
impl Guarded for AppState {
fn guard(&self) -> &Guard { &self.encryption }
}
async fn store_phone(guard: Guard) -> String {
guard.encrypt("13800138000".as_bytes()).unwrap_or_default().len().to_string()
}
let state = AppState { encryption: /* 你的守卫 */ todo!() };
let app: Router = Router::new().route("/", get(store_phone)).with_state(state);§提取是不会失败的
Guarded::guard() 返回引用,取不到守卫只可能是状态装配错了 —— 那是编译期
就该发现的事,不是运行期。所以 Rejection 是 Infallible:这个提取器
永远不会把请求拒掉。
加解密本身的失败(密钥不对、密文被改)发生在处理器体里,由调用方决定怎么 回应 —— 本库不替应用决定「解密失败该返回 500 还是 422」。
Type Aliases§
- Guard
Extractor - 提取器实现的宿主类型。写出来只是为了让「守卫是怎么进来的」这件事 在文档与再导出里有个名字。