easy_ffprobe/
ratio.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use core::fmt;
use std::{fmt::Display, str::FromStr};

use serde::{
    de::{self, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};

#[derive(Debug, Clone, PartialEq, Eq)]
/// Ratio eg. 1/1000
pub struct Ratio((u64, u64));

impl Ratio {
    pub fn numerator(&self) -> u64 {
        self.0 .0
    }
    pub fn denominator(&self) -> u64 {
        self.0 .1
    }
}

impl Display for Ratio {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.0 .0, self.0 .1)
    }
}

impl FromStr for Ratio {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = s.split(|c| c == ':' || c == '/').collect();
        if parts.len() != 2 {
            return Err("Invalid ratio format".to_string());
        }
        let numerator = parts[0].parse::<u64>().map_err(|_| "Invalid number")?;
        let denominator = parts[1].parse::<u64>().map_err(|_| "Invalid number")?;
        Ok(Ratio((numerator, denominator)))
    }
}

impl Serialize for Ratio {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> Deserialize<'de> for Ratio {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(RatioVisitor)
    }
}

struct RatioVisitor;

impl<'de> Visitor<'de> for RatioVisitor {
    type Value = Ratio;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(
            "a ratio in the format of 'numerator:denominator' or 'numerator/denominator'",
        )
    }

    fn visit_str<E>(self, value: &str) -> Result<Ratio, E>
    where
        E: de::Error,
    {
        Ratio::from_str(value).map_err(de::Error::custom)
    }
}