reporter - done
This commit is contained in:
@@ -189,8 +189,8 @@
|
||||
|
||||
Реализовать компоновщик для построения отчёта об объектах умного дома в [таком стиле](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c07dfc726e8ccbccdcc2d88a79d3f190).
|
||||
|
||||
- [ ] Использовать статический полиморфизм (дженерики).
|
||||
- [ ] Вызов метода report() должен выводить в терминал отчёт обо всех добавленных объектах.
|
||||
- [x] Использовать статический полиморфизм (дженерики).
|
||||
- [x] Вызов метода report() должен выводить в терминал отчёт обо всех добавленных объектах.
|
||||
|
||||
Добавить возможность добавления callback-ов в объект комнаты, которые срабатывают при добавлении новых устройств в комнату (паттерн Observer).
|
||||
|
||||
|
||||
17
smart-house/src/bin/reporter.rs
Normal file
17
smart-house/src/bin/reporter.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use smart_house::{HouseBuilder, PowerSocket, Reporter, Thermometer};
|
||||
|
||||
fn main() {
|
||||
let house = HouseBuilder::new()
|
||||
.add_room("Main")
|
||||
.add_device("PSockB", PowerSocket::stub(12.0, false))
|
||||
.add_room("Hall")
|
||||
.add_device("ThermB", Thermometer::stub(18.5))
|
||||
.build();
|
||||
|
||||
Reporter::new()
|
||||
.add_reportable(house.get_room("Main").unwrap())
|
||||
.add_reportable(house.get_device("Main", "PSockB").unwrap())
|
||||
.add_reportable(house.get_room("Hall").unwrap())
|
||||
.add_reportable(house.get_device("Hall", "ThermB").unwrap())
|
||||
.report();
|
||||
}
|
||||
@@ -34,7 +34,7 @@ impl From<super::PowerSocket> for Device {
|
||||
}
|
||||
|
||||
impl PrintStatus for Device {
|
||||
fn print_status_into(&self, out: &mut impl Write) -> Result<(), std::io::Error> {
|
||||
fn print_status_into(&self, out: &mut dyn Write) -> Result<(), std::io::Error> {
|
||||
out.write_fmt(format_args!("{}", self.display()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ impl House {
|
||||
}
|
||||
|
||||
impl PrintStatus for House {
|
||||
fn print_status_into(&self, out: &mut impl Write) -> Result<(), std::io::Error> {
|
||||
fn print_status_into(&self, out: &mut dyn Write) -> Result<(), std::io::Error> {
|
||||
for (room_name, room) in self.rooms.iter() {
|
||||
out.write_fmt(format_args!("{}:", room_name))?;
|
||||
out.write_fmt(format_args!("{}", "-".repeat(32)))?;
|
||||
|
||||
@@ -6,6 +6,7 @@ mod power_socket;
|
||||
mod room;
|
||||
mod builders;
|
||||
mod print_status;
|
||||
mod reporter;
|
||||
mod thermometer;
|
||||
|
||||
pub use builders::{HouseBuilder, RoomBuilder};
|
||||
@@ -14,5 +15,6 @@ pub use error::Error;
|
||||
pub use house::House;
|
||||
pub use power_socket::PowerSocket;
|
||||
pub use print_status::PrintStatus;
|
||||
pub use reporter::Reporter;
|
||||
pub use room::Room;
|
||||
pub use thermometer::Thermometer;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub trait PrintStatus {
|
||||
fn print_status_into(&self, out: &mut impl std::io::Write) -> Result<(), std::io::Error>;
|
||||
fn print_status_into(&self, out: &mut dyn std::io::Write) -> Result<(), std::io::Error>;
|
||||
|
||||
fn print_status(&self) {
|
||||
if let Err(e) = self.print_status_into(&mut std::io::stdout()) {
|
||||
|
||||
30
smart-house/src/reporter.rs
Normal file
30
smart-house/src/reporter.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use crate::PrintStatus;
|
||||
|
||||
pub struct Reporter<'a> {
|
||||
reportables: Vec<&'a dyn PrintStatus>,
|
||||
}
|
||||
|
||||
impl<'a> Reporter<'a> {
|
||||
pub fn new() -> Self {
|
||||
Self { reportables: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn add_reportable<T: PrintStatus>(mut self, reportable: &'a T) -> Self {
|
||||
self.reportables.push(reportable);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn report(&self) {
|
||||
println!("{}", "=".repeat(16));
|
||||
for reportable in &self.reportables {
|
||||
reportable.print_status();
|
||||
println!("\n{}", "=".repeat(16));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Default for Reporter<'a> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ impl Room {
|
||||
}
|
||||
|
||||
impl PrintStatus for Room {
|
||||
fn print_status_into(&self, out: &mut impl Write) -> Result<(), std::io::Error> {
|
||||
fn print_status_into(&self, out: &mut dyn Write) -> Result<(), std::io::Error> {
|
||||
for (name, device) in self.devices.iter() {
|
||||
out.write_fmt(format_args!("{} => ", name))?;
|
||||
device.print_status_into(out)?;
|
||||
|
||||
Reference in New Issue
Block a user