use std::fmt;
use sane_sys as sys;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Fixed(sys::SANE_Fixed);
impl Fixed {
pub const MIN: Self = Self(sys::SANE_Fixed::MIN);
pub const MAX: Self = Self(sys::SANE_Fixed::MAX);
pub const EPSILON: Self = Self(1);
pub const fn from_raw(raw: sys::SANE_Fixed) -> Self {
Self(raw)
}
pub const fn to_raw(self) -> sys::SANE_Fixed {
self.0
}
pub fn from_f64(value: f64) -> Self {
Self(sys::SANE_FIX(value))
}
pub fn to_f64(self) -> f64 {
sys::SANE_UNFIX(self.0)
}
}
impl From<i16> for Fixed {
fn from(value: i16) -> Self {
Self(sys::SANE_Fixed::from(value) << sys::SANE_FIXED_SCALE_SHIFT)
}
}
impl From<Fixed> for f64 {
fn from(value: Fixed) -> Self {
value.to_f64()
}
}
impl fmt::Debug for Fixed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Fixed({})", self.to_f64())
}
}
impl fmt::Display for Fixed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.to_f64(), f)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn conversions() {
assert_eq!(Fixed::from(1).to_raw(), 65536);
assert_eq!(Fixed::from(-1).to_raw(), -65536);
assert_eq!(Fixed::from_f64(0.5).to_raw(), 32768);
assert_eq!(Fixed::from_f64(-0.5).to_f64(), -0.5);
assert_eq!(Fixed::from_f64(1e9), Fixed::MAX);
assert_eq!(Fixed::from_f64(-1e9), Fixed::MIN);
assert_eq!(Fixed::from_f64(f64::NAN), Fixed::default());
assert_eq!(f64::from(Fixed::from_raw(3 << 15)), 1.5);
assert_eq!(Fixed::from(i16::MIN), Fixed::MIN);
assert_eq!(Fixed::from_f64(2.25).to_string(), "2.25");
}
#[test]
fn ordering_follows_the_value() {
assert!(Fixed::from(-2) < Fixed::from_f64(-1.5));
assert!(Fixed::from_f64(0.1) < Fixed::from_f64(0.2));
}
}