From fd96ed3e7009a5ae44a2227f3d7835c0e43d0744 Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Mon, 23 Feb 2026 20:51:45 +0300 Subject: [PATCH] homework: power socket mock - do not fail on unknown command --- smart-house/src/bin/power_socket_mock.rs | 4 +- smart-house/src/bin/stubs_example.rs | 8 ++-- smart-house/src/device.rs | 4 +- smart-house/src/house.rs | 4 +- smart-house/src/room.rs | 4 +- smart-house/src/thermometer.rs | 57 ++++++++++++++++++++---- 6 files changed, 61 insertions(+), 20 deletions(-) diff --git a/smart-house/src/bin/power_socket_mock.rs b/smart-house/src/bin/power_socket_mock.rs index 05bf185..fecb398 100644 --- a/smart-house/src/bin/power_socket_mock.rs +++ b/smart-house/src/bin/power_socket_mock.rs @@ -64,7 +64,9 @@ fn handle_connection(mut stream: TcpStream, real_power_socket: Arc panic!("unexpected command: {}", buf[0]), + _ => { + println!("unknown command {} - ignore it", buf[0]); + } } } } diff --git a/smart-house/src/bin/stubs_example.rs b/smart-house/src/bin/stubs_example.rs index c81b8ad..16d01d5 100644 --- a/smart-house/src/bin/stubs_example.rs +++ b/smart-house/src/bin/stubs_example.rs @@ -26,21 +26,21 @@ fn create_house_demo() -> House { "Hall".to_string(), room!( "PSocA" => PowerSocket::stub(9.5, true), - "ThermA" => Thermometer::new(20.1), + "ThermA" => Thermometer::stub(20.1), ), ), ( "Main".to_string(), room!( "PSocB" => PowerSocket::stub(11.2, true), - "ThermB" => Thermometer::new(24.5), + "ThermB" => Thermometer::stub(24.5), "PSocC" => PowerSocket::stub(10.4, true), ), ), ( "Bedroom".to_string(), room!( - "ThermC" => Thermometer::new(19.3), + "ThermC" => Thermometer::stub(19.3), "PSocD" => PowerSocket::stub(12.1, true), ), ), @@ -68,7 +68,7 @@ fn add_new_room_in_house_demo(house: &mut House) { house.insert_room( "Closet", room!( - "ThermD" => Thermometer::new(9.5) + "ThermD" => Thermometer::stub(9.5) ), ); house.print_status(); diff --git a/smart-house/src/device.rs b/smart-house/src/device.rs index cf16565..2cb4ae3 100644 --- a/smart-house/src/device.rs +++ b/smart-house/src/device.rs @@ -45,7 +45,7 @@ mod tests { #[test] fn smoke_test() { - let dev_thermometer = Device::Thermometer(Thermometer::new(20.1)); + let dev_thermometer = Device::Thermometer(Thermometer::stub(20.1)); let dev_power_socket = Device::PowerSocket(PowerSocket::stub(11.2, false)); dev_thermometer.print_status(); @@ -62,7 +62,7 @@ mod tests { #[test] fn display_test() { - let dev_thermometer = Device::Thermometer(Thermometer::new(20.1)); + let dev_thermometer = Device::Thermometer(Thermometer::stub(20.1)); let dev_power_socket = Device::PowerSocket(PowerSocket::stub(11.2, false)); assert_eq!(format!("{}", dev_thermometer.display()), "DEV:Thermometer[ 20.1 ]"); diff --git a/smart-house/src/house.rs b/smart-house/src/house.rs index 72ab80a..cf96164 100644 --- a/smart-house/src/house.rs +++ b/smart-house/src/house.rs @@ -61,7 +61,7 @@ mod tests { "main".to_string(), Room::new( [ - ("ThermA".to_string(), Thermometer::new(20.0).into()), + ("ThermA".to_string(), Thermometer::stub(20.0).into()), ("PSocA".to_string(), PowerSocket::stub(12.34, false).into()), ("PSocB".to_string(), PowerSocket::stub(10.01, true).into()), ] @@ -74,7 +74,7 @@ mod tests { Room::new( [ ("PSocC".to_string(), PowerSocket::stub(11.11, true).into()), - ("ThermB".to_string(), Thermometer::new(17.99).into()), + ("ThermB".to_string(), Thermometer::stub(17.99).into()), ] .into_iter() .collect(), diff --git a/smart-house/src/room.rs b/smart-house/src/room.rs index 06bae14..735899f 100644 --- a/smart-house/src/room.rs +++ b/smart-house/src/room.rs @@ -54,7 +54,7 @@ mod tests { fn create_test_room() -> Room { room!( "PSoc" => PowerSocket::stub(12.34, false), - "Therm" => Thermometer::new(21.56), + "Therm" => Thermometer::stub(21.56), ) } @@ -85,7 +85,7 @@ mod tests { #[test] fn test_add_remove() { let mut room = create_test_room(); - let result = room.insert_device("NewTerm", Thermometer::new(20.0).into()); + let result = room.insert_device("NewTerm", Thermometer::stub(20.0).into()); assert!(result.is_none()); assert_eq!(room.devices.len(), 3); diff --git a/smart-house/src/thermometer.rs b/smart-house/src/thermometer.rs index a6ca607..9925aaa 100644 --- a/smart-house/src/thermometer.rs +++ b/smart-house/src/thermometer.rs @@ -1,17 +1,19 @@ -use std::fmt::Display; +use std::fmt::{Debug, Display}; #[derive(Debug)] pub struct Thermometer { - temperature: f32, + handle: Box, } impl Thermometer { - pub fn new(temperature: f32) -> Self { - Self { temperature } + pub fn stub(temperature: f32) -> Self { + Self { + handle: Box::new(ThermometerHandleStub::new(temperature)), + } } pub fn get_temperature(&self) -> f32 { - self.temperature + self.handle.get_temperature() } pub fn display(&self) -> impl Display { @@ -19,20 +21,57 @@ impl Thermometer { } } +trait ThermometerHandle: Debug { + fn get_temperature(&self) -> f32; +} + +#[derive(Debug)] +struct ThermometerHandleStub { + temperature: f32, +} + +impl ThermometerHandleStub { + fn new(temperature: f32) -> Self { + Self { temperature } + } +} + +impl ThermometerHandle for ThermometerHandleStub { + fn get_temperature(&self) -> f32 { + self.temperature + } +} + +#[derive(Debug)] +struct ThermometerClient { + value: f32, +} + +impl ThermometerClient { + fn new() -> Self { + Self { value: f32::NAN } + } +} + +impl ThermometerHandle for ThermometerClient { + fn get_temperature(&self) -> f32 { + self.value + } +} + #[cfg(test)] mod tests { use super::*; #[test] fn smoke_test() { - let thermometer = Thermometer::new(20.0); - assert_eq!(thermometer.temperature, 20.0); + let thermometer = Thermometer::stub(20.0); assert_eq!(thermometer.get_temperature(), 20.0); } #[test] fn display_test() { - assert_eq!(format!("{}", Thermometer::new(19.550).display()), "Thermometer[ 19.5 ]"); - assert_eq!(format!("{}", Thermometer::new(19.551).display()), "Thermometer[ 19.6 ]"); + assert_eq!(format!("{}", Thermometer::stub(19.550).display()), "Thermometer[ 19.5 ]"); + assert_eq!(format!("{}", Thermometer::stub(19.551).display()), "Thermometer[ 19.6 ]"); } }