2023-09-21 03:06:34 +00:00
|
|
|
use build_html::{self, Html, HtmlContainer};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Form {
|
2023-09-22 04:38:26 +00:00
|
|
|
path: String,
|
2023-09-21 03:06:34 +00:00
|
|
|
method: String,
|
|
|
|
encoding: Option<String>,
|
|
|
|
elements: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Form {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2023-09-22 04:38:26 +00:00
|
|
|
path: "/".to_owned(),
|
2023-09-21 03:06:34 +00:00
|
|
|
method: "get".to_owned(),
|
|
|
|
encoding: None,
|
|
|
|
elements: "".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 04:38:26 +00:00
|
|
|
pub fn with_path(mut self, path: &str) -> Self {
|
|
|
|
self.path = path.to_owned();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
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 {
|
2023-09-27 02:43:33 +00:00
|
|
|
Some(ref encoding) => format!("enctype=\"{encoding}\"", encoding = encoding),
|
2023-09-21 03:06:34 +00:00
|
|
|
None => format!(""),
|
|
|
|
};
|
|
|
|
format!(
|
2023-09-22 04:38:26 +00:00
|
|
|
"<form action=\"{path}\" method=\"{method}\" {encoding}>\n{elements}\n</form>\n",
|
|
|
|
path = self.path,
|
2023-09-21 03:06:34 +00:00
|
|
|
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,
|
2023-09-22 04:38:26 +00:00
|
|
|
id: Option<String>,
|
2023-09-21 03:06:34 +00:00
|
|
|
value: Option<String>,
|
2023-09-22 04:38:26 +00:00
|
|
|
content: Option<String>,
|
2023-09-21 03:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Html for Input {
|
|
|
|
fn to_html_string(&self) -> String {
|
2023-09-22 04:38:26 +00:00
|
|
|
let id = match self.id {
|
|
|
|
Some(ref id) => format!("id=\"{}\"", id),
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
|
|
|
let value = match self.value {
|
|
|
|
Some(ref value) => format!("value=\"{}\"", value),
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
format!(
|
2023-09-22 04:38:26 +00:00
|
|
|
"<input type=\"{ty}\" name=\"{name}\" {id} {value}>{content}</input>\n",
|
2023-09-21 03:06:34 +00:00
|
|
|
ty = self.ty,
|
|
|
|
name = self.name,
|
2023-09-22 04:38:26 +00:00
|
|
|
id = id,
|
|
|
|
value = value,
|
|
|
|
content = self.content.clone().unwrap_or("".to_owned()),
|
2023-09-21 03:06:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Input {
|
2023-09-22 04:38:26 +00:00
|
|
|
pub fn new(ty: &str, name: &str) -> Self {
|
2023-09-21 03:06:34 +00:00
|
|
|
Self {
|
|
|
|
ty: ty.to_owned(),
|
|
|
|
name: name.to_owned(),
|
2023-09-22 04:38:26 +00:00
|
|
|
id: None,
|
2023-09-21 03:06:34 +00:00
|
|
|
value: None,
|
2023-09-22 04:38:26 +00:00
|
|
|
content: None,
|
2023-09-21 03:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 04:38:26 +00:00
|
|
|
pub fn with_id(mut self, val: &str) -> Self {
|
|
|
|
self.id = Some(val.to_owned());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
pub fn with_value(mut self, val: &str) -> Self {
|
|
|
|
self.value = Some(val.to_owned());
|
|
|
|
self
|
|
|
|
}
|
2023-09-22 04:38:26 +00:00
|
|
|
|
|
|
|
pub fn with_content(mut self, val: &str) -> Self {
|
|
|
|
self.content = Some(val.to_owned());
|
|
|
|
self
|
|
|
|
}
|
2023-09-21 03:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2023-09-27 02:43:33 +00:00
|
|
|
ty: Option<String>,
|
2023-09-22 04:38:26 +00:00
|
|
|
name: Option<String>,
|
|
|
|
label: String,
|
2023-09-21 03:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Button {
|
2023-09-22 04:38:26 +00:00
|
|
|
pub fn new(label: &str) -> Self {
|
2023-09-21 03:06:34 +00:00
|
|
|
Self {
|
2023-09-27 02:43:33 +00:00
|
|
|
ty: None,
|
2023-09-22 04:38:26 +00:00
|
|
|
name: None,
|
|
|
|
label: label.to_owned(),
|
2023-09-21 03:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-27 02:43:33 +00:00
|
|
|
|
|
|
|
pub fn with_type(mut self, ty: &str) -> Self {
|
|
|
|
self.ty = Some(ty.to_owned());
|
|
|
|
self
|
|
|
|
}
|
2023-09-21 03:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Html for Button {
|
|
|
|
fn to_html_string(&self) -> String {
|
2023-09-27 02:43:33 +00:00
|
|
|
let ty = match self.ty {
|
|
|
|
Some(ref ty) => format!("type={}", ty),
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
2023-09-22 04:38:26 +00:00
|
|
|
let name = match self.name {
|
|
|
|
Some(ref name) => format!("name={}", name),
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
2023-09-21 03:06:34 +00:00
|
|
|
format!(
|
2023-09-27 02:43:33 +00:00
|
|
|
"<button {ty} {name}>{label}</button>",
|
2023-09-22 04:38:26 +00:00
|
|
|
name = name,
|
|
|
|
label = self.label
|
2023-09-21 03:06:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
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,)
|
|
|
|
}
|
|
|
|
}
|