Changed requirements for values that implement Into<String>
This commit is contained in:
@@ -12,8 +12,11 @@ impl Render for h1 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl h1 {
|
impl h1 {
|
||||||
pub(crate) fn new(text: String) -> Self {
|
pub(crate) fn new<T>(text: T) -> Self
|
||||||
Self { text }
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self { text: text.into() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,15 +32,18 @@ impl Render for h2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl h2 {
|
impl h2 {
|
||||||
pub(crate) fn new(text: String) -> Self {
|
pub(crate) fn new<T>(text: T) -> Self
|
||||||
Self { text }
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
Self { text: text.into() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub(crate) struct link {
|
pub(crate) struct link {
|
||||||
rel: &'static str,
|
rel: String,
|
||||||
href: &'static str,
|
href: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for link {
|
impl Render for link {
|
||||||
@@ -47,15 +53,22 @@ impl Render for link {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl link {
|
impl link {
|
||||||
pub(crate) fn new(rel: &'static str, href: &'static str) -> Self {
|
pub(crate) fn new<T, U>(rel: T, href: U) -> Self
|
||||||
Self { rel, href }
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
U: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
rel: rel.into(),
|
||||||
|
href: href.into(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub(crate) struct div {
|
pub(crate) struct div {
|
||||||
id: &'static str,
|
id: String,
|
||||||
classes: Vec<&'static str>,
|
classes: Vec<String>,
|
||||||
content: Vec<Box<dyn Render>>,
|
content: Vec<Box<dyn Render>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,10 +85,14 @@ impl Render for div {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl div {
|
impl div {
|
||||||
pub(crate) fn new(id: &'static str, classes: Vec<&'static str>) -> Self {
|
pub(crate) fn new<T, U>(id: T, classes: Vec<U>) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
U: Into<String> + Clone,
|
||||||
|
{
|
||||||
Self {
|
Self {
|
||||||
id,
|
id: id.into(),
|
||||||
classes,
|
classes: classes.iter().map(|x| x.clone().into()).collect(),
|
||||||
content: vec![],
|
content: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,65 +104,99 @@ impl div {
|
|||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub(crate) struct p {
|
pub(crate) struct p {
|
||||||
id: &'static str,
|
id: String,
|
||||||
classes: Vec<&'static str>,
|
classes: Vec<String>,
|
||||||
text: String
|
text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for p {
|
impl Render for p {
|
||||||
fn render(&self) -> String {
|
fn render(&self) -> String {
|
||||||
let classes = self.classes.join(" ");
|
let classes = self.classes.join(" ");
|
||||||
format!("<p id=\"{}\" class=\"{}\">{}</p>", self.id, classes, self.text)
|
format!(
|
||||||
|
"<p id=\"{}\" class=\"{}\">{}</p>",
|
||||||
|
self.id, classes, self.text
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl p {
|
impl p {
|
||||||
pub(crate) fn new(id: &'static str, classes: Vec<&'static str>, text: String) -> Self {
|
pub(crate) fn new<T, U, V>(id: T, classes: Vec<U>, text: V) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
U: Into<String> + Clone,
|
||||||
|
V: Into<String>,
|
||||||
|
{
|
||||||
Self {
|
Self {
|
||||||
id,
|
id: id.into(),
|
||||||
classes,
|
classes: classes.iter().map(|x| x.clone().into()).collect(),
|
||||||
text
|
text: text.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub(crate) struct img {
|
pub(crate) struct img {
|
||||||
id: &'static str,
|
id: String,
|
||||||
classes: Vec<&'static str>,
|
classes: Vec<String>,
|
||||||
src: &'static str,
|
src: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for img {
|
impl Render for img {
|
||||||
fn render(&self) -> String {
|
fn render(&self) -> String {
|
||||||
let classes = self.classes.join(" ");
|
let classes = self.classes.join(" ");
|
||||||
format!("<img id=\"{}\" class=\"{}\" src=\"{}\">", self.id, classes, self.src)
|
format!(
|
||||||
|
"<img id=\"{}\" class=\"{}\" src=\"{}\">",
|
||||||
|
self.id, classes, self.src
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl img {
|
impl img {
|
||||||
pub(crate) fn new(id: &'static str, classes: Vec<&'static str>, src: &'static str) -> Self {
|
pub(crate) fn new<T, U, V>(id: T, classes: Vec<U>, src: V) -> Self
|
||||||
Self { id, classes, src }
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
U: Into<String> + Clone,
|
||||||
|
V: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
id: id.into(),
|
||||||
|
classes: classes.iter().map(|x| x.clone().into()).collect(),
|
||||||
|
src: src.into(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub(crate) struct a {
|
pub(crate) struct a {
|
||||||
id: &'static str,
|
id: String,
|
||||||
classes: Vec<&'static str>,
|
classes: Vec<String>,
|
||||||
href: &'static str,
|
href: String,
|
||||||
text: String,
|
text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for a {
|
impl Render for a {
|
||||||
fn render(&self) -> String {
|
fn render(&self) -> String {
|
||||||
let classes = self.classes.join(" ");
|
let classes = self.classes.join(" ");
|
||||||
format!("<a id=\"{}\" class=\"{}\" href=\"{}\">{}</a>", self.id, classes, self.href, self.text)
|
format!(
|
||||||
|
"<a id=\"{}\" class=\"{}\" href=\"{}\">{}</a>",
|
||||||
|
self.id, classes, self.href, self.text
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl a {
|
impl a {
|
||||||
pub(crate) fn new(id: &'static str, classes: Vec<&'static str>, href: &'static str, text: String) -> Self {
|
pub(crate) fn new<T, U, V, W>(id: T, classes: Vec<U>, href: V, text: W) -> Self
|
||||||
Self { id, classes, href, text }
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
U: Into<String> + Clone,
|
||||||
|
V: Into<String>,
|
||||||
|
W: Into<String>,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
id: id.into(),
|
||||||
|
classes: classes.iter().map(|x| x.clone().into()).collect(),
|
||||||
|
href: href.into(),
|
||||||
|
text: text.into(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ pub(crate) trait Render {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Page {
|
pub(crate) struct Page {
|
||||||
title: &'static str,
|
title: String,
|
||||||
head: Vec<Box<dyn Render>>,
|
head: Vec<Box<dyn Render>>,
|
||||||
body: Vec<Box<dyn Render>>,
|
body: Vec<Box<dyn Render>>,
|
||||||
}
|
}
|
||||||
@@ -45,9 +45,12 @@ impl Render for Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Page {
|
impl Page {
|
||||||
pub(crate) fn new(title: &'static str) -> Self {
|
pub(crate) fn new<T>(title: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
Page {
|
Page {
|
||||||
title,
|
title: title.into(),
|
||||||
head: vec![],
|
head: vec![],
|
||||||
body: vec![],
|
body: vec![],
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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::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 futures_util::future::LocalBoxFuture;
|
||||||
|
use std::future::{Ready, ready};
|
||||||
|
|
||||||
pub(crate) struct MimeType {
|
pub(crate) struct MimeType {
|
||||||
mime_type: String,
|
mime_type: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MimeType {
|
impl MimeType {
|
||||||
pub(crate) fn new(mime_type: String) -> Self {
|
pub(crate) fn new<T>(mime_type: T) -> Self
|
||||||
|
where
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
Self {
|
Self {
|
||||||
mime_type
|
mime_type: mime_type.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S,B> Transform<S, ServiceRequest> for MimeType
|
impl<S, B> Transform<S, ServiceRequest> for MimeType
|
||||||
where
|
where
|
||||||
S: Service<ServiceRequest, Response=ServiceResponse<B>, Error=Error>,
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
B: 'static,
|
B: 'static,
|
||||||
{
|
{
|
||||||
@@ -30,7 +33,7 @@ where
|
|||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
let mime_type = self.mime_type.clone();
|
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<S> {
|
|||||||
mime_type: String,
|
mime_type: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S,B> Service<ServiceRequest> for MimeTypeMiddleware<S>
|
impl<S, B> Service<ServiceRequest> for MimeTypeMiddleware<S>
|
||||||
where
|
where
|
||||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
@@ -53,7 +56,7 @@ where
|
|||||||
|
|
||||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
let fut = self.service.call(req);
|
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 {
|
Box::pin(async move {
|
||||||
let mut res = fut.await?;
|
let mut res = fut.await?;
|
||||||
res.headers_mut().append(CONTENT_TYPE, val);
|
res.headers_mut().append(CONTENT_TYPE, val);
|
||||||
|
|||||||
Reference in New Issue
Block a user