builderx-macros 0.1.0

A concise builder-pattern UI DSL for Rust
Documentation
extern crate builderx_core as builderx;

use builderx_core::{AttachChild, AttachChildren, BxAdapter, CanAttach, CanAttachMany};
use builderx_macros::bx;

#[derive(Default, Debug, PartialEq)]
struct StubBuilder(Vec<&'static str>);

impl AttachChild<&'static str> for StubBuilder {
    fn attach_child(mut self, child: &'static str) -> Self {
        self.0.push(child);
        self
    }
}

impl AttachChildren<&'static str> for StubBuilder {
    fn attach_children<I>(mut self, children: I) -> Self
    where
        I: IntoIterator<Item = &'static str>,
    {
        self.0.extend(children);
        self
    }
}

struct StubAdapter;

impl BxAdapter for StubAdapter {}

impl CanAttach<StubBuilder, &'static str> for StubAdapter {
    fn do_attach(builder: StubBuilder, child: &'static str) -> StubBuilder {
        builder.attach_child(child)
    }
}

impl<I> CanAttachMany<StubBuilder, I, &'static str> for StubAdapter
where
    I: IntoIterator<Item = &'static str>,
{
    fn do_attach_many(builder: StubBuilder, children: I) -> StubBuilder {
        builder.attach_children(children)
    }
}

#[test]
fn applies_children_and_spreads() {
    let built = bx! { StubAdapter => StubBuilder::default() { "a", "b", ..["c", "d"] } };
    assert_eq!(built, StubBuilder(vec!["a", "b", "c", "d"]));
}