add structs for Thermometer and PowerSocket; add Device trait
This commit is contained in:
45
smart-house/src/device/thermometer.rs
Normal file
45
smart-house/src/device/thermometer.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
pub struct Thermometer {
|
||||
temperature: f32,
|
||||
}
|
||||
|
||||
impl Thermometer {
|
||||
pub fn new(temperature: f32) -> Self {
|
||||
Self { temperature }
|
||||
}
|
||||
|
||||
pub fn get_temperature(&self) -> f32 {
|
||||
self.temperature
|
||||
}
|
||||
|
||||
pub fn display(&self) -> impl Display {
|
||||
format!("Thermometer[ {:02.1} ]", self.get_temperature())
|
||||
}
|
||||
}
|
||||
|
||||
impl super::Device for Thermometer {
|
||||
fn print_status(&self) {
|
||||
println!("{}", self.display())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn smoke_test() {
|
||||
let thermometer = Thermometer::new(20.0);
|
||||
assert_eq!(thermometer.temperature, 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 ]");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user