homework: extract print_status into a separate trait

This commit is contained in:
7 changed files with 22 additions and 10 deletions

View File

@@ -89,7 +89,7 @@
соответствующими ключами.
Доработать формирование отчёта:
- [ ] Вынести метод формирования отчёта в трейт и реализовать его на всех типах, которые возвращают отчёт: умное устройство,
- [x] Вынести метод формирования отчёта в трейт и реализовать его на всех типах, которые возвращают отчёт: умное устройство,
комната, дом.
Привести тесты в соответствие с новым функционалом.

View File

@@ -1,3 +1,4 @@
use crate::PrintStatus;
use std::fmt::Display;
#[derive(Debug)]
@@ -17,10 +18,6 @@ impl Device {
}
}
}
pub fn print_status(&self) {
println!("{}", self.display());
}
}
impl From<super::Thermometer> for Device {
@@ -35,6 +32,12 @@ impl From<super::PowerSocket> for Device {
}
}
impl PrintStatus for Device {
fn print_status(&self) {
println!("{}", self.display());
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -1,4 +1,4 @@
use crate::{Device, Error, Room};
use crate::{Device, Error, PrintStatus, Room};
use std::collections::HashMap;
#[derive(Debug)]
@@ -36,8 +36,10 @@ impl House {
};
Ok(device)
}
}
pub fn print_status(&self) {
impl PrintStatus for House {
fn print_status(&self) {
for (room_name, room) in self.rooms.iter() {
println!("{}:", room_name);
println!("{}", "-".repeat(32));

View File

@@ -4,11 +4,13 @@ mod house;
mod power_socket;
#[macro_use]
mod room;
mod print_status;
mod thermometer;
pub use device::Device;
pub use error::Error;
pub use house::House;
pub use power_socket::PowerSocket;
pub use print_status::PrintStatus;
pub use room::Room;
pub use thermometer::Thermometer;

View File

@@ -1,4 +1,4 @@
use smart_house::{Device, House, PowerSocket, Room, Thermometer, room};
use smart_house::{Device, House, PowerSocket, PrintStatus, Room, Thermometer, room};
fn main() {
let mut house = House::new(

View File

@@ -0,0 +1,3 @@
pub trait PrintStatus {
fn print_status(&self);
}

View File

@@ -1,4 +1,4 @@
use crate::Device;
use crate::{Device, PrintStatus};
use std::collections::HashMap;
#[derive(Debug)]
@@ -26,8 +26,10 @@ impl Room {
pub fn remove_device(&mut self, name: &str) -> Option<Device> {
self.devices.remove(name)
}
}
pub fn print_status(&self) {
impl PrintStatus for Room {
fn print_status(&self) {
for (name, device) in self.devices.iter() {
print!("{} => ", name);
device.print_status();