use std::{error::Error, fmt::Display}; use actix_web::{ body::BoxBody, http::{ header::{HeaderName, HeaderValue}, StatusCode, }, HttpResponseBuilder, Responder, }; pub mod home; pub mod layout; pub mod node; struct HtmlTemplate(pub T); impl Responder for HtmlTemplate { type Body = BoxBody; fn respond_to(self, req: &actix_web::HttpRequest) -> actix_web::HttpResponse { let mut out = String::new(); self.0.render(&mut out).unwrap(); HttpResponseBuilder::new(StatusCode::OK) .body(out) .respond_to(req) } } pub struct ContentType(pub &'static str, pub T); impl Responder for ContentType { type Body = T::Body; fn respond_to(self, req: &actix_web::HttpRequest) -> actix_web::HttpResponse { let mut r = self.1.respond_to(req); r.headers_mut().insert( HeaderName::from_static("content-type"), HeaderValue::from_static(self.0), ); r } } pub type MyResult = actix_web::Result; #[derive(Debug)] pub struct MyError(anyhow::Error); impl Responder for MyError { type Body = BoxBody; fn respond_to(self, req: &actix_web::HttpRequest) -> actix_web::HttpResponse { HttpResponseBuilder::new(StatusCode::BAD_REQUEST) .body(format!("error: {}", self.0)) .respond_to(req) } } impl actix_web::error::ResponseError for MyError { fn status_code(&self) -> StatusCode { StatusCode::BAD_REQUEST } } impl Display for MyError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } impl From for MyError { fn from(err: anyhow::Error) -> MyError { MyError(err) } } impl From for MyError { fn from(err: std::fmt::Error) -> MyError { MyError(anyhow::anyhow!("{err}")) } }