1use std::fmt::{Display, Formatter, Result};
2
3#[derive(Clone, Copy, Debug)]
7pub enum Unit {
8 Centimeter(f32),
10 Inch(f32),
12 None(f32),
14 Millimeter(f32),
16 Pica(f32),
18 Pixel(f32),
20 Point(f32),
22 QuarterMillimeter(f32),
24}
25
26impl Unit {
27 pub fn value(self) -> f32 {
29 match self {
30 Self::Centimeter(value) => value,
31 Self::Inch(value) => value,
32 Self::None(value) => value,
33 Self::Millimeter(value) => value,
34 Self::Pica(value) => value,
35 Self::Pixel(value) => value,
36 Self::Point(value) => value,
37 Self::QuarterMillimeter(value) => value,
38 }
39 }
40
41 pub fn symbol(self) -> &'static str {
43 match self {
44 Self::Centimeter(_) => "cm",
45 Self::Inch(_) => "in",
46 Self::None(_) => "",
47 Self::Millimeter(_) => "mm",
48 Self::Pica(_) => "pc",
49 Self::Pixel(_) => "px",
50 Self::Point(_) => "pt",
51 Self::QuarterMillimeter(_) => "Q",
52 }
53 }
54
55 pub fn scale(self, factor: f32) -> Self {
56 match self {
57 Self::Centimeter(value) => Self::Centimeter(value * factor),
58 Self::Inch(value) => Self::Inch(value * factor),
59 Self::None(value) => Self::None(value * factor),
60 Self::Millimeter(value) => Self::Millimeter(value * factor),
61 Self::Pica(value) => Self::Pica(value * factor),
62 Self::Pixel(value) => Self::Pixel(value * factor),
63 Self::Point(value) => Self::Point(value * factor),
64 Self::QuarterMillimeter(value) => Self::QuarterMillimeter(value * factor),
65 }
66 }
67}
68
69impl Display for Unit {
70 fn fmt(&self, fmt: &mut Formatter) -> Result {
71 write!(
72 fmt,
73 "{value}{symbol}",
74 value = self.value(),
75 symbol = self.symbol()
76 )
77 }
78}