tailwind_rs_core/utilities/sizing/
fraction.rs1use serde::{Deserialize, Serialize};
4use std::fmt;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub enum Fraction {
9 Half,
11 Third,
13 TwoThirds,
15 Quarter,
17 TwoQuarters,
19 ThreeQuarters,
21 Fifth,
23 TwoFifths,
25 ThreeFifths,
27 FourFifths,
29 Sixth,
31 TwoSixths,
33 ThreeSixths,
35 FourSixths,
37 FiveSixths,
39 Twelfth,
41 TwoTwelfths,
43 ThreeTwelfths,
45 FourTwelfths,
47 FiveTwelfths,
49 SixTwelfths,
51 SevenTwelfths,
53 EightTwelfths,
55 NineTwelfths,
57 TenTwelfths,
59 ElevenTwelfths,
61}
62
63impl fmt::Display for Fraction {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 match self {
66 Fraction::Half => write!(f, "1/2"),
67 Fraction::Third => write!(f, "1/3"),
68 Fraction::TwoThirds => write!(f, "2/3"),
69 Fraction::Quarter => write!(f, "1/4"),
70 Fraction::TwoQuarters => write!(f, "2/4"),
71 Fraction::ThreeQuarters => write!(f, "3/4"),
72 Fraction::Fifth => write!(f, "1/5"),
73 Fraction::TwoFifths => write!(f, "2/5"),
74 Fraction::ThreeFifths => write!(f, "3/5"),
75 Fraction::FourFifths => write!(f, "4/5"),
76 Fraction::Sixth => write!(f, "1/6"),
77 Fraction::TwoSixths => write!(f, "2/6"),
78 Fraction::ThreeSixths => write!(f, "3/6"),
79 Fraction::FourSixths => write!(f, "4/6"),
80 Fraction::FiveSixths => write!(f, "5/6"),
81 Fraction::Twelfth => write!(f, "1/12"),
82 Fraction::TwoTwelfths => write!(f, "2/12"),
83 Fraction::ThreeTwelfths => write!(f, "3/12"),
84 Fraction::FourTwelfths => write!(f, "4/12"),
85 Fraction::FiveTwelfths => write!(f, "5/12"),
86 Fraction::SixTwelfths => write!(f, "6/12"),
87 Fraction::SevenTwelfths => write!(f, "7/12"),
88 Fraction::EightTwelfths => write!(f, "8/12"),
89 Fraction::NineTwelfths => write!(f, "9/12"),
90 Fraction::TenTwelfths => write!(f, "10/12"),
91 Fraction::ElevenTwelfths => write!(f, "11/12"),
92 }
93 }
94}
95
96impl Fraction {
97 pub fn to_class_name(&self) -> String {
98 self.to_string()
99 }
100
101 pub fn to_css_value(&self) -> String {
102 match self {
103 Fraction::Half => "50%".to_string(),
104 Fraction::Third => "33.333333%".to_string(),
105 Fraction::TwoThirds => "66.666667%".to_string(),
106 Fraction::Quarter => "25%".to_string(),
107 Fraction::TwoQuarters => "50%".to_string(),
108 Fraction::ThreeQuarters => "75%".to_string(),
109 Fraction::Fifth => "20%".to_string(),
110 Fraction::TwoFifths => "40%".to_string(),
111 Fraction::ThreeFifths => "60%".to_string(),
112 Fraction::FourFifths => "80%".to_string(),
113 Fraction::Sixth => "16.666667%".to_string(),
114 Fraction::TwoSixths => "33.333333%".to_string(),
115 Fraction::ThreeSixths => "50%".to_string(),
116 Fraction::FourSixths => "66.666667%".to_string(),
117 Fraction::FiveSixths => "83.333333%".to_string(),
118 Fraction::Twelfth => "8.333333%".to_string(),
119 Fraction::TwoTwelfths => "16.666667%".to_string(),
120 Fraction::ThreeTwelfths => "25%".to_string(),
121 Fraction::FourTwelfths => "33.333333%".to_string(),
122 Fraction::FiveTwelfths => "41.666667%".to_string(),
123 Fraction::SixTwelfths => "50%".to_string(),
124 Fraction::SevenTwelfths => "58.333333%".to_string(),
125 Fraction::EightTwelfths => "66.666667%".to_string(),
126 Fraction::NineTwelfths => "75%".to_string(),
127 Fraction::TenTwelfths => "83.333333%".to_string(),
128 Fraction::ElevenTwelfths => "91.666667%".to_string(),
129 }
130 }
131}
132
133#[cfg(test)]
134mod tests {
135 use super::*;
136
137 #[test]
138 fn test_fraction_display() {
139 assert_eq!(Fraction::Half.to_string(), "1/2");
140 assert_eq!(Fraction::Third.to_string(), "1/3");
141 assert_eq!(Fraction::TwoThirds.to_string(), "2/3");
142 }
143
144 #[test]
145 fn test_fraction_css_value() {
146 assert_eq!(Fraction::Half.to_css_value(), "50%");
147 assert_eq!(Fraction::Third.to_css_value(), "33.333333%");
148 assert_eq!(Fraction::TwoThirds.to_css_value(), "66.666667%");
149 }
150}