use crate::prelude::*;

pub trait Template {
	fn render(&self, fmt: &mut Formatter) -> FmtResult;

	fn display(self) -> impl Display
	where
		Self: Sized,
	{
		struct D<T>(T);
		impl<T: Template> Display for D<T> {
			fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
				self.0.render(fmt)
			}
		}

		D(self)
	}
}

pub struct TemplateFn<F>(F);
pub fn template_fn<F>(f: F) -> TemplateFn<F>
where
	F: Fn(&mut Formatter) -> FmtResult,
{
	TemplateFn(f)
}
impl<F> Template for TemplateFn<F>
where
	F: Fn(&mut Formatter) -> FmtResult,
{
	fn render(&self, fmt: &mut Formatter) -> FmtResult {
		self.0(fmt)
	}
}
