Skip to main content

rfham_core/
power.rs

1//!
2//! Provides ..., a one-line description
3//!
4//! More detailed description
5//!
6//! # Examples
7//!
8//! ```rust
9//! ```
10//!
11
12use crate::error::CoreError;
13use serde::{Deserialize, Serialize};
14use std::{fmt::Display, str::FromStr};
15use uom::{
16    fmt::DisplayStyle,
17    si::{f64::Power as BasePower, power as power_unit},
18};
19
20// ------------------------------------------------------------------------------------------------
21// Public Macros
22// ------------------------------------------------------------------------------------------------
23
24// ------------------------------------------------------------------------------------------------
25// Public Types
26// ------------------------------------------------------------------------------------------------
27
28#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Deserialize, Serialize)]
29pub struct Power(BasePower);
30
31// ------------------------------------------------------------------------------------------------
32// Public Functions
33// ------------------------------------------------------------------------------------------------
34pub fn from_dc_circuit(voltage: f64, current: f64) -> Power {
35    watts(voltage * current)
36}
37
38pub fn from_ac_circuit(voltage: f64, current: f64, factor: f64) -> Power {
39    assert!((0.0..=1.0).contains(&factor));
40    watts(voltage * current * factor)
41}
42
43pub fn milliwatts(value: f64) -> Power {
44    Power(BasePower::new::<power_unit::milliwatt>(value))
45}
46
47pub fn watts(value: f64) -> Power {
48    Power(BasePower::new::<power_unit::watt>(value))
49}
50
51pub fn kilowatts(value: f64) -> Power {
52    Power(BasePower::new::<power_unit::kilowatt>(value))
53}
54
55// ------------------------------------------------------------------------------------------------
56// Private Macros
57// ------------------------------------------------------------------------------------------------
58
59// ------------------------------------------------------------------------------------------------
60// Private Types
61// ------------------------------------------------------------------------------------------------
62
63// ------------------------------------------------------------------------------------------------
64// Implementations
65// ------------------------------------------------------------------------------------------------
66
67impl Display for Power {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        if f.alternate() {
70            if self.value() < 0.001 {
71                self.0
72                    .into_format_args(power_unit::milliwatt, DisplayStyle::Description)
73                    .fmt(f)
74            } else if self.value() >= 1000.0 {
75                self.0
76                    .into_format_args(power_unit::kilowatt, DisplayStyle::Description)
77                    .fmt(f)
78            } else {
79                self.0
80                    .into_format_args(power_unit::watt, DisplayStyle::Description)
81                    .fmt(f)
82            }
83        } else {
84            self.0
85                .into_format_args(power_unit::watt, DisplayStyle::Abbreviation)
86                .fmt(f)
87        }
88    }
89}
90
91impl FromStr for Power {
92    type Err = CoreError;
93
94    fn from_str(s: &str) -> Result<Self, Self::Err> {
95        BasePower::from_str(s)
96            .map(Self)
97            .map_err(|_| CoreError::InvalidValueFromStr(s.to_string(), "Power"))
98    }
99}
100
101impl From<BasePower> for Power {
102    fn from(value: BasePower) -> Self {
103        Self(value)
104    }
105}
106
107impl From<f64> for Power {
108    fn from(value: f64) -> Self {
109        watts(value)
110    }
111}
112
113impl From<Power> for BasePower {
114    fn from(value: Power) -> Self {
115        value.0
116    }
117}
118
119impl From<Power> for f64 {
120    fn from(value: Power) -> Self {
121        value.0.value
122    }
123}
124
125impl AsRef<BasePower> for Power {
126    fn as_ref(&self) -> &BasePower {
127        &self.0
128    }
129}
130
131impl Power {
132    pub fn value(&self) -> f64 {
133        self.0.value
134    }
135}
136
137// ------------------------------------------------------------------------------------------------
138// Private Functions
139// ------------------------------------------------------------------------------------------------
140
141// ------------------------------------------------------------------------------------------------
142// Sub-Modules
143// ------------------------------------------------------------------------------------------------