use std::fmt::{self, Debug, Display, Formatter};
use std::ops::{Deref, DerefMut};
use serde::{Deserialize, Deserializer};
use crate::Depot;
use crate::extract::{Extractible, Metadata};
use crate::http::{ParseError, Request};
use crate::serde::from_str_val;
pub struct CookieParam<T, const REQUIRED: bool = true>(Option<T>);
fn parse_cookie_value<'de, T>(value: &'de str) -> Option<T>
where
T: Deserialize<'de>,
{
let trimmed = value.trim_start();
let looks_like_json = trimmed.starts_with('{')
|| trimmed.starts_with('[')
|| trimmed.starts_with('"')
|| trimmed
.chars()
.next()
.is_some_and(|c| c.is_ascii_digit() || matches!(c, '-' | 't' | 'f' | 'n'));
if looks_like_json {
if let Ok(v) = serde_json::from_str::<T>(value) {
return Some(v);
}
}
from_str_val(value).ok()
}
impl<T> CookieParam<T, true> {
pub fn into_inner(self) -> T {
self.0
.expect("`CookieParam<T, true>` into_inner get `None`")
}
}
impl<T> CookieParam<T, false> {
pub fn into_inner(self) -> Option<T> {
self.0
}
}
impl<T> Deref for CookieParam<T, true> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.0
.as_ref()
.expect("`CookieParam<T, true>` deref get `None`")
}
}
impl<T> Deref for CookieParam<T, false> {
type Target = Option<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for CookieParam<T, true> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0
.as_mut()
.expect("`CookieParam<T, true>` deref_mut get `None`")
}
}
impl<T> DerefMut for CookieParam<T, false> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<'de, T, const R: bool> Deserialize<'de> for CookieParam<T, R>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
T::deserialize(deserializer).map(|value| Self(Some(value)))
}
}
impl<T: Debug, const R: bool> fmt::Debug for CookieParam<T, R> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<T: Display> Display for CookieParam<T, true> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.0
.as_ref()
.expect("`CookieParam<T, true>` value should not be None")
.fmt(f)
}
}
impl<'ex, T> Extractible<'ex> for CookieParam<T, true>
where
T: Deserialize<'ex>,
{
fn metadata() -> &'static Metadata {
static METADATA: Metadata = Metadata::new("");
&METADATA
}
#[allow(refining_impl_trait)]
async fn extract(_req: &'ex mut Request, _depot: &'ex mut Depot) -> Result<Self, ParseError> {
unimplemented!("cookie parameter can not be extracted from request");
}
#[allow(refining_impl_trait)]
async fn extract_with_arg(
req: &'ex mut Request,
_depot: &'ex mut Depot,
arg: &str,
) -> Result<Self, ParseError> {
let value = req
.cookies()
.get(arg)
.and_then(|v| parse_cookie_value(v.value()))
.ok_or_else(|| {
ParseError::other(format!(
"cookie parameter {arg} not found or convert to type failed"
))
})?;
Ok(Self(value))
}
}
impl<'ex, T> Extractible<'ex> for CookieParam<T, false>
where
T: Deserialize<'ex>,
{
fn metadata() -> &'static Metadata {
static METADATA: Metadata = Metadata::new("");
&METADATA
}
#[allow(refining_impl_trait)]
async fn extract(_req: &'ex mut Request, _depot: &'ex mut Depot) -> Result<Self, ParseError> {
unimplemented!("cookie parameter can not be extracted from request")
}
#[allow(refining_impl_trait)]
async fn extract_with_arg(
req: &'ex mut Request,
_depot: &'ex mut Depot,
arg: &str,
) -> Result<Self, ParseError> {
let value = req
.cookies()
.get(arg)
.and_then(|v| parse_cookie_value(v.value()));
Ok(Self(value))
}
}
#[cfg(test)]
mod tests {
use http::header::HeaderValue;
use serde::Serialize;
use super::*;
use crate::test::TestClient;
#[test]
fn test_required_cookie_param_into_inner() {
let param = CookieParam::<String, true>(Some("param".to_owned()));
assert_eq!("param".to_owned(), param.into_inner());
}
#[test]
fn test_required_cookie_param_deref() {
let param = CookieParam::<String, true>(Some("param".to_owned()));
assert_eq!(&"param".to_owned(), param.deref())
}
#[test]
fn test_required_cookie_param_deref_mut() {
let mut param = CookieParam::<String, true>(Some("param".to_owned()));
assert_eq!(&mut "param".to_owned(), param.deref_mut())
}
#[test]
fn test_cookie_param_into_inner() {
let param = CookieParam::<String, false>(Some("param".to_owned()));
assert_eq!(Some("param".to_owned()), param.into_inner());
}
#[test]
fn test_cookie_param_deref() {
let param = CookieParam::<String, false>(Some("param".to_owned()));
assert_eq!(&Some("param".to_owned()), param.deref())
}
#[test]
fn test_cookie_param_deref_mut() {
let mut param = CookieParam::<String, false>(Some("param".to_owned()));
assert_eq!(&mut Some("param".to_owned()), param.deref_mut())
}
#[test]
fn test_cookie_param_deserialize() {
let param = serde_json::from_str::<CookieParam<String, true>>(r#""param""#).unwrap();
assert_eq!(param.0.unwrap(), "param");
}
#[test]
fn test_cookie_param_debug() {
let param = CookieParam::<String, true>(Some("param".to_owned()));
assert_eq!(format!("{param:?}"), r#"Some("param")"#);
}
#[test]
fn test_cookie_param_display() {
let param = CookieParam::<String, true>(Some("param".to_owned()));
assert_eq!(format!("{param}"), "param");
}
#[test]
fn test_required_cookie_param_metadata() {
let metadata = CookieParam::<String, true>::metadata();
assert_eq!("", metadata.name);
}
#[tokio::test]
#[should_panic]
async fn test_required_cookie_param_extract() {
let mut req = Request::new();
let mut depot = Depot::new();
let _ = CookieParam::<String, true>::extract(&mut req, &mut depot).await;
}
#[tokio::test]
async fn test_required_cookie_param_extract_with_value() {
let mut req = TestClient::get("http://127.0.0.1:5801").build_hyper();
req.headers_mut()
.append("cookie", HeaderValue::from_static("param=param"));
let schema = req.uri().scheme().cloned().unwrap();
let mut req = Request::from_hyper(req, schema);
let mut depot = Depot::new();
let result =
CookieParam::<String, true>::extract_with_arg(&mut req, &mut depot, "param").await;
assert_eq!(result.unwrap().0.unwrap(), "param");
}
#[tokio::test]
async fn test_required_cookie_param_extract_with_json_value() {
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct AuthToken {
pkce_verifier: String,
csrf_token: String,
}
let expected = AuthToken {
pkce_verifier: "verifier".into(),
csrf_token: "csrf".into(),
};
let cookie = cookie::Cookie::new(
"token",
serde_json::to_string(&expected).expect("serialize cookie value"),
);
let mut req = TestClient::get("http://127.0.0.1:5801")
.add_header("cookie", cookie.encoded().to_string(), true)
.build();
let mut depot = Depot::new();
let result =
CookieParam::<AuthToken, true>::extract_with_arg(&mut req, &mut depot, "token")
.await
.unwrap();
assert_eq!(result.into_inner(), expected);
}
#[tokio::test]
async fn test_required_cookie_param_extract_json_value_as_value() {
let cookie = cookie::Cookie::new("data", r#"{"x":1}"#);
let mut req = TestClient::get("http://127.0.0.1:5801")
.add_header("cookie", cookie.encoded().to_string(), true)
.build();
let mut depot = Depot::new();
let result =
CookieParam::<serde_json::Value, true>::extract_with_arg(&mut req, &mut depot, "data")
.await
.unwrap();
assert_eq!(result.into_inner(), serde_json::json!({"x": 1}));
}
#[tokio::test]
async fn test_required_cookie_param_extract_untagged_enum_string_not_matched_by_json() {
#[derive(Debug, Deserialize, PartialEq)]
#[serde(untagged)]
enum StringOrStruct {
String(String),
Struct { x: i32 },
}
let cookie = cookie::Cookie::new("data", r#"{"x":1}"#);
let mut req = TestClient::get("http://127.0.0.1:5801")
.add_header("cookie", cookie.encoded().to_string(), true)
.build();
let mut depot = Depot::new();
let result =
CookieParam::<StringOrStruct, true>::extract_with_arg(&mut req, &mut depot, "data")
.await
.unwrap();
assert_eq!(result.into_inner(), StringOrStruct::Struct { x: 1 });
}
#[tokio::test]
#[should_panic]
async fn test_required_cookie_param_extract_with_value_panic() {
let req = TestClient::get("http://127.0.0.1:5801").build_hyper();
let schema = req.uri().scheme().cloned().unwrap();
let mut req = Request::from_hyper(req, schema);
let mut depot = Depot::new();
let result =
CookieParam::<String, true>::extract_with_arg(&mut req, &mut depot, "param").await;
assert_eq!(result.unwrap().0.unwrap(), "param");
}
#[test]
fn test_cookie_param_metadata() {
let metadata = CookieParam::<String, false>::metadata();
assert_eq!("", metadata.name);
}
#[tokio::test]
#[should_panic]
async fn test_cookie_param_extract() {
let mut req = Request::new();
let mut depot = Depot::new();
let _ = CookieParam::<String, false>::extract(&mut req, &mut depot).await;
}
#[tokio::test]
async fn test_cookie_param_extract_with_value() {
let mut req = TestClient::get("http://127.0.0.1:5801").build_hyper();
req.headers_mut()
.append("cookie", HeaderValue::from_static("param=param"));
let schema = req.uri().scheme().cloned().unwrap();
let mut req = Request::from_hyper(req, schema);
let mut depot = Depot::new();
let result =
CookieParam::<String, false>::extract_with_arg(&mut req, &mut depot, "param").await;
assert_eq!(result.unwrap().0.unwrap(), "param");
}
#[tokio::test]
#[should_panic]
async fn test_cookie_param_extract_with_value_panic() {
let req = TestClient::get("http://127.0.0.1:5801").build_hyper();
let schema = req.uri().scheme().cloned().unwrap();
let mut req = Request::from_hyper(req, schema);
let mut depot = Depot::new();
let result =
CookieParam::<String, false>::extract_with_arg(&mut req, &mut depot, "param").await;
assert_eq!(result.unwrap().0.unwrap(), "param");
}
}