2023-09-21 03:06:34 +00:00
|
|
|
use build_html::{self, Html, HtmlContainer};
|
|
|
|
|
2023-10-07 00:36:27 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Attributes(Vec<(String, String)>);
|
|
|
|
|
|
|
|
/*
|
|
|
|
impl FromIterator<(String, String)> for Attributes {
|
|
|
|
fn from_iter<T>(iter: T) -> Self
|
|
|
|
where
|
|
|
|
T: IntoIterator<Item = (String, String)>,
|
|
|
|
{
|
|
|
|
Attributes(iter.collect::<Vec<(String, String)>>())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromIterator<(&str, &str)> for Attributes {
|
|
|
|
fn from_iter<T>(iter: T) -> Self
|
|
|
|
where
|
|
|
|
T: IntoIterator<Item = (&str, &str)>,
|
|
|
|
{
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
impl ToString for Attributes {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.map(|(key, value)| format!("{}=\"{}\"", key, value))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(" ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
#[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-10-04 19:57:18 +00:00
|
|
|
None => "".to_owned(),
|
2023-09-21 03:06:34 +00:00
|
|
|
};
|
|
|
|
format!(
|
2023-10-26 02:52:03 +00:00
|
|
|
"<form action=\"{path}\" method=\"{method}\" {encoding}>\n{elements}\n</form>\n",
|
2023-09-22 04:38:26 +00:00
|
|
|
path = self.path,
|
2023-09-21 03:06:34 +00:00
|
|
|
method = self.method,
|
|
|
|
encoding = encoding,
|
2023-10-06 23:04:31 +00:00
|
|
|
elements = self.elements.to_html_string(),
|
2023-09-21 03:06:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-07 00:36:27 +00:00
|
|
|
attributes: Attributes,
|
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-10-07 00:36:27 +00:00
|
|
|
let attrs = self.attributes.to_string();
|
2023-09-22 04:38:26 +00:00
|
|
|
|
2023-09-21 03:06:34 +00:00
|
|
|
format!(
|
2023-10-06 23:04:31 +00:00
|
|
|
"<input type=\"{ty}\" name=\"{name}\" {id} {value} {attrs} />\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,
|
2023-10-06 23:04:31 +00:00
|
|
|
attrs = attrs,
|
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-10-07 00:36:27 +00:00
|
|
|
attributes: Attributes::default(),
|
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-10-06 23:04:31 +00:00
|
|
|
pub fn with_attributes<'a>(
|
|
|
|
mut self,
|
|
|
|
values: impl IntoIterator<Item = (&'a str, &'a str)>,
|
|
|
|
) -> Self {
|
2023-10-07 00:36:27 +00:00
|
|
|
self.attributes = Attributes(
|
|
|
|
values
|
|
|
|
.into_iter()
|
|
|
|
.map(|(a, b)| (a.to_owned(), b.to_owned()))
|
|
|
|
.collect::<Vec<(String, String)>>(),
|
|
|
|
);
|
2023-09-22 04:38:26 +00:00
|
|
|
self
|
|
|
|
}
|
2023-09-21 03:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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-10-07 03:51:41 +00:00
|
|
|
attributes: Attributes,
|
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-10-07 03:51:41 +00:00
|
|
|
attributes: Attributes::default(),
|
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-10-07 03:51:41 +00:00
|
|
|
|
|
|
|
pub fn with_attributes<'a>(
|
|
|
|
mut self,
|
|
|
|
values: impl IntoIterator<Item = (&'a str, &'a str)>,
|
|
|
|
) -> Self {
|
|
|
|
self.attributes = Attributes(
|
|
|
|
values
|
|
|
|
.into_iter()
|
|
|
|
.map(|(a, b)| (a.to_owned(), b.to_owned()))
|
|
|
|
.collect::<Vec<(String, String)>>(),
|
|
|
|
);
|
|
|
|
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-10-07 03:51:41 +00:00
|
|
|
"<button {ty} {name} {attrs}>{label}</button>",
|
2023-09-22 04:38:26 +00:00
|
|
|
name = name,
|
2023-10-07 03:51:41 +00:00
|
|
|
label = self.label,
|
|
|
|
attrs = self.attributes.to_string()
|
2023-09-21 03:06:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|