use crate::{Device, House, Room}; use std::collections::HashMap; pub struct HouseBuilder { rooms: HashMap, } impl HouseBuilder { pub fn new() -> Self { Self { rooms: HashMap::new() } } pub fn add_room(self, name: &str) -> RoomBuilder { RoomBuilder { parent: self, name: name.to_string(), devices: HashMap::new(), } } pub fn build(self) -> House { House::new(self.rooms) } } impl Default for HouseBuilder { fn default() -> Self { Self::new() } } pub struct RoomBuilder { parent: HouseBuilder, name: String, devices: HashMap, } impl RoomBuilder { pub fn add_device(mut self, name: &str, device: impl Into) -> Self { self.devices.insert(name.to_string(), device.into()); self } pub fn add_room(mut self, name: &str) -> RoomBuilder { self.parent.rooms.insert(self.name, Room::new(self.devices)); self.parent.add_room(name) } pub fn build(mut self) -> House { self.parent.rooms.insert(self.name, Room::new(self.devices)); self.parent.build() } }