Rename the formatters

This commit is contained in:
Savanni D'Gerinel 2024-01-31 08:38:17 -05:00
parent 2cbd539bf4
commit 55c1a6372f
3 changed files with 77 additions and 81 deletions

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com> Copyright 2023-2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of FitnessTrax. This file is part of FitnessTrax.
@ -16,7 +16,7 @@ You should have received a copy of the GNU General Public License along with Fit
use crate::{ use crate::{
components::TextEntry, components::TextEntry,
types::{FormatOption, Weight}, types::{FormatOption, WeightFormatter},
}; };
use gtk::prelude::*; use gtk::prelude::*;
@ -25,7 +25,7 @@ pub struct WeightLabel {
} }
impl WeightLabel { impl WeightLabel {
pub fn new(weight: Option<Weight>) -> Self { pub fn new(weight: Option<WeightFormatter>) -> Self {
let label = gtk::Label::builder() let label = gtk::Label::builder()
.css_classes(["card", "weight-view"]) .css_classes(["card", "weight-view"])
.can_focus(true) .can_focus(true)
@ -44,16 +44,19 @@ impl WeightLabel {
} }
} }
pub fn weight_editor<OnUpdate>(weight: Option<Weight>, on_update: OnUpdate) -> TextEntry<Weight> pub fn weight_editor<OnUpdate>(
weight: Option<WeightFormatter>,
on_update: OnUpdate,
) -> TextEntry<WeightFormatter>
where where
OnUpdate: Fn(Weight) + 'static, OnUpdate: Fn(WeightFormatter) + 'static,
{ {
TextEntry::new( TextEntry::new(
"0 kg", "0 kg",
weight, weight,
|val: &Weight| val.format(FormatOption::Abbreviated), |val: &WeightFormatter| val.format(FormatOption::Abbreviated),
move |v: &str| { move |v: &str| {
let new_weight = Weight::parse(v); let new_weight = WeightFormatter::parse(v);
match new_weight { match new_weight {
Ok(w) => { Ok(w) => {
on_update(w); on_update(w);

View File

@ -57,9 +57,9 @@ pub enum FormatOption {
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Time(chrono::NaiveTime); pub struct TimeFormatter(chrono::NaiveTime);
impl Time { impl TimeFormatter {
fn format(&self, option: FormatOption) -> String { fn format(&self, option: FormatOption) -> String {
match option { match option {
FormatOption::Abbreviated => self.0.format("%H:%M"), FormatOption::Abbreviated => self.0.format("%H:%M"),
@ -68,7 +68,7 @@ impl Time {
.to_string() .to_string()
} }
fn parse(s: &str) -> Result<Time, ParseError> { fn parse(s: &str) -> Result<TimeFormatter, ParseError> {
let parts = s let parts = s
.split(':') .split(':')
.map(|part| part.parse::<u32>().map_err(|_| ParseError)) .map(|part| part.parse::<u32>().map_err(|_| ParseError))
@ -76,10 +76,10 @@ impl Time {
match parts.len() { match parts.len() {
0 => Err(ParseError), 0 => Err(ParseError),
1 => Err(ParseError), 1 => Err(ParseError),
2 => Ok(Time( 2 => Ok(TimeFormatter(
chrono::NaiveTime::from_hms_opt(parts[0], parts[1], 0).unwrap(), chrono::NaiveTime::from_hms_opt(parts[0], parts[1], 0).unwrap(),
)), )),
3 => Ok(Time( 3 => Ok(TimeFormatter(
chrono::NaiveTime::from_hms_opt(parts[0], parts[1], parts[2]).unwrap(), chrono::NaiveTime::from_hms_opt(parts[0], parts[1], parts[2]).unwrap(),
)), )),
_ => Err(ParseError), _ => Err(ParseError),
@ -87,39 +87,23 @@ impl Time {
} }
} }
/* impl std::ops::Deref for TimeFormatter {
impl std::ops::Add for Time {
type Output = Time;
fn add(self, rside: chrono::Duration) -> Self::Output {
Self::Output::from(self.0 + rside)
}
}
impl std::ops::Sub for Time {
type Output = Time;
fn sub(self, rside: chrono::Duration) -> Self::Output {
Self::Output::from(self.0 - rside)
}
}
*/
impl std::ops::Deref for Time {
type Target = chrono::NaiveTime; type Target = chrono::NaiveTime;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
} }
impl From<chrono::NaiveTime> for Time { impl From<chrono::NaiveTime> for TimeFormatter {
fn from(value: chrono::NaiveTime) -> Self { fn from(value: chrono::NaiveTime) -> Self {
Self(value) Self(value)
} }
} }
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Weight(si::Kilogram<f64>); pub struct WeightFormatter(si::Kilogram<f64>);
impl Weight { impl WeightFormatter {
pub fn format(&self, option: FormatOption) -> String { pub fn format(&self, option: FormatOption) -> String {
match option { match option {
FormatOption::Abbreviated => format!("{} kg", self.0.value_unsafe), FormatOption::Abbreviated => format!("{} kg", self.0.value_unsafe),
@ -127,44 +111,44 @@ impl Weight {
} }
} }
pub fn parse(s: &str) -> Result<Weight, ParseError> { pub fn parse(s: &str) -> Result<WeightFormatter, ParseError> {
s.parse::<f64>() s.parse::<f64>()
.map(|w| Weight(w * si::KG)) .map(|w| WeightFormatter(w * si::KG))
.map_err(|_| ParseError) .map_err(|_| ParseError)
} }
} }
impl std::ops::Add for Weight { impl std::ops::Add for WeightFormatter {
type Output = Weight; type Output = WeightFormatter;
fn add(self, rside: Self) -> Self::Output { fn add(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 + rside.0) Self::Output::from(self.0 + rside.0)
} }
} }
impl std::ops::Sub for Weight { impl std::ops::Sub for WeightFormatter {
type Output = Weight; type Output = WeightFormatter;
fn sub(self, rside: Self) -> Self::Output { fn sub(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 - rside.0) Self::Output::from(self.0 - rside.0)
} }
} }
impl std::ops::Deref for Weight { impl std::ops::Deref for WeightFormatter {
type Target = si::Kilogram<f64>; type Target = si::Kilogram<f64>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
} }
impl From<si::Kilogram<f64>> for Weight { impl From<si::Kilogram<f64>> for WeightFormatter {
fn from(value: si::Kilogram<f64>) -> Self { fn from(value: si::Kilogram<f64>) -> Self {
Self(value) Self(value)
} }
} }
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Distance(si::Meter<f64>); pub struct DistanceFormatter(si::Meter<f64>);
impl Distance { impl DistanceFormatter {
pub fn format(&self, option: FormatOption) -> String { pub fn format(&self, option: FormatOption) -> String {
match option { match option {
FormatOption::Abbreviated => format!("{} km", self.0.value_unsafe / 1000.), FormatOption::Abbreviated => format!("{} km", self.0.value_unsafe / 1000.),
@ -172,43 +156,43 @@ impl Distance {
} }
} }
pub fn parse(s: &str) -> Result<Distance, ParseError> { pub fn parse(s: &str) -> Result<DistanceFormatter, ParseError> {
let value = s.parse::<f64>().map_err(|_| ParseError)?; let value = s.parse::<f64>().map_err(|_| ParseError)?;
Ok(Distance(value * 1000. * si::M)) Ok(DistanceFormatter(value * 1000. * si::M))
} }
} }
impl std::ops::Add for Distance { impl std::ops::Add for DistanceFormatter {
type Output = Distance; type Output = DistanceFormatter;
fn add(self, rside: Self) -> Self::Output { fn add(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 + rside.0) Self::Output::from(self.0 + rside.0)
} }
} }
impl std::ops::Sub for Distance { impl std::ops::Sub for DistanceFormatter {
type Output = Distance; type Output = DistanceFormatter;
fn sub(self, rside: Self) -> Self::Output { fn sub(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 - rside.0) Self::Output::from(self.0 - rside.0)
} }
} }
impl std::ops::Deref for Distance { impl std::ops::Deref for DistanceFormatter {
type Target = si::Meter<f64>; type Target = si::Meter<f64>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
} }
impl From<si::Meter<f64>> for Distance { impl From<si::Meter<f64>> for DistanceFormatter {
fn from(value: si::Meter<f64>) -> Self { fn from(value: si::Meter<f64>) -> Self {
Self(value) Self(value)
} }
} }
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Duration(si::Second<f64>); pub struct DurationFormatter(si::Second<f64>);
impl Duration { impl DurationFormatter {
pub fn format(&self, option: FormatOption) -> String { pub fn format(&self, option: FormatOption) -> String {
let (hours, minutes) = self.hours_and_minutes(); let (hours, minutes) = self.hours_and_minutes();
let (h, m) = match option { let (h, m) = match option {
@ -222,9 +206,9 @@ impl Duration {
} }
} }
pub fn parse(s: &str) -> Result<Duration, ParseError> { pub fn parse(s: &str) -> Result<DurationFormatter, ParseError> {
let value = s.parse::<f64>().map_err(|_| ParseError)?; let value = s.parse::<f64>().map_err(|_| ParseError)?;
Ok(Duration(value * 60. * si::S)) Ok(DurationFormatter(value * 60. * si::S))
} }
fn hours_and_minutes(&self) -> (i64, i64) { fn hours_and_minutes(&self) -> (i64, i64) {
@ -235,28 +219,28 @@ impl Duration {
} }
} }
impl std::ops::Add for Duration { impl std::ops::Add for DurationFormatter {
type Output = Duration; type Output = DurationFormatter;
fn add(self, rside: Self) -> Self::Output { fn add(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 + rside.0) Self::Output::from(self.0 + rside.0)
} }
} }
impl std::ops::Sub for Duration { impl std::ops::Sub for DurationFormatter {
type Output = Duration; type Output = DurationFormatter;
fn sub(self, rside: Self) -> Self::Output { fn sub(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 - rside.0) Self::Output::from(self.0 - rside.0)
} }
} }
impl std::ops::Deref for Duration { impl std::ops::Deref for DurationFormatter {
type Target = si::Second<f64>; type Target = si::Second<f64>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
} }
impl From<si::Second<f64>> for Duration { impl From<si::Second<f64>> for DurationFormatter {
fn from(value: si::Second<f64>) -> Self { fn from(value: si::Second<f64>) -> Self {
Self(value) Self(value)
} }
@ -277,54 +261,63 @@ mod test {
#[test] #[test]
fn it_parses_weight_values() { fn it_parses_weight_values() {
assert_eq!(Weight::parse("15.3"), Ok(Weight(15.3 * si::KG))); assert_eq!(
assert_eq!(Weight::parse("15.ab"), Err(ParseError)); WeightFormatter::parse("15.3"),
Ok(WeightFormatter(15.3 * si::KG))
);
assert_eq!(WeightFormatter::parse("15.ab"), Err(ParseError));
} }
#[test] #[test]
fn it_formats_weight_values() { fn it_formats_weight_values() {
assert_eq!( assert_eq!(
Weight::from(15.3 * si::KG).format(FormatOption::Abbreviated), WeightFormatter::from(15.3 * si::KG).format(FormatOption::Abbreviated),
"15.3 kg" "15.3 kg"
); );
assert_eq!( assert_eq!(
Weight::from(15.3 * si::KG).format(FormatOption::Full), WeightFormatter::from(15.3 * si::KG).format(FormatOption::Full),
"15.3 kilograms" "15.3 kilograms"
); );
} }
#[test] #[test]
fn it_parses_distance_values() { fn it_parses_distance_values() {
assert_eq!(Distance::parse("70"), Ok(Distance(70000. * si::M))); assert_eq!(
assert_eq!(Distance::parse("15.ab"), Err(ParseError)); DistanceFormatter::parse("70"),
Ok(DistanceFormatter(70000. * si::M))
);
assert_eq!(DistanceFormatter::parse("15.ab"), Err(ParseError));
} }
#[test] #[test]
fn it_formats_distance_values() { fn it_formats_distance_values() {
assert_eq!( assert_eq!(
Distance::from(70000. * si::M).format(FormatOption::Abbreviated), DistanceFormatter::from(70000. * si::M).format(FormatOption::Abbreviated),
"70 km" "70 km"
); );
assert_eq!( assert_eq!(
Distance::from(70000. * si::M).format(FormatOption::Full), DistanceFormatter::from(70000. * si::M).format(FormatOption::Full),
"70 kilometers" "70 kilometers"
); );
} }
#[test] #[test]
fn it_parses_duration_values() { fn it_parses_duration_values() {
assert_eq!(Duration::parse("70"), Ok(Duration(4200. * si::S))); assert_eq!(
assert_eq!(Duration::parse("15.ab"), Err(ParseError)); DurationFormatter::parse("70"),
Ok(DurationFormatter(4200. * si::S))
);
assert_eq!(DurationFormatter::parse("15.ab"), Err(ParseError));
} }
#[test] #[test]
fn it_formats_duration_values() { fn it_formats_duration_values() {
assert_eq!( assert_eq!(
Duration::from(4200. * si::S).format(FormatOption::Abbreviated), DurationFormatter::from(4200. * si::S).format(FormatOption::Abbreviated),
"1h 10m" "1h 10m"
); );
assert_eq!( assert_eq!(
Duration::from(4200. * si::S).format(FormatOption::Full), DurationFormatter::from(4200. * si::S).format(FormatOption::Full),
"1 hours 10 minutes" "1 hours 10 minutes"
); );
} }
@ -332,14 +325,14 @@ mod test {
#[test] #[test]
fn it_parses_time_values() { fn it_parses_time_values() {
assert_eq!( assert_eq!(
Time::parse("13:25"), TimeFormatter::parse("13:25"),
Ok(Time::from( Ok(TimeFormatter::from(
chrono::NaiveTime::from_hms_opt(13, 25, 0).unwrap() chrono::NaiveTime::from_hms_opt(13, 25, 0).unwrap()
)), )),
); );
assert_eq!( assert_eq!(
Time::parse("13:25:50"), TimeFormatter::parse("13:25:50"),
Ok(Time::from( Ok(TimeFormatter::from(
chrono::NaiveTime::from_hms_opt(13, 25, 50).unwrap() chrono::NaiveTime::from_hms_opt(13, 25, 50).unwrap()
)), )),
); );
@ -347,7 +340,7 @@ mod test {
#[test] #[test]
fn it_formats_time_values() { fn it_formats_time_values() {
let time = Time::from(chrono::NaiveTime::from_hms_opt(13, 25, 50).unwrap()); let time = TimeFormatter::from(chrono::NaiveTime::from_hms_opt(13, 25, 50).unwrap());
assert_eq!(time.format(FormatOption::Abbreviated), "13:25".to_owned()); assert_eq!(time.format(FormatOption::Abbreviated), "13:25".to_owned());
assert_eq!(time.format(FormatOption::Full), "13:25:50".to_owned()); assert_eq!(time.format(FormatOption::Full), "13:25:50".to_owned());
} }

View File

@ -14,7 +14,7 @@ General Public License for more details.
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>. You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::{app::App, types::Weight}; use crate::{app::App, types::WeightFormatter};
use emseries::{Record, RecordId, Recordable}; use emseries::{Record, RecordId, Recordable};
use ft_core::TraxRecord; use ft_core::TraxRecord;
use std::{ use std::{
@ -124,13 +124,13 @@ impl DayDetailViewModel {
} }
} }
pub fn weight(&self) -> Option<Weight> { pub fn weight(&self) -> Option<WeightFormatter> {
(*self.weight.read().unwrap()) (*self.weight.read().unwrap())
.as_ref() .as_ref()
.map(|w| Weight::from(w.weight)) .map(|w| WeightFormatter::from(w.weight))
} }
pub fn set_weight(&self, new_weight: Weight) { pub fn set_weight(&self, new_weight: WeightFormatter) {
let mut record = self.weight.write().unwrap(); let mut record = self.weight.write().unwrap();
let new_record = match *record { let new_record = match *record {
Some(ref rstate) => rstate.clone().with_value(ft_core::Weight { Some(ref rstate) => rstate.clone().with_value(ft_core::Weight {