monorepo/file-service/src/html.rs

155 lines
3.2 KiB
Rust
Raw Normal View History

use build_html::{self, Html, HtmlContainer};
#[derive(Clone, Debug)]
pub struct Form {
method: String,
encoding: Option<String>,
elements: String,
}
impl Form {
pub fn new() -> Self {
Self {
method: "get".to_owned(),
encoding: None,
elements: "".to_owned(),
}
}
pub fn with_method(mut self, method: &str) -> Self {
self.method = method.to_owned();
self
}
pub fn with_encoding(mut self, encoding: &str) -> Self {
self.encoding = Some(encoding.to_owned());
self
}
}
impl Html for Form {
fn to_html_string(&self) -> String {
let encoding = match self.encoding {
Some(ref encoding) => format!("encoding={encoding}", encoding = encoding),
None => format!(""),
};
format!(
"<form method={method} {encoding}>\n{elements}\n</form>\n",
method = self.method,
encoding = encoding,
elements = self.elements.to_html_string()
)
}
}
impl HtmlContainer for Form {
fn add_html<H: Html>(&mut self, html: H) {
self.elements.push_str(&html.to_html_string());
}
}
#[derive(Clone, Debug)]
pub struct Input {
ty: String,
name: String,
id: String,
value: Option<String>,
}
impl Html for Input {
fn to_html_string(&self) -> String {
format!(
"<input type=\"{ty}\" name=\"{name}\" id=\"{id}\">{value}</input>\n",
ty = self.ty,
name = self.name,
id = self.id,
value = self.value.clone().unwrap_or("".to_owned()),
)
}
}
impl Input {
pub fn new(ty: &str, name: &str, id: &str) -> Self {
Self {
ty: ty.to_owned(),
name: name.to_owned(),
id: id.to_owned(),
value: None,
}
}
pub fn with_value(mut self, val: &str) -> Self {
self.value = Some(val.to_owned());
self
}
}
#[derive(Clone, Debug)]
pub struct Label {
target: String,
text: String,
}
impl Label {
pub fn new(target: &str, text: &str) -> Self {
Self {
target: target.to_owned(),
text: text.to_owned(),
}
}
}
impl Html for Label {
fn to_html_string(&self) -> String {
format!(
"<label for=\"{target}\">{text}</label>",
target = self.target,
text = self.text
)
}
}
#[derive(Clone, Debug)]
pub struct Button {
name: String,
text: String,
}
impl Button {
pub fn new(name: &str, text: &str) -> Self {
Self {
name: name.to_owned(),
text: text.to_owned(),
}
}
}
impl Html for Button {
fn to_html_string(&self) -> String {
format!(
"<button name={name}>{text}</button>",
name = self.name,
text = self.text
)
}
}
2023-09-21 03:31:52 +00:00
#[derive(Clone, Debug)]
pub struct Image {
path: String,
}
impl Image {
pub fn new(path: &str) -> Self {
Self {
path: path.to_owned(),
}
}
}
impl Html for Image {
fn to_html_string(&self) -> String {
format!("<img src={path} />", path = self.path,)
}
}