use crate::error::{Error, Result};
use crate::http::FormValue;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum BodyData {
Form(HashMap<String, FormValue>),
Json(serde_json::Value),
Text(String),
Raw(Vec<u8>),
Empty,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestData {
pub query: HashMap<String, String>,
pub params: HashMap<String, String>,
pub body: BodyData,
pub headers: HashMap<String, String>,
pub method: String,
pub uri: String,
}
impl RequestData {
pub fn new(
query: HashMap<String, String>,
params: HashMap<String, String>,
body: BodyData,
headers: HashMap<String, String>,
method: String,
uri: String,
) -> Self {
Self {
query,
params,
body,
headers,
method,
uri,
}
}
pub fn str_query(&self, key: &str) -> Result<String> {
self.query
.get(key)
.filter(|s| !s.is_empty())
.cloned()
.ok_or_else(|| Error::InvalidInput(format!("Query parameter '{}' is required", key)))
}
pub fn int_query(&self, key: &str) -> Result<i32> {
self.str_query(key)?.parse().map_err(|_| {
Error::InvalidInput(format!("Query parameter '{}' must be a valid integer", key))
})
}
pub fn bool_query(&self, key: &str) -> Result<bool> {
let value = self.str_query(key)?;
Ok(matches!(value.as_str(), "true" | "1" | "yes" | "on"))
}
pub fn str_query_or(&self, key: &str, default: &str) -> String {
self.str_query(key).unwrap_or_else(|_| default.to_string())
}
pub fn int_query_or(&self, key: &str, default: i32) -> i32 {
self.int_query(key).unwrap_or(default)
}
pub fn bool_query_or(&self, key: &str, default: bool) -> bool {
self.bool_query(key).unwrap_or(default)
}
pub fn str_param(&self, key: &str) -> Result<String> {
self.params
.get(key)
.filter(|s| !s.is_empty())
.cloned()
.ok_or_else(|| Error::InvalidInput(format!("Route parameter '{}' is required", key)))
}
pub fn int_param(&self, key: &str) -> Result<i32> {
self.str_param(key)?.parse().map_err(|_| {
Error::InvalidInput(format!("Route parameter '{}' must be a valid integer", key))
})
}
pub fn str_param_or(&self, key: &str, default: &str) -> String {
self.str_param(key).unwrap_or_else(|_| default.to_string())
}
pub fn int_param_or(&self, key: &str, default: i32) -> i32 {
self.int_param(key).unwrap_or(default)
}
pub fn str_body(&self, key: &str) -> Result<String> {
match &self.body {
BodyData::Form(form) => form
.get(key)
.map(|v| v.as_string().to_string())
.filter(|s| !s.is_empty())
.ok_or_else(|| Error::InvalidInput(format!("Field '{}' is required", key))),
BodyData::Json(json) => json
.get(key)
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.filter(|s| !s.is_empty())
.ok_or_else(|| Error::InvalidInput(format!("Field '{}' is required", key))),
_ => Err(Error::InvalidInput(format!(
"Field '{}' not found in body",
key
))),
}
}
pub fn int_body(&self, key: &str) -> Result<i32> {
match &self.body {
BodyData::Form(form) => form
.get(key)
.ok_or_else(|| Error::InvalidInput(format!("Field '{}' is required", key)))?
.as_string()
.parse()
.map_err(|_| {
Error::InvalidInput(format!("Field '{}' must be a valid integer", key))
}),
BodyData::Json(json) => json
.get(key)
.ok_or_else(|| Error::InvalidInput(format!("Field '{}' is required", key)))?
.as_i64()
.map(|n| n as i32)
.or_else(|| json.get(key)?.as_str()?.parse().ok())
.ok_or_else(|| {
Error::InvalidInput(format!("Field '{}' must be a valid integer", key))
}),
_ => Err(Error::InvalidInput(format!(
"Field '{}' not found in body",
key
))),
}
}
pub fn bool_body(&self, key: &str) -> Result<bool> {
match &self.body {
BodyData::Form(form) => {
let value = form
.get(key)
.ok_or_else(|| Error::InvalidInput(format!("Field '{}' is required", key)))?
.as_string();
Ok(matches!(value, "true" | "1" | "yes" | "on" | "checked"))
}
BodyData::Json(json) => json
.get(key)
.ok_or_else(|| Error::InvalidInput(format!("Field '{}' is required", key)))?
.as_bool()
.or_else(|| {
json.get(key)?
.as_str()
.map(|s| matches!(s, "true" | "1" | "yes" | "on"))
})
.ok_or_else(|| Error::InvalidInput(format!("Field '{}' must be a boolean", key))),
_ => Err(Error::InvalidInput(format!(
"Field '{}' not found in body",
key
))),
}
}
pub fn str_body_or(&self, key: &str, default: &str) -> String {
self.str_body(key).unwrap_or_else(|_| default.to_string())
}
pub fn int_body_or(&self, key: &str, default: i32) -> i32 {
self.int_body(key).unwrap_or(default)
}
pub fn bool_body_or(&self, key: &str, default: bool) -> bool {
self.bool_body(key).unwrap_or(default)
}
pub fn form_data(&self) -> HashMap<String, String> {
match &self.body {
BodyData::Form(form) => form
.iter()
.map(|(k, v)| (k.clone(), v.as_string().to_string()))
.collect(),
_ => HashMap::new(),
}
}
pub fn is_form(&self) -> bool {
matches!(&self.body, BodyData::Form(_))
}
pub fn is_json(&self) -> bool {
matches!(&self.body, BodyData::Json(_))
}
}