Update to Actix 4
Some checks failed
Core Repos/ImageApi/pipeline/pr-master There was a failure building this commit

This commit is contained in:
Cameron Cordes
2022-03-01 20:38:41 -05:00
parent 1e3f33c2d3
commit 69fe307516
8 changed files with 709 additions and 1074 deletions

View File

@@ -1,5 +1,6 @@
use actix_web::dev::{Body, ResponseBody};
use serde::Deserialize;
use actix_web::body::MessageBody;
use actix_web::{body::BoxBody, HttpResponse};
use serde::de::DeserializeOwned;
use crate::database::{models::User, UserDao};
use std::cell::RefCell;
@@ -55,32 +56,26 @@ impl UserDao for TestUserDao {
}
pub trait BodyReader {
fn read_to_str(&self) -> &str;
fn read_to_str(self) -> String;
}
impl BodyReader for ResponseBody<Body> {
fn read_to_str(&self) -> &str {
match self {
ResponseBody::Body(Body::Bytes(ref b)) => std::str::from_utf8(b).unwrap(),
_ => panic!("Unknown response body"),
}
impl BodyReader for HttpResponse<BoxBody> {
fn read_to_str(self) -> String {
let body = self.into_body().try_into_bytes().unwrap();
std::str::from_utf8(&body).unwrap().to_string()
}
}
pub trait TypedBodyReader<'a, T>
pub trait TypedBodyReader<T>
where
T: Deserialize<'a>,
T: DeserializeOwned,
{
fn read_body(&'a self) -> T;
fn read_body(self) -> T;
}
impl<'a, T: Deserialize<'a>> TypedBodyReader<'a, T> for ResponseBody<Body> {
fn read_body(&'a self) -> T {
match self {
ResponseBody::Body(Body::Bytes(ref b)) => {
serde_json::from_str(std::str::from_utf8(b).unwrap()).unwrap()
}
_ => panic!("Unknown response body"),
}
impl<T: DeserializeOwned> TypedBodyReader<T> for HttpResponse<BoxBody> {
fn read_body(self) -> T {
let body = self.read_to_str();
serde_json::from_value(serde_json::Value::String(body.clone())).unwrap()
}
}