1use core::str;
2use std::ops::Deref;
3use std::str::FromStr;
4
5#[derive(Debug, Default, PartialEq, PartialOrd, Clone)]
7pub struct UnitValue<T> {
8 pub value: T,
9 pub unit: Option<String>,
10}
11
12impl<T> UnitValue<T> {
13 pub fn without_unit(value: T) -> Self {
14 Self { value, unit: None }
15 }
16
17 pub fn with_unit(value: T, unit: impl Into<String>) -> Self {
18 Self {
19 value,
20 unit: Some(unit.into()),
21 }
22 }
23}
24
25impl<T> Deref for UnitValue<T> {
26 type Target = T;
27
28 fn deref(&self) -> &Self::Target {
29 &self.value
30 }
31}
32
33impl<T: FromStr> FromStr for UnitValue<T> {
34 type Err = T::Err;
35
36 fn from_str(s: &str) -> Result<Self, Self::Err> {
37 let (value, unit) = s.split_once('*').map_or((s, None), |(val, unit)| (val, Some(unit)));
38 Ok(Self {
39 value: T::from_str(value)?,
40 unit: unit.map(str::to_string),
41 })
42 }
43}