use std::collections::BTreeMap;
use async_trait::async_trait;
use bytes::Bytes;
use crate::error::ShadowError;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ResponseRecord {
pub ok: bool,
pub status: u16,
pub headers: BTreeMap<String, String>,
pub body: Bytes,
}
impl ResponseRecord {
pub fn ok(body: Vec<u8>) -> Self {
Self {
ok: true,
status: 200,
headers: BTreeMap::new(),
body: Bytes::from(body),
}
}
pub fn err(status: u16, body: Vec<u8>) -> Self {
Self {
ok: false,
status,
headers: BTreeMap::new(),
body: Bytes::from(body),
}
}
#[must_use]
pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.insert(key.into(), value.into());
self
}
}
#[async_trait]
pub trait Backend: Send + Sync {
async fn call(&self, input: &[u8]) -> Result<ResponseRecord, ShadowError>;
}