markdown_composer/
traits.rs

1//! Contains Markdown trait definitions.
2
3use dyn_clonable::clonable;
4use std::fmt;
5
6/// A [MarkdownElement](trait.MarkdownElement.html) that can be rendered as a
7/// footer value.
8pub trait AsFooter {
9    /// Returns the `MarkdownElement` that can be used to render the footer.
10    fn as_footer(&self) -> Box<dyn MarkdownElement>;
11}
12
13/// An element that can be rendered as to markdown.
14#[clonable]
15pub trait MarkdownElement: Clone + fmt::Debug {
16    /// Renders the element to markdown.
17    fn render(&self) -> String;
18}
19
20/// Implemented for all types that do implement `Display`.
21///
22/// The implementation calls the `Display::fmt` method.
23impl<T> MarkdownElement for T
24where
25    T: Clone + fmt::Debug + fmt::Display,
26{
27    fn render(&self) -> String {
28        format!("{}", self)
29    }
30}
31
32// Implemented for easier `Box` handling.
33impl<'a, T: 'a> From<T> for Box<dyn MarkdownElement + 'a>
34where
35    T: Clone + fmt::Debug + fmt::Display,
36{
37    fn from(value: T) -> Self {
38        Box::new(value)
39    }
40}