Skip to main content

simp_rexif/
rational.rs

1use std::fmt;
2use std::fmt::Display;
3
4/// Encapsulation of the TIFF type that represents a signed rational number
5#[derive(Copy, Clone, Debug, PartialEq)]
6pub struct IRational {
7    pub numerator: i32,
8    pub denominator: i32,
9}
10
11impl IRational {
12    /// Floating point value (numerator divided by denominator)
13    #[must_use]
14    pub fn value(&self) -> f64 {
15        f64::from(self.numerator) / f64::from(self.denominator)
16    }
17}
18
19impl Display for IRational {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        write!(f, "{}/{}", self.numerator, self.denominator)
22    }
23}
24
25#[derive(Copy, Clone, Debug, PartialEq)]
26/// Encapsulation of the TIFF type that represents an unsigned rational number
27pub struct URational {
28    pub numerator: u32,
29    pub denominator: u32,
30}
31
32impl URational {
33    /// Floating point value (numerator divided by denominator)
34    #[must_use]
35    pub fn value(&self) -> f64 {
36        f64::from(self.numerator) / f64::from(self.denominator)
37    }
38}
39
40impl Display for URational {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        write!(f, "{}/{}", self.numerator, self.denominator)
43    }
44}