diff --git a/src/html/elements.rs b/src/html/elements.rs index 2de304c..570b61c 100644 --- a/src/html/elements.rs +++ b/src/html/elements.rs @@ -12,8 +12,11 @@ impl Render for h1 { } impl h1 { - pub(crate) fn new(text: String) -> Self { - Self { text } + pub(crate) fn new(text: T) -> Self + where + T: Into, + { + Self { text: text.into() } } } @@ -29,15 +32,18 @@ impl Render for h2 { } impl h2 { - pub(crate) fn new(text: String) -> Self { - Self { text } + pub(crate) fn new(text: T) -> Self + where + T: Into, + { + Self { text: text.into() } } } #[allow(non_camel_case_types)] pub(crate) struct link { - rel: &'static str, - href: &'static str, + rel: String, + href: String, } impl Render for link { @@ -47,15 +53,22 @@ impl Render for link { } impl link { - pub(crate) fn new(rel: &'static str, href: &'static str) -> Self { - Self { rel, href } + pub(crate) fn new(rel: T, href: U) -> Self + where + T: Into, + U: Into, + { + Self { + rel: rel.into(), + href: href.into(), + } } } #[allow(non_camel_case_types)] pub(crate) struct div { - id: &'static str, - classes: Vec<&'static str>, + id: String, + classes: Vec, content: Vec>, } @@ -72,10 +85,14 @@ impl Render for div { } impl div { - pub(crate) fn new(id: &'static str, classes: Vec<&'static str>) -> Self { + pub(crate) fn new(id: T, classes: Vec) -> Self + where + T: Into, + U: Into + Clone, + { Self { - id, - classes, + id: id.into(), + classes: classes.iter().map(|x| x.clone().into()).collect(), content: vec![], } } @@ -87,65 +104,99 @@ impl div { #[allow(non_camel_case_types)] pub(crate) struct p { - id: &'static str, - classes: Vec<&'static str>, - text: String + id: String, + classes: Vec, + text: String, } impl Render for p { fn render(&self) -> String { let classes = self.classes.join(" "); - format!("

{}

", self.id, classes, self.text) + format!( + "

{}

", + self.id, classes, self.text + ) } } impl p { - pub(crate) fn new(id: &'static str, classes: Vec<&'static str>, text: String) -> Self { + pub(crate) fn new(id: T, classes: Vec, text: V) -> Self + where + T: Into, + U: Into + Clone, + V: Into, + { Self { - id, - classes, - text + id: id.into(), + classes: classes.iter().map(|x| x.clone().into()).collect(), + text: text.into(), } } } #[allow(non_camel_case_types)] pub(crate) struct img { - id: &'static str, - classes: Vec<&'static str>, - src: &'static str, + id: String, + classes: Vec, + src: String, } impl Render for img { fn render(&self) -> String { let classes = self.classes.join(" "); - format!("", self.id, classes, self.src) + format!( + "", + self.id, classes, self.src + ) } } impl img { - pub(crate) fn new(id: &'static str, classes: Vec<&'static str>, src: &'static str) -> Self { - Self { id, classes, src } + pub(crate) fn new(id: T, classes: Vec, src: V) -> Self + where + T: Into, + U: Into + Clone, + V: Into, + { + Self { + id: id.into(), + classes: classes.iter().map(|x| x.clone().into()).collect(), + src: src.into(), + } } } #[allow(non_camel_case_types)] pub(crate) struct a { - id: &'static str, - classes: Vec<&'static str>, - href: &'static str, + id: String, + classes: Vec, + href: String, text: String, } impl Render for a { fn render(&self) -> String { let classes = self.classes.join(" "); - format!("{}", self.id, classes, self.href, self.text) + format!( + "{}", + self.id, classes, self.href, self.text + ) } } impl a { - pub(crate) fn new(id: &'static str, classes: Vec<&'static str>, href: &'static str, text: String) -> Self { - Self { id, classes, href, text } + pub(crate) fn new(id: T, classes: Vec, href: V, text: W) -> Self + where + T: Into, + U: Into + Clone, + V: Into, + W: Into, + { + Self { + id: id.into(), + classes: classes.iter().map(|x| x.clone().into()).collect(), + href: href.into(), + text: text.into(), + } } -} \ No newline at end of file +} diff --git a/src/html/mod.rs b/src/html/mod.rs index 1c8a963..242037d 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -5,7 +5,7 @@ pub(crate) trait Render { } pub(crate) struct Page { - title: &'static str, + title: String, head: Vec>, body: Vec>, } @@ -45,9 +45,12 @@ impl Render for Page { } impl Page { - pub(crate) fn new(title: &'static str) -> Self { + pub(crate) fn new(title: T) -> Self + where + T: Into, + { Page { - title, + title: title.into(), head: vec![], body: vec![], } diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index bf0aac1..bf084ad 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -1,24 +1,27 @@ -use std::future::{ready, Ready}; -use actix_web::dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}; use actix_web::Error; -use actix_web::http::header::{HeaderValue, CONTENT_TYPE}; +use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform, forward_ready}; +use actix_web::http::header::{CONTENT_TYPE, HeaderValue}; use futures_util::future::LocalBoxFuture; +use std::future::{Ready, ready}; pub(crate) struct MimeType { mime_type: String, } impl MimeType { - pub(crate) fn new(mime_type: String) -> Self { + pub(crate) fn new(mime_type: T) -> Self + where + T: Into, + { Self { - mime_type + mime_type: mime_type.into(), } } } -impl Transform for MimeType +impl Transform for MimeType where - S: Service, Error=Error>, + S: Service, Error = Error>, S::Future: 'static, B: 'static, { @@ -30,7 +33,7 @@ where fn new_transform(&self, service: S) -> Self::Future { let mime_type = self.mime_type.clone(); - ready(Ok(MimeTypeMiddleware{service, mime_type})) + ready(Ok(MimeTypeMiddleware { service, mime_type })) } } @@ -39,7 +42,7 @@ pub(crate) struct MimeTypeMiddleware { mime_type: String, } -impl Service for MimeTypeMiddleware +impl Service for MimeTypeMiddleware where S: Service, Error = Error>, S::Future: 'static, @@ -53,11 +56,11 @@ where fn call(&self, req: ServiceRequest) -> Self::Future { let fut = self.service.call(req); - let val = HeaderValue::from_str(self.mime_type.as_str()).unwrap_or_else(|_| HeaderValue::from_static("text/html")); + let val = HeaderValue::from_str(self.mime_type.as_str()).expect("Invalid MimeType"); Box::pin(async move { let mut res = fut.await?; res.headers_mut().append(CONTENT_TYPE, val); Ok(res) }) } -} \ No newline at end of file +}