Files
markdown_composer
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//! Contains Markdown trait definitions.

use dyn_clonable::clonable;
use std::fmt;

/// A [MarkdownElement](trait.MarkdownElement.html) that can be rendered as a
/// footer value.
pub trait AsFooter {
    /// Returns the `MarkdownElement` that can be used to render the footer.
    fn as_footer(&self) -> Box<dyn MarkdownElement>;
}

/// An element that can be rendered as to markdown.
#[clonable]
pub trait MarkdownElement: Clone + fmt::Debug {
    /// Renders the element to markdown.
    fn render(&self) -> String;
}

/// Implemented for all types that do implement `Display`.
///
/// The implementation calls the `Display::fmt` method.
impl<T> MarkdownElement for T
where
    T: Clone + fmt::Debug + fmt::Display,
{
    fn render(&self) -> String {
        format!("{}", self)
    }
}

// Implemented for easier `Box` handling.
impl<'a, T: 'a> From<T> for Box<dyn MarkdownElement + 'a>
where
    T: Clone + fmt::Debug + fmt::Display,
{
    fn from(value: T) -> Self {
        Box::new(value)
    }
}