From f3278c093e65f3c3a2ef892a1dca2c5c20d2fa73 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Sun, 25 Feb 2024 22:26:48 -0800 Subject: [PATCH] Implement TryFrom for f32 to/from MasteryScore conversion (#296) --- Cargo.toml | 2 +- src/data.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aaa4792..7539e66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trane" -version = "0.19.0" +version = "0.19.1" edition = "2021" description = "An automated system for learning complex skills" license = "AGPL-3.0" diff --git a/src/data.rs b/src/data.rs index f4f6dec..19cad54 100644 --- a/src/data.rs +++ b/src/data.rs @@ -22,7 +22,7 @@ use self::course_generator::{ /// The score used by students to evaluate their mastery of a particular exercise after a trial. /// More detailed descriptions of the levels are provided using the example of an exercise that /// requires the student to learn a musical passage. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] pub enum MasteryScore { /// One signifies the student has barely any mastery of the exercise. For a musical passage, /// this level of mastery represents the initial attempts at hearing and reading the music, and @@ -70,6 +70,34 @@ impl MasteryScore { } } +impl TryFrom for f32 { + type Error = (); + + fn try_from(score: MasteryScore) -> Result { + Ok(score.float_score()) + } +} + +impl TryFrom for MasteryScore { + type Error = (); + + fn try_from(score: f32) -> Result { + if (score - 1.0_f32).abs() < f32::EPSILON { + Ok(MasteryScore::One) + } else if (score - 2.0_f32).abs() < f32::EPSILON { + Ok(MasteryScore::Two) + } else if (score - 3.0_f32).abs() < f32::EPSILON { + Ok(MasteryScore::Three) + } else if (score - 4.0_f32).abs() < f32::EPSILON { + Ok(MasteryScore::Four) + } else if (score - 5.0_f32).abs() < f32::EPSILON { + Ok(MasteryScore::Five) + } else { + Err(()) + } + } +} + //@