Files
rust-otus/smart-house/src/builders.rs
2026-03-06 22:57:13 +03:00

54 lines
1.2 KiB
Rust

use crate::{Device, House, Room};
use std::collections::HashMap;
pub struct HouseBuilder {
rooms: HashMap<String, Room>,
}
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<String, Device>,
}
impl RoomBuilder {
pub fn add_device(mut self, name: &str, device: impl Into<Device>) -> 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()
}
}