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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::rc::Rc;

use crate::{dev::{VDomComponent, VDomNode}, VDomElement};

/// Allows for embedding into [html!](macro.html.html) macro.
///
/// ```rust
/// use vertigo::{dev::VDomNode, Embed, html};
///
/// struct Point {
///     pub x: i32,
///     pub y: i32,
/// }
///
/// impl Embed for Point {
///     fn embed(self) -> VDomNode {
///         VDomNode::text(format!("({}, {})", self.x, self.y))
///     }
/// }
///
/// let x = Point { x: 1, y: 2 };
///
/// let rendered = html! {
///     <div> {x} </div>
/// };
///
/// assert_eq!(
///     format!("{:?}", rendered),
///     format!("{:?}", html! {
///         <div> "(1, 2)" </div>
///     })
/// )
/// ```
pub trait Embed {
    fn embed(self) -> VDomNode;
}

impl Embed for VDomNode {
    fn embed(self) -> VDomNode {
        self
    }
}

impl Embed for VDomElement {
    fn embed(self) -> VDomNode {
        VDomNode::Element { node: self }
    }
}

impl Embed for VDomComponent {
    fn embed(self) -> VDomNode {
        VDomNode::Component { node: self }
    }
}

impl Embed for &str {
    fn embed(self) -> VDomNode {
        VDomNode::text(self)
    }
}

impl Embed for String {
    fn embed(self) -> VDomNode {
        VDomNode::text(self)
    }
}

impl Embed for &String {
    fn embed(self) -> VDomNode {
        VDomNode::text(self)
    }
}

impl Embed for Rc<String> {
    fn embed(self) -> VDomNode {
        VDomNode::text(&*self)
    }
}

macro_rules! impl_to_string {
    ($ty: ty) => {
        impl Embed for $ty {
            fn embed(self) -> VDomNode {
                VDomNode::text(self.to_string())
            }
        }
    };
}

impl_to_string!(i8);
impl_to_string!(i16);
impl_to_string!(i32);
impl_to_string!(i64);
impl_to_string!(i128);
impl_to_string!(isize);

impl_to_string!(u8);
impl_to_string!(u16);
impl_to_string!(u32);
impl_to_string!(u64);
impl_to_string!(u128);
impl_to_string!(usize);

impl_to_string!(f32);
impl_to_string!(f64);

impl_to_string!(&Rc<i8>);
impl_to_string!(&Rc<i16>);
impl_to_string!(&Rc<i32>);
impl_to_string!(&Rc<i64>);
impl_to_string!(&Rc<i128>);
impl_to_string!(&Rc<isize>);

impl_to_string!(&Rc<u8>);
impl_to_string!(&Rc<u16>);
impl_to_string!(&Rc<u32>);
impl_to_string!(&Rc<u64>);
impl_to_string!(&Rc<u128>);
impl_to_string!(&Rc<usize>);

impl_to_string!(&Rc<f32>);
impl_to_string!(&Rc<f64>);