Skip to main content

write_wire/
write_wire.rs

1use brdb::{BrFsReader, Brdb, Brick, IntoReader, World, assets};
2use std::path::PathBuf;
3
4/// Writes a world with two bricks and a wire to example_wire
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let path = PathBuf::from("./example_wire.brdb");
7
8    // Ensures the memory db can be created without errors
9    let db = Brdb::new(&path)?.into_reader();
10    let mut world = World::new();
11    // Register the built-in component type/struct mappings so the gate and
12    // rerouter component types resolve when writing.
13    world.register_all_components();
14    world.meta.bundle.description = "Example World".to_string();
15
16    let (a, a_id) = Brick {
17        position: (0, 0, 1).into(),
18        color: (255, 0, 0).into(),
19        asset: assets::bricks::B_REROUTE,
20        ..Default::default()
21    }
22    .with_component(assets::components::Rerouter)
23    .with_id_split();
24    let (b, b_id) = Brick {
25        position: (15, 0, 1).into(),
26        color: (255, 0, 0).into(),
27        asset: assets::components::LogicGate::BoolNot.brick(),
28        ..Default::default()
29    }
30    .with_component(assets::components::LogicGate::BoolNot.component())
31    .with_id_split();
32
33    world.add_bricks([a, b]);
34    world.add_wire_connection(
35        assets::components::LogicGate::BoolNot.output_of(b_id),
36        assets::components::Rerouter::input_of(a_id),
37    );
38
39    db.save("example world", &world)?;
40
41    println!("{}", db.get_fs()?.render());
42
43    Ok(())
44}