use plume_models::{Connection, users::User}; use rocket::response::Content; use rocket_i18n::Catalog; use templates::Html; pub use askama_escape::escape; pub type BaseContext<'a> = &'a(&'a Connection, &'a Catalog, Option); pub type Ructe = Content>; #[macro_export] macro_rules! render { ($group:tt :: $page:tt ( $( $param:expr ),* ) ) => { { use rocket::{http::ContentType, response::Content}; use templates; let mut res = vec![]; templates::$group::$page( &mut res, $( $param ),* ).unwrap(); Content(ContentType::HTML, res) } } } pub enum Size { Small, Medium, } impl Size { fn as_str(&self) -> &'static str { match self { Size::Small => "small", Size::Medium => "medium", } } } pub fn avatar(conn: &Connection, user: &User, size: Size, pad: bool, catalog: &Catalog) -> Html { let name = escape(&user.name(conn)).to_string(); Html(format!( r#"
"#, size = size.as_str(), padded = if pad { "padded" } else { "" }, url = user.avatar_url(conn), title = i18n!(catalog, "{0}'s avatar"; name), )) } pub fn tabs(links: &[(&str, &str, bool)]) -> Html { let mut res = String::from(r#"
"#); for (url, title, selected) in links { res.push_str(r#""#); } else { res.push_str("\">"); } res.push_str(title); res.push_str(""); } res.push_str("
"); Html(res) } pub fn paginate(catalog: &Catalog, page: i32, total: i32) -> Html { let mut res = String::new(); res.push_str(r#""); Html(res) } #[macro_export] macro_rules! icon { ($name:expr) => { Html(concat!(r#"")) } } macro_rules! input { ($catalog:expr, $name:tt ($kind:tt), $label:expr, $optional:expr, $details:expr, $form:expr, $err:expr, $props:expr) => { { use validator::ValidationErrorsKind; use std::borrow::Cow; Html(format!(r#" {error} "#, name = stringify!($name), label = i18n!($catalog, $label), kind = stringify!($kind), optional = if $optional { format!("{}", i18n!($catalog, "Optional")) } else { String::new() }, details = if $details.len() > 0 { format!("{}", i18n!($catalog, $details)) } else { String::new() }, error = if let Some(ValidationErrorsKind::Field(errs)) = $err.errors().get(stringify!($name)) { format!(r#"

{}

"#, i18n!($catalog, &*errs[0].message.clone().unwrap_or(Cow::from("Unknown error")))) } else { String::new() }, val = escape(&$form.$name), props = $props )) } }; ($catalog:expr, $name:tt (optional $kind:tt), $label:expr, $details:expr, $form:expr, $err:expr, $props:expr) => { input!($catalog, $name ($kind), $label, true, $details, $form, $err, $props) }; ($catalog:expr, $name:tt (optional $kind:tt), $label:expr, $form:expr, $err:expr, $props:expr) => { input!($catalog, $name ($kind), $label, true, "", $form, $err, $props) }; ($catalog:expr, $name:tt ($kind:tt), $label:expr, $details:expr, $form:expr, $err:expr, $props:expr) => { input!($catalog, $name ($kind), $label, false, $details, $form, $err, $props) }; ($catalog:expr, $name:tt ($kind:tt), $label:expr, $form:expr, $err:expr, $props:expr) => { input!($catalog, $name ($kind), $label, false, "", $form, $err, $props) }; ($catalog:expr, $name:tt ($kind:tt), $label:expr, $form:expr, $err:expr) => { input!($catalog, $name ($kind), $label, false, "", $form, $err, "") }; ($catalog:expr, $name:tt ($kind:tt), $label:expr, $props:expr) => { { Html(format!(r#" "#, name = stringify!($name), label = i18n!($catalog, $label), kind = stringify!($kind), props = $props )) } }; }