use std::{fmt::Display, str::FromStr};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use webparse::Response;
use wenmeng::{RecvRequest, ProtResult, RecvResponse};
use crate::{Helper, ProxyError};
#[serde_as]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StaticResponse {
body: &'static str,
}
impl StaticResponse {
pub async fn deal_request(&self, req: &mut RecvRequest) -> ProtResult<RecvResponse> {
let val = Helper::format_req(req, self.body);
return Ok(Response::text().body(val).unwrap().into_type());
}
}
impl FromStr for StaticResponse {
type Err = ProxyError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(StaticResponse {
body: Helper::get_static_str(s),
})
}
}
impl Display for StaticResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.body)
}
}