Files
aindustries.be/src/html/elements.rs

203 lines
4.0 KiB
Rust

use crate::html::Render;
#[allow(non_camel_case_types)]
pub(crate) struct h1 {
text: String,
}
impl Render for h1 {
fn render(&self) -> String {
format!("<h1>{}</h1>", self.text)
}
}
impl h1 {
pub(crate) fn new<T>(text: T) -> Self
where
T: Into<String>,
{
Self { text: text.into() }
}
}
#[allow(non_camel_case_types)]
pub(crate) struct h2 {
text: String,
}
impl Render for h2 {
fn render(&self) -> String {
format!("<h2>{}</h2>", self.text)
}
}
impl h2 {
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: String,
href: String,
}
impl Render for link {
fn render(&self) -> String {
format!("<link rel=\"{}\" href=\"{}\">", self.rel, self.href)
}
}
impl link {
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: String,
classes: Vec<String>,
content: Vec<Box<dyn Render>>,
}
impl Render for div {
fn render(&self) -> String {
let classes = self.classes.join(" ");
format!(
"<div id=\"{}\" class=\"{}\">{}</div>",
self.id,
classes,
self.content.render()
)
}
}
impl div {
pub(crate) fn new<T, U>(id: T, classes: Vec<U>) -> Self
where
T: Into<String>,
U: Into<String> + Clone,
{
Self {
id: id.into(),
classes: classes.iter().map(|x| x.clone().into()).collect(),
content: vec![],
}
}
pub(crate) fn append_element(&mut self, element: impl Render + 'static) {
self.content.push(Box::new(element));
}
}
#[allow(non_camel_case_types)]
pub(crate) struct p {
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
)
}
}
impl p {
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: id.into(),
classes: classes.iter().map(|x| x.clone().into()).collect(),
text: text.into(),
}
}
}
#[allow(non_camel_case_types)]
pub(crate) struct img {
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
)
}
}
impl img {
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: 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
)
}
}
impl a {
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(),
}
}
}