use build_html::{self, Html, HtmlContainer}; #[derive(Clone, Debug)] pub struct Form { classes: Option, path: String, method: String, encoding: Option, elements: String, } impl Form { pub fn new() -> Self { Self { classes: None, path: "/".to_owned(), method: "get".to_owned(), encoding: None, elements: "".to_owned(), } } pub fn with_path(mut self, path: &str) -> Self { self.path = path.to_owned(); self } 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 classes = match self.classes { Some(ref classes) => format!("class=\"{}\"", classes), None => "".to_owned(), }; let encoding = match self.encoding { Some(ref encoding) => format!("enctype=\"{encoding}\"", encoding = encoding), None => "".to_owned(), }; format!( "
\n{elements}\n
\n", path = self.path, method = self.method, encoding = encoding, elements = self.elements.to_html_string(), classes = classes, ) } } 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: Option, value: Option, attributes: Vec<(String, String)>, } impl Html for Input { fn to_html_string(&self) -> String { 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(), }; let attrs = self .attributes .iter() .map(|(key, value)| format!("{}=\"{}\"", key, value)) .collect::>() .join(" "); format!( "\n", ty = self.ty, name = self.name, id = id, value = value, attrs = attrs, ) } } impl Input { pub fn new(ty: &str, name: &str) -> Self { Self { ty: ty.to_owned(), name: name.to_owned(), id: None, value: None, attributes: vec![], } } pub fn with_id(mut self, val: &str) -> Self { self.id = Some(val.to_owned()); self } pub fn with_value(mut self, val: &str) -> Self { self.value = Some(val.to_owned()); self } pub fn with_attributes<'a>( mut self, values: impl IntoIterator, ) -> Self { self.attributes = values .into_iter() .map(|(a, b)| (a.to_owned(), b.to_owned())) .collect::>(); 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 { ty: Option, name: Option, label: String, } impl Button { pub fn new(label: &str) -> Self { Self { ty: None, name: None, label: label.to_owned(), } } pub fn with_type(mut self, ty: &str) -> Self { self.ty = Some(ty.to_owned()); self } } impl Html for Button { fn to_html_string(&self) -> String { let ty = match self.ty { Some(ref ty) => format!("type={}", ty), None => "".to_owned(), }; let name = match self.name { Some(ref name) => format!("name={}", name), None => "".to_owned(), }; format!( "", name = name, label = self.label ) } } #[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!("", path = self.path,) } }