diff --git a/axumtest/src/header/mod.rs b/axumtest/src/header/mod.rs deleted file mode 100644 index 1179c5e..0000000 --- a/axumtest/src/header/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod ciusr; diff --git a/axumtest/src/headers/cipwd.rs b/axumtest/src/headers/cipwd.rs new file mode 100644 index 0000000..d03cf70 --- /dev/null +++ b/axumtest/src/headers/cipwd.rs @@ -0,0 +1,39 @@ +//! Header parsing for X-CIPWD + +use axum::headers::Header; +use axum::headers::HeaderName; +use axum::headers::HeaderValue; + +static NAME: HeaderName = HeaderName::from_static("x-cipwd"); + +pub struct CiPwd(String); + +impl Header for CiPwd { + fn name() -> &'static HeaderName { + &NAME + } + + fn decode<'i, I>(values: &mut I) -> Result + where + Self: Sized, + I: Iterator, + { + let value = values.next().ok_or_else(axum::headers::Error::invalid)?; + Ok(CiPwd( + value + .to_str() + .or(Err(axum::headers::Error::invalid()))? + .into(), + )) + } + + fn encode>(&self, values: &mut E) { + values.extend(std::iter::once(HeaderValue::from_str(&self.0).unwrap())) + } +} + +impl std::fmt::Display for CiPwd { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} diff --git a/axumtest/src/headers/cirole.rs b/axumtest/src/headers/cirole.rs new file mode 100644 index 0000000..3ac39c7 --- /dev/null +++ b/axumtest/src/headers/cirole.rs @@ -0,0 +1,39 @@ +//! Header parsing for X-CIROLE + +use axum::headers::Header; +use axum::headers::HeaderName; +use axum::headers::HeaderValue; + +static NAME: HeaderName = HeaderName::from_static("x-cirole"); + +pub struct CiRole(String); + +impl Header for CiRole { + fn name() -> &'static HeaderName { + &NAME + } + + fn decode<'i, I>(values: &mut I) -> Result + where + Self: Sized, + I: Iterator, + { + let value = values.next().ok_or_else(axum::headers::Error::invalid)?; + Ok(CiRole( + value + .to_str() + .or(Err(axum::headers::Error::invalid()))? + .into(), + )) + } + + fn encode>(&self, values: &mut E) { + values.extend(std::iter::once(HeaderValue::from_str(&self.0).unwrap())) + } +} + +impl std::fmt::Display for CiRole { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} diff --git a/axumtest/src/header/ciusr.rs b/axumtest/src/headers/ciusr.rs similarity index 100% rename from axumtest/src/header/ciusr.rs rename to axumtest/src/headers/ciusr.rs diff --git a/axumtest/src/headers/mod.rs b/axumtest/src/headers/mod.rs new file mode 100644 index 0000000..9469bda --- /dev/null +++ b/axumtest/src/headers/mod.rs @@ -0,0 +1,3 @@ +pub mod cipwd; +pub mod cirole; +pub mod ciusr; diff --git a/axumtest/src/main.rs b/axumtest/src/main.rs index c33703d..6e2d159 100644 --- a/axumtest/src/main.rs +++ b/axumtest/src/main.rs @@ -1,9 +1,11 @@ -mod header; +mod headers; use axum::routing::get; use axum::routing::Router; use axum::TypedHeader; -use header::ciusr::CiUsr; +use headers::cipwd::CiPwd; +use headers::cirole::CiRole; +use headers::ciusr::CiUsr; #[tokio::main] async fn main() { @@ -14,6 +16,10 @@ async fn main() { .unwrap(); } -async fn index(TypedHeader(usr): TypedHeader) -> String { +async fn index( + TypedHeader(usr): TypedHeader, + TypedHeader(pwd): TypedHeader, + TypedHeader(role): TypedHeader, +) -> String { format!("Hellow {}", usr) }