use build_html::{self, Html, HtmlContainer}; #[derive(Clone, Debug)] pub struct Form { method: String, encoding: Option, 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!( "
\n{elements}\n
\n", method = self.method, encoding = encoding, elements = self.elements.to_html_string() ) } } impl HtmlContainer for Form { fn add_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, } impl Html for Input { fn to_html_string(&self) -> String { format!( "{value}\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!( "", 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!( "", name = self.name, text = self.text ) } }