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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
//!
//! Functions to build elements and attributes building blocks
//!
use super::*;
///
/// Create an escapable element
///
/// ```
/// let mut s = String::new();
/// let k = hypermelon::build::raw_escapable("<test>I can insert my own elements!</test>");
/// hypermelon::render_escapable(k,&mut s).unwrap()
///
/// ```
///
pub fn raw_escapable<D: fmt::Display>(data: D) -> RawEscapable<D> {
RawEscapable::new(data)
}
pub fn raw<D: fmt::Display>(data: D) -> Raw<D> {
Raw::new(data)
}
///
/// Create an escapable element from a closure
///
/// ```
/// let mut s = String::new();
/// let k = hypermelon::build::from_closure_escapable(|w|{
///
/// w.render(hypermelon::build::raw_escapable("<test/>"))?;
///
/// w.render(hypermelon::build::single("test2"))
///
/// });
/// hypermelon::render_escapable(k,&mut s).unwrap()
///
/// ```
///
pub fn from_closure_escapable<F: FnOnce(&mut ElemWriteEscapable) -> fmt::Result>(
func: F,
) -> ClosureEscapable<F> {
ClosureEscapable::new(func)
}
///
/// Create an element from a closure
///
/// ```
/// let mut s = String::new();
/// let k = hypermelon::build::from_closure(|w|{
///
/// w.render(hypermelon::build::single("test"))
///
/// });
/// hypermelon::render(k,&mut s).unwrap()
///
/// ```
///
pub fn from_closure<F: FnOnce(&mut ElemWrite) -> fmt::Result>(func: F) -> Closure<F> {
Closure::new(func)
}
pub fn from_closure2<F: FnOnce() -> E, E: Elem>(func: F) -> Closure2<F> {
Closure2 { func }
}
///
/// Create an element from an iterator of elements
///
/// ```
/// let mut s = String::new();
/// let k = hypermelon::build::from_iter((0..10).map(|_|hypermelon::build::single("hello")));
/// hypermelon::render(k,&mut s).unwrap()
///
/// ```
///
pub fn from_iter<I: Iterator<Item = R>, R: Elem>(iter: I) -> Iter<I> {
Iter::new(iter)
}
///
/// Create an element that has no closing tag.
///
/// ```
/// let mut s = String::new();
/// let k = hypermelon::build::single("hello");
/// hypermelon::render(k,&mut s).unwrap()
///
/// ```
pub fn single<D: fmt::Display>(tag: D) -> Single<D, (), &'static str, &'static str> {
Single::new(tag)
}
///
/// Create an element.
///
/// ```
/// let mut s = String::new();
/// let k = hypermelon::build::elem("hello");
/// hypermelon::render(k,&mut s).unwrap()
///
/// ```
pub fn elem<D: fmt::Display>(tag: D) -> Element<D, ()> {
Element::new(tag)
}
///
/// Box an element
///
/// ```
/// let mut s = String::new();
/// let k = hypermelon::build::single("hello");
/// let k = hypermelon::build::box_elem(k);
/// hypermelon::render(k,&mut s).unwrap()
///
/// ```
pub fn box_elem<'a, E: Elem + 'a>(elem: E) -> DynamicElement<'a> {
DynamicElement::new(elem)
}
///
/// Create an attr from a closure.
///
/// ```
/// use hypermelon::build;
/// let mut s = String::new();
/// let k = build::elem("hello").with(
/// build::attr_from_closure(|w|
/// w.render(("test","val"))
/// )
/// );
/// hypermelon::render(k,&mut s).unwrap()
///
/// ```
pub fn attr_from_closure<F: FnOnce(&mut AttrWrite) -> fmt::Result>(func: F) -> AttrClosure<F> {
AttrClosure::new(func)
}
///
/// Create a path attribute
///
/// ```
/// use hypermelon::build;
/// use hypermelon::attr::PathCommand;
/// let mut s = String::new();
/// let k = build::elem("hello").with(
/// build::path(
/// (0..10).map(|_|PathCommand::L(5.0,5.0))
/// )
/// );
/// hypermelon::render(k,&mut s).unwrap()
///
/// ```
pub fn path<I: IntoIterator<Item = PathCommand<D>>, D: fmt::Display>(iter: I) -> Path<I> {
Path::new(iter)
}
///
/// Create a points attribute
///
/// ```
/// use hypermelon::build;
/// let mut s = String::new();
/// let k = build::elem("hello").with(
/// build::points(
/// (0..10).map(|_|(5.0,5.0))
/// )
/// );
/// hypermelon::render(k,&mut s).unwrap()
///
/// ```
pub fn points<I: IntoIterator<Item = (D, D)>, D: fmt::Display>(iter: I) -> Points<I> {
Points::new(iter)
}
///
/// Create a path attribute from a closure
///
/// ```
/// use hypermelon::build;
/// use hypermelon::attr::PathCommand;
/// let mut s = String::new();
/// let k = build::elem("hello").with(
/// build::path_from_closure(|w|{
/// let mut w=w.start();
/// w.put(PathCommand::L(5.0,5.0))?;
/// w.put(PathCommand::L(5.0,5.0))
/// })
/// );
/// hypermelon::render(k,&mut s).unwrap()
///
/// ```
///
pub fn path_from_closure<F: FnOnce(PathSinkBuilder) -> std::fmt::Result>(
func: F,
) -> PathClosure<F> {
PathClosure::new(func)
}