Changed requirements for values that implement Into<String>

This commit is contained in:
2025-10-06 22:31:41 +02:00
parent e63edda9ca
commit bcd50d3a17
3 changed files with 105 additions and 48 deletions

View File

@@ -12,8 +12,11 @@ impl Render for h1 {
}
impl h1 {
pub(crate) fn new(text: String) -> Self {
Self { text }
pub(crate) fn new<T>(text: T) -> Self
where
T: Into<String>,
{
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<T>(text: T) -> Self
where
T: Into<String>,
{
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<T, U>(rel: T, href: U) -> Self
where
T: Into<String>,
U: Into<String>,
{
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<String>,
content: Vec<Box<dyn Render>>,
}
@@ -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<T, U>(id: T, classes: Vec<U>) -> Self
where
T: Into<String>,
U: Into<String> + 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<String>,
text: String,
}
impl Render for p {
fn render(&self) -> String {
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 {
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 {
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<String>,
src: String,
}
impl Render for img {
fn render(&self) -> String {
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 {
pub(crate) fn new(id: &'static str, classes: Vec<&'static str>, src: &'static str) -> Self {
Self { id, classes, src }
pub(crate) fn new<T, U, V>(id: T, classes: Vec<U>, src: V) -> Self
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)]
pub(crate) struct a {
id: &'static str,
classes: Vec<&'static str>,
href: &'static str,
id: String,
classes: Vec<String>,
href: String,
text: String,
}
impl Render for a {
fn render(&self) -> String {
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 {
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<T, U, V, W>(id: T, classes: Vec<U>, href: V, text: W) -> Self
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(),
}
}
}

View File

@@ -5,7 +5,7 @@ pub(crate) trait Render {
}
pub(crate) struct Page {
title: &'static str,
title: String,
head: Vec<Box<dyn Render>>,
body: Vec<Box<dyn Render>>,
}
@@ -45,9 +45,12 @@ impl Render for Page {
}
impl Page {
pub(crate) fn new(title: &'static str) -> Self {
pub(crate) fn new<T>(title: T) -> Self
where
T: Into<String>,
{
Page {
title,
title: title.into(),
head: vec![],
body: vec![],
}

View File

@@ -1,17 +1,20 @@
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<T>(mime_type: T) -> Self
where
T: Into<String>,
{
Self {
mime_type
mime_type: mime_type.into(),
}
}
}
@@ -53,7 +56,7 @@ 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);