use std::cmp::Ordering;
use std::fmt;
use super::traits::{ParseableScore, Score, ScoreParseError};
use super::ScoreLevel;
const SCALE: i64 = 100_000;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HardSoftDecimalScore {
hard: i64,
soft: i64,
}
impl HardSoftDecimalScore {
pub const ZERO: HardSoftDecimalScore = HardSoftDecimalScore { hard: 0, soft: 0 };
pub const ONE_HARD: HardSoftDecimalScore = HardSoftDecimalScore {
hard: SCALE,
soft: 0,
};
pub const ONE_SOFT: HardSoftDecimalScore = HardSoftDecimalScore {
hard: 0,
soft: SCALE,
};
#[inline]
pub const fn of(hard: i64, soft: i64) -> Self {
HardSoftDecimalScore {
hard: hard.strict_mul(SCALE),
soft: soft.strict_mul(SCALE),
}
}
#[inline]
pub const fn of_scaled(hard: i64, soft: i64) -> Self {
HardSoftDecimalScore { hard, soft }
}
#[inline]
pub const fn of_hard(hard: i64) -> Self {
HardSoftDecimalScore {
hard: hard.strict_mul(SCALE),
soft: 0,
}
}
#[inline]
pub const fn of_soft(soft: i64) -> Self {
HardSoftDecimalScore {
hard: 0,
soft: soft.strict_mul(SCALE),
}
}
pub fn parse_level(text: &str) -> Result<i64, ScoreParseError> {
parse_fixed(text).map_err(|reason| ScoreParseError {
message: format!("Invalid HardSoftDecimalScore level '{}': {}", text, reason),
})
}
#[inline]
pub const fn of_hard_scaled(hard: i64) -> Self {
HardSoftDecimalScore { hard, soft: 0 }
}
#[inline]
pub const fn of_soft_scaled(soft: i64) -> Self {
HardSoftDecimalScore { hard: 0, soft }
}
#[inline]
pub const fn hard_scaled(&self) -> i64 {
self.hard
}
#[inline]
pub const fn soft_scaled(&self) -> i64 {
self.soft
}
pub const fn hard_score(&self) -> HardSoftDecimalScore {
HardSoftDecimalScore::of_scaled(self.hard, 0)
}
pub const fn soft_score(&self) -> HardSoftDecimalScore {
HardSoftDecimalScore::of_scaled(0, self.soft)
}
#[inline]
pub const fn has_hard_component(&self) -> bool {
self.hard != 0
}
}
impl Score for HardSoftDecimalScore {
#[inline]
fn is_feasible(&self) -> bool {
self.hard >= 0
}
#[inline]
fn zero() -> Self {
HardSoftDecimalScore::ZERO
}
#[inline]
fn levels_count() -> usize {
2
}
#[inline]
fn level_number(&self, index: usize) -> i64 {
match index {
0 => self.hard,
1 => self.soft,
_ => panic!("HardSoftDecimalScore has 2 levels, got index {}", index),
}
}
fn from_level_numbers(levels: &[i64]) -> Self {
assert_eq!(
levels.len(),
2,
"HardSoftDecimalScore requires exactly 2 levels"
);
HardSoftDecimalScore::of_scaled(levels[0], levels[1])
}
impl_score_scale!(HardSoftDecimalScore { hard, soft } => of_scaled);
fn level_label(index: usize) -> ScoreLevel {
match index {
0 => ScoreLevel::Hard,
1 => ScoreLevel::Soft,
_ => panic!("HardSoftDecimalScore has 2 levels, got index {}", index),
}
}
#[inline]
fn to_scalar(&self) -> f64 {
self.hard as f64 * 1_000_000.0 + self.soft as f64
}
}
impl Ord for HardSoftDecimalScore {
fn cmp(&self, other: &Self) -> Ordering {
match self.hard.cmp(&other.hard) {
Ordering::Equal => self.soft.cmp(&other.soft),
other => other,
}
}
}
impl_score_ops!(HardSoftDecimalScore { hard, soft } => of_scaled);
impl fmt::Debug for HardSoftDecimalScore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"HardSoftDecimalScore({}, {})",
Fixed(self.hard),
Fixed(self.soft)
)
}
}
impl fmt::Display for HardSoftDecimalScore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}hard/{}soft", Fixed(self.hard), Fixed(self.soft))
}
}
struct Fixed(i64);
impl fmt::Display for Fixed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let magnitude = self.0.unsigned_abs();
let scale = SCALE as u64;
let sign = if self.0 < 0 { "-" } else { "" };
let (whole, mut fraction) = (magnitude / scale, magnitude % scale);
if fraction == 0 {
return write!(f, "{sign}{whole}");
}
let mut digits = FRACTION_DIGITS;
while fraction % 10 == 0 {
fraction /= 10;
digits -= 1;
}
write!(f, "{sign}{whole}.{fraction:0digits$}")
}
}
const FRACTION_DIGITS: usize = 5;
fn parse_fixed(text: &str) -> Result<i64, String> {
let (negative, unsigned) = match text.as_bytes().first() {
Some(b'-') => (true, &text[1..]),
Some(b'+') => (false, &text[1..]),
_ => (false, text),
};
let (whole, fraction) = match unsigned.split_once('.') {
Some((whole, fraction)) => (whole, Some(fraction)),
None => (unsigned, None),
};
let is_digits = |part: &str| !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit());
if !is_digits(whole) || fraction.is_some_and(|fraction| !is_digits(fraction)) {
return Err("expected a decimal number such as -12 or 3.25".to_string());
}
let fraction = fraction.unwrap_or("");
if fraction.len() > FRACTION_DIGITS {
return Err(format!(
"at most {FRACTION_DIGITS} decimal places are representable"
));
}
let out_of_range = || "out of range for a HardSoftDecimalScore level".to_string();
let mut scaled: i128 = 0;
for digit in whole.bytes().chain(fraction.bytes()) {
scaled = scaled
.checked_mul(10)
.and_then(|value| value.checked_add(i128::from(digit - b'0')))
.filter(|value| *value <= i128::from(i64::MAX) + 1)
.ok_or_else(out_of_range)?;
}
for _ in fraction.len()..FRACTION_DIGITS {
scaled = scaled
.checked_mul(10)
.filter(|value| *value <= i128::from(i64::MAX) + 1)
.ok_or_else(out_of_range)?;
}
let scaled = if negative { -scaled } else { scaled };
i64::try_from(scaled).map_err(|_| out_of_range())
}
impl ParseableScore for HardSoftDecimalScore {
fn parse(s: &str) -> Result<Self, ScoreParseError> {
let s = s.trim();
let parts: Vec<&str> = s.split('/').collect();
if parts.len() != 2 {
return Err(ScoreParseError {
message: format!(
"Invalid HardSoftDecimalScore format '{}': expected 'Xhard/Ysoft'",
s
),
});
}
let hard_str = parts[0]
.trim()
.strip_suffix("hard")
.ok_or_else(|| ScoreParseError {
message: format!("Hard score part '{}' must end with 'hard'", parts[0]),
})?;
let soft_str = parts[1]
.trim()
.strip_suffix("soft")
.ok_or_else(|| ScoreParseError {
message: format!("Soft score part '{}' must end with 'soft'", parts[1]),
})?;
let hard = parse_fixed(hard_str).map_err(|reason| ScoreParseError {
message: format!("Invalid hard score '{}': {}", hard_str, reason),
})?;
let soft = parse_fixed(soft_str).map_err(|reason| ScoreParseError {
message: format!("Invalid soft score '{}': {}", soft_str, reason),
})?;
Ok(HardSoftDecimalScore::of_scaled(hard, soft))
}
fn to_string_repr(&self) -> String {
format!("{}", self)
}
}