-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get rid of `format_dosage` and `parse_dosage` functions in favor of `fmt::Display` and `std::str::FromStr` traits implementations which serve same functionality. There are also mixed changes related to output formatting.
- Loading branch information
Showing
8 changed files
with
127 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,63 @@ | ||
use clap::builder::TypedValueParser; | ||
use delegate::delegate; | ||
use derivative::Derivative; | ||
use float_pretty_print::PrettyPrintFloat; | ||
use measurements::{Mass, Measurement}; | ||
use miette::IntoDiagnostic; | ||
use std::str::FromStr; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt; | ||
|
||
pub type Dosage = Mass; | ||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Derivative)] | ||
pub struct Dosage(Mass); | ||
|
||
/// Function will take human-readable input as representation of mass of | ||
/// substance that was ingested (also referred as Dosage) | ||
pub fn parse_dosage(input: &str) -> miette::Result<Dosage> | ||
{ | ||
Mass::from_str(input).into_diagnostic() | ||
impl std::str::FromStr for Dosage { | ||
type Err = String; | ||
|
||
/// Parse a &str into a valid `Dosage`. | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let mass = Mass::from_str(s).unwrap(); | ||
Ok(Dosage(mass)) | ||
} | ||
} | ||
|
||
|
||
impl fmt::Display for Dosage { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let suggested_unit = self.0.get_appropriate_units(); | ||
let value_of_unit = format!("{:4.4}", PrettyPrintFloat(suggested_unit.1)); | ||
let unit = suggested_unit.0; | ||
let formatted = format!("{} {}", value_of_unit.trim_start(), unit); | ||
write!(f, "{}", formatted) | ||
} | ||
} | ||
|
||
pub fn format_dosage(input: &Dosage) -> miette::Result<String> { | ||
let suggested_unit = input.get_appropriate_units(); | ||
let float = format!("{:4.4}", PrettyPrintFloat(suggested_unit.1)); | ||
let unit = suggested_unit.0; | ||
Ok(format!("{} {}", float.trim_start(), unit)) | ||
impl Dosage { | ||
pub fn from_base_units(units: f64) -> Dosage { | ||
Dosage(Mass::from_base_units(units)) | ||
} | ||
|
||
delegate! { | ||
to self.0 { | ||
pub fn as_base_units(&self) -> f64; | ||
} | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests | ||
{ | ||
use super::*; | ||
use std::str::FromStr; | ||
|
||
#[test] | ||
fn test_parse_dosage() | ||
{ | ||
assert_eq!(parse_dosage("100g").unwrap(), Mass::from_grams(100f64)); | ||
assert_eq!(parse_dosage("100kg").unwrap(), Mass::from_kilograms(100f64)); | ||
assert_eq!( | ||
parse_dosage("100ug").unwrap(), | ||
Mass::from_micrograms(100f64) | ||
) | ||
fn test_parse_dosage() { | ||
assert_eq!(Dosage::from_str("100g").unwrap(), Dosage(Mass::from_grams(100f64))); | ||
assert_eq!(Dosage::from_str("100kg").unwrap(), Dosage(Mass::from_kilograms(100f64))); | ||
assert_eq!(Dosage::from_str("100kg").unwrap(), Dosage(Mass::from_kilograms(100f64))); | ||
} | ||
|
||
#[test] | ||
fn test_print_dosage() { | ||
let dosage = Mass::from_grams(0.1); | ||
assert_eq!(format_dosage(&dosage).unwrap(), "100 mg"); | ||
fn test_format_dosage() { | ||
let dosage = Dosage(Mass::from_grams(0.1)); | ||
assert_eq!(dosage.to_string(), "100 mg"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters