use std::fmt::{self, Display};
use std::ops::Deref;
use horrorshow::{Render, RenderMut, RenderOnce};
use crate::render::Gazetta;
pub struct Site<'a, G>
where
G: Gazetta + 'a,
G::SiteMeta: 'a,
{
pub title: &'a str,
pub origin: &'a str,
pub prefix: &'a str,
pub stylesheets: Option<&'a str>,
pub javascript: Option<&'a str>,
pub icon: Option<&'a str>,
pub meta: &'a G::SiteMeta,
}
impl<'a, G> Site<'a, G>
where
G: Gazetta,
{
pub fn base(&self) -> Base<'a> {
Base {
origin: self.origin,
prefix: self.prefix,
}
}
}
impl<'a, G> Deref for Site<'a, G>
where
G: Gazetta + 'a,
G::SiteMeta: 'a,
{
type Target = G::SiteMeta;
fn deref(&self) -> &Self::Target {
self.meta
}
}
impl<'a, G> Copy for Site<'a, G>
where
G: Gazetta + 'a,
G::PageMeta: 'a,
G::SiteMeta: 'a,
{
}
impl<'a, G> Clone for Site<'a, G>
where
G: Gazetta + 'a,
G::PageMeta: 'a,
G::SiteMeta: 'a,
{
fn clone(&self) -> Self {
*self
}
}
impl<'a, G> fmt::Debug for Site<'a, G>
where
G: Gazetta + 'a,
G::PageMeta: 'a,
G::SiteMeta: fmt::Debug + 'a,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Site")
.field("title", &self.title)
.field("stylesheets", &self.stylesheets)
.field("javascript", &self.javascript)
.field("icon", &self.icon)
.field("meta", &self.meta)
.finish()
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Base<'a> {
origin: &'a str,
prefix: &'a str,
}
impl<'a> Display for Base<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.origin)?;
f.write_str(self.prefix)?;
Ok(())
}
}
impl Render for Base<'_> {
fn render(&self, tmpl: &mut horrorshow::TemplateBuffer<'_>) {
tmpl.write_fmt(format_args!("{self}"))
}
}
impl RenderMut for Base<'_> {
fn render_mut(&mut self, tmpl: &mut horrorshow::TemplateBuffer<'_>) {
self.render(tmpl)
}
}
impl RenderOnce for Base<'_> {
fn render_once(self, tmpl: &mut horrorshow::TemplateBuffer<'_>)
where
Self: Sized,
{
self.render(tmpl)
}
fn size_hint(&self) -> usize {
self.origin.len() + self.prefix.len()
}
}