use crate::error::FrameworkError;
use bytes::Bytes;
use http_body_util::BodyExt;
use hyper::body::Incoming;
use serde::de::DeserializeOwned;
pub async fn collect_body(body: Incoming) -> Result<Bytes, FrameworkError> {
body.collect()
.await
.map(|collected| collected.to_bytes())
.map_err(|e| FrameworkError::internal(format!("Failed to read request body: {e}")))
}
pub fn parse_json<T: DeserializeOwned>(bytes: &Bytes) -> Result<T, FrameworkError> {
serde_json::from_slice(bytes)
.map_err(|e| FrameworkError::internal(format!("Failed to parse JSON body: {e}")))
}
pub fn parse_form<T: DeserializeOwned>(bytes: &Bytes) -> Result<T, FrameworkError> {
serde_urlencoded::from_bytes(bytes)
.map_err(|e| FrameworkError::internal(format!("Failed to parse form body: {e}")))
}