Files
aindustries.be/src/html/elements.rs
2025-11-24 21:21:36 +01:00

505 lines
10 KiB
Rust

use crate::html::Render;
pub(crate) struct Heading {
level: u8,
id: String,
text: String,
classes: Vec<String>,
}
impl Render for Heading {
fn render(&self) -> String {
let classes = self.classes.join(" ");
format!(
"<h{} id=\"{}\" class=\"{}\">{}</h{}>",
self.level, self.id, classes, self.text, self.level
)
}
}
impl Heading {
pub(crate) fn builder() -> HeadingBuilder {
HeadingBuilder::new()
}
}
pub(crate) struct HeadingBuilder {
level: Option<u8>,
id: Option<String>,
text: Option<String>,
classes: Option<Vec<String>>,
}
impl HeadingBuilder {
pub(crate) fn new() -> Self {
Self {
level: None,
id: None,
text: None,
classes: None,
}
}
pub(crate) fn level(self, level: u8) -> Self {
Self {
level: Some(level),
..self
}
}
pub(crate) fn id<T>(self, id: T) -> Self
where
T: Into<String>,
{
Self {
id: Some(id.into()),
..self
}
}
pub(crate) fn text<T>(self, text: T) -> Self
where
T: Into<String>,
{
Self {
text: Some(text.into()),
..self
}
}
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
where
T: Into<String>,
{
Self {
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
..self
}
}
pub(crate) fn build(self) -> Heading {
let level = self.level.unwrap_or(1);
let id = self.id.unwrap_or(String::new());
let text = self.text.unwrap_or(String::new());
let classes = self.classes.unwrap_or(Vec::new());
Heading {
level,
id,
text,
classes,
}
}
}
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 builder() -> LinkBuilder {
LinkBuilder::new()
}
}
pub(crate) struct LinkBuilder {
rel: Option<String>,
href: Option<String>,
}
impl LinkBuilder {
pub(crate) fn new() -> Self {
Self {
rel: None,
href: None,
}
}
pub(crate) fn rel<T>(self, rel: T) -> Self
where
T: Into<String>,
{
Self {
rel: Some(rel.into()),
..self
}
}
pub(crate) fn href<T>(self, href: T) -> Self
where
T: Into<String>,
{
Self {
href: Some(href.into()),
..self
}
}
pub(crate) fn build(self) -> Link {
let rel = self.rel.unwrap_or(String::new());
let href = self.href.unwrap_or(String::new());
Link { rel, href }
}
}
pub(crate) struct Paragraph {
id: String,
classes: Vec<String>,
text: String,
}
impl Render for Paragraph {
fn render(&self) -> String {
let classes = self.classes.join(" ");
format!(
"<p id=\"{}\" class=\"{}\">{}</p>",
self.id, classes, self.text
)
}
}
impl Paragraph {
pub(crate) fn builder() -> ParagraphBuilder {
ParagraphBuilder::new()
}
}
pub(crate) struct ParagraphBuilder {
id: Option<String>,
classes: Option<Vec<String>>,
text: Option<String>,
}
impl ParagraphBuilder {
pub(crate) fn new() -> Self {
Self {
id: None,
classes: None,
text: None,
}
}
pub(crate) fn id<T>(self, id: T) -> Self
where
T: Into<String>,
{
Self {
id: Some(id.into()),
..self
}
}
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
where
T: Into<String>,
{
Self {
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
..self
}
}
pub(crate) fn text<T>(self, text: T) -> Self
where
T: Into<String>,
{
Self {
text: Some(text.into()),
..self
}
}
pub(crate) fn build(self) -> Paragraph {
let id = self.id.unwrap_or(String::new());
let classes = self.classes.unwrap_or(Vec::new());
let text = self.text.unwrap_or(String::new());
Paragraph { id, classes, text }
}
}
pub(crate) struct Image {
id: String,
classes: Vec<String>,
src: String,
}
impl Render for Image {
fn render(&self) -> String {
let classes = self.classes.join(" ");
format!(
"<img id=\"{}\" class=\"{}\" src=\"{}\">",
self.id, classes, self.src
)
}
}
impl Image {
pub(crate) fn builder() -> ImageBuilder {
ImageBuilder::new()
}
}
pub(crate) struct ImageBuilder {
id: Option<String>,
classes: Option<Vec<String>>,
src: Option<String>,
}
impl ImageBuilder {
pub(crate) fn new() -> Self {
Self {
id: None,
classes: None,
src: None,
}
}
pub(crate) fn id<T>(self, id: T) -> Self
where
T: Into<String>,
{
Self {
id: Some(id.into()),
..self
}
}
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
where
T: Into<String>,
{
Self {
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
..self
}
}
pub(crate) fn src<T>(self, src: T) -> Self
where
T: Into<String>,
{
Self {
src: Some(src.into()),
..self
}
}
pub(crate) fn build(self) -> Image {
let id = self.id.unwrap_or(String::new());
let classes = self.classes.unwrap_or(Vec::new());
let src = self.src.unwrap_or(String::new());
Image { id, classes, src }
}
}
pub(crate) struct Anchor {
id: String,
classes: Vec<String>,
href: String,
text: String,
}
impl Render for Anchor {
fn render(&self) -> String {
let classes = self.classes.join(" ");
format!(
"<a id=\"{}\" class=\"{}\" href=\"{}\">{}</a>",
self.id, classes, self.href, self.text
)
}
}
impl Anchor {
pub(crate) fn builder() -> AnchorBuilder {
AnchorBuilder::new()
}
}
pub(crate) struct AnchorBuilder {
id: Option<String>,
classes: Option<Vec<String>>,
href: Option<String>,
text: Option<String>,
}
impl AnchorBuilder {
pub(crate) fn new() -> Self {
Self {
id: None,
classes: None,
href: None,
text: None,
}
}
pub(crate) fn id<T>(self, id: T) -> Self
where
T: Into<String>,
{
Self {
id: Some(id.into()),
..self
}
}
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
where
T: Into<String>,
{
Self {
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
..self
}
}
pub(crate) fn href<T>(self, href: T) -> Self
where
T: Into<String>,
{
Self {
href: Some(href.into()),
..self
}
}
pub(crate) fn text<T>(self, text: T) -> Self
where
T: Into<String>,
{
Self {
text: Some(text.into()),
..self
}
}
pub(crate) fn build(self) -> Anchor {
let id = self.id.unwrap_or(String::new());
let classes = self.classes.unwrap_or(Vec::new());
let href = self.href.unwrap_or(String::new());
let text = self.text.unwrap_or(String::new());
Anchor {
id,
classes,
href,
text,
}
}
}
pub(crate) struct Iframe {
id: String,
classes: Vec<String>,
src: String,
title: String,
width: usize,
height: usize,
}
impl Render for Iframe {
fn render(&self) -> String {
let classes = self.classes.join(" ");
format!(
"<iframe id=\"{}\" classes=\"{}\" title=\"{}\" width=\"{}\" height=\"{}\" src=\"{}\"></iframe>",
self.id, classes, self.title, self.width, self.height, self.src
)
}
}
pub(crate) struct IframeBuilder {
id: Option<String>,
classes: Option<Vec<String>>,
src: Option<String>,
title: Option<String>,
width: Option<usize>,
height: Option<usize>,
}
impl IframeBuilder {
pub(crate) fn new() -> Self {
Self {
id: None,
classes: None,
src: None,
title: None,
width: None,
height: None,
}
}
pub(crate) fn id<T>(self, id: T) -> Self
where
T: Into<String>,
{
Self {
id: Some(id.into()),
..self
}
}
pub(crate) fn classes<T>(self, classes: Vec<T>) -> Self
where
T: Into<String>,
{
Self {
classes: Some(classes.into_iter().map(|x| x.into()).collect()),
..self
}
}
pub(crate) fn src<T>(self, src: T) -> Self
where
T: Into<String>,
{
Self {
src: Some(src.into()),
..self
}
}
pub(crate) fn title<T>(self, title: T) -> Self
where
T: Into<String>,
{
Self {
title: Some(title.into()),
..self
}
}
pub(crate) fn width(self, width: usize) -> Self {
Self {
width: Some(width),
..self
}
}
pub(crate) fn height(self, height: usize) -> Self {
Self {
height: Some(height),
..self
}
}
pub(crate) fn build(self) -> Iframe {
let id = self.id.unwrap_or(String::new());
let classes = self.classes.unwrap_or(Vec::new());
let title = self.title.unwrap_or(String::new());
let width = self.width.unwrap_or(0);
let height = self.height.unwrap_or(0);
let src = self.src.unwrap_or(String::new());
Iframe {
id,
title,
classes,
width,
height,
src,
}
}
}