1use core::fmt;
2use std::{fmt::Display, str::FromStr};
3
4use serde::{
5 de::{self, Visitor},
6 Deserialize, Deserializer, Serialize, Serializer,
7};
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct Ratio((u64, u64));
12
13impl Ratio {
14 pub fn numerator(&self) -> u64 {
15 self.0 .0
16 }
17 pub fn denominator(&self) -> u64 {
18 self.0 .1
19 }
20}
21
22impl Display for Ratio {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 write!(f, "{}:{}", self.0 .0, self.0 .1)
25 }
26}
27
28impl FromStr for Ratio {
29 type Err = String;
30
31 fn from_str(s: &str) -> Result<Self, Self::Err> {
32 let parts: Vec<&str> = s.split(|c| c == ':' || c == '/').collect();
33 if parts.len() != 2 {
34 return Err("Invalid ratio format".to_string());
35 }
36 let numerator = parts[0].parse::<u64>().map_err(|_| "Invalid number")?;
37 let denominator = parts[1].parse::<u64>().map_err(|_| "Invalid number")?;
38 Ok(Ratio((numerator, denominator)))
39 }
40}
41
42impl Serialize for Ratio {
43 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
44 where
45 S: Serializer,
46 {
47 serializer.serialize_str(&self.to_string())
48 }
49}
50
51impl<'de> Deserialize<'de> for Ratio {
52 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
53 where
54 D: Deserializer<'de>,
55 {
56 deserializer.deserialize_str(RatioVisitor)
57 }
58}
59
60struct RatioVisitor;
61
62impl<'de> Visitor<'de> for RatioVisitor {
63 type Value = Ratio;
64
65 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
66 formatter.write_str(
67 "a ratio in the format of 'numerator:denominator' or 'numerator/denominator'",
68 )
69 }
70
71 fn visit_str<E>(self, value: &str) -> Result<Ratio, E>
72 where
73 E: de::Error,
74 {
75 Ratio::from_str(value).map_err(de::Error::custom)
76 }
77}