1
2#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
4pub enum BlockElements {
5 UpperHalfBlock,
7 LowerOneEighthBlock,
9 LowerOneQuarterBlock,
11 LowerThreeEighthsBlock,
13 LowerHalfBlock,
15 LowerFiveEighthsBlock,
17 LowerThreeQuartersBlock,
19 LowerSevenEighthsBlock,
21 FullBlock,
23 LeftSevenEighthsBlock,
25 LeftThreeQuartersBlock,
27 LeftFiveEighthsBlock,
29 LeftHalfBlock,
31 LeftThreeEighthsBlock,
33 LeftOneQuarterBlock,
35 LeftOneEighthBlock,
37 RightHalfBlock,
39 LightShade,
41 MediumShade,
43 DarkShade,
45 UpperOneEighthBlock,
47 RightOneEighthBlock,
49 QuadrantLowerLeft,
51 QuadrantLowerRight,
53 QuadrantUpperLeft,
55 QuadrantUpperLeftAndLowerLeftAndLowerRight,
57 QuadrantUpperLeftAndLowerRight,
59 QuadrantUpperLeftAndUpperRightAndLowerLeft,
61 QuadrantUpperLeftAndUpperRightAndLowerRight,
63 QuadrantUpperRight,
65 QuadrantUpperRightAndLowerLeft,
67}
68
69impl Into<char> for BlockElements {
70 fn into(self) -> char {
71 match self {
72 BlockElements::UpperHalfBlock => '▀',
73 BlockElements::LowerOneEighthBlock => '▁',
74 BlockElements::LowerOneQuarterBlock => '▂',
75 BlockElements::LowerThreeEighthsBlock => '▃',
76 BlockElements::LowerHalfBlock => '▄',
77 BlockElements::LowerFiveEighthsBlock => '▅',
78 BlockElements::LowerThreeQuartersBlock => '▆',
79 BlockElements::LowerSevenEighthsBlock => '▇',
80 BlockElements::FullBlock => '█',
81 BlockElements::LeftSevenEighthsBlock => '▉',
82 BlockElements::LeftThreeQuartersBlock => '▊',
83 BlockElements::LeftFiveEighthsBlock => '▋',
84 BlockElements::LeftHalfBlock => '▌',
85 BlockElements::LeftThreeEighthsBlock => '▍',
86 BlockElements::LeftOneQuarterBlock => '▎',
87 BlockElements::LeftOneEighthBlock => '▏',
88 BlockElements::RightHalfBlock => '▐',
89 BlockElements::LightShade => '░',
90 BlockElements::MediumShade => '▒',
91 BlockElements::DarkShade => '▓',
92 BlockElements::UpperOneEighthBlock => '▔',
93 BlockElements::RightOneEighthBlock => '▕',
94 BlockElements::QuadrantLowerLeft => '▖',
95 BlockElements::QuadrantLowerRight => '▗',
96 BlockElements::QuadrantUpperLeft => '▘',
97 BlockElements::QuadrantUpperLeftAndLowerLeftAndLowerRight => '▙',
98 BlockElements::QuadrantUpperLeftAndLowerRight => '▚',
99 BlockElements::QuadrantUpperLeftAndUpperRightAndLowerLeft => '▛',
100 BlockElements::QuadrantUpperLeftAndUpperRightAndLowerRight => '▜',
101 BlockElements::QuadrantUpperRight => '▝',
102 BlockElements::QuadrantUpperRightAndLowerLeft => '▞',
103 }
104 }
105}
106
107impl std::convert::TryFrom<char> for BlockElements {
108 type Error = ();
109 fn try_from(c: char) -> Result<Self, Self::Error> {
110 match c {
111 '▀' => Ok(BlockElements::UpperHalfBlock),
112 '▁' => Ok(BlockElements::LowerOneEighthBlock),
113 '▂' => Ok(BlockElements::LowerOneQuarterBlock),
114 '▃' => Ok(BlockElements::LowerThreeEighthsBlock),
115 '▄' => Ok(BlockElements::LowerHalfBlock),
116 '▅' => Ok(BlockElements::LowerFiveEighthsBlock),
117 '▆' => Ok(BlockElements::LowerThreeQuartersBlock),
118 '▇' => Ok(BlockElements::LowerSevenEighthsBlock),
119 '█' => Ok(BlockElements::FullBlock),
120 '▉' => Ok(BlockElements::LeftSevenEighthsBlock),
121 '▊' => Ok(BlockElements::LeftThreeQuartersBlock),
122 '▋' => Ok(BlockElements::LeftFiveEighthsBlock),
123 '▌' => Ok(BlockElements::LeftHalfBlock),
124 '▍' => Ok(BlockElements::LeftThreeEighthsBlock),
125 '▎' => Ok(BlockElements::LeftOneQuarterBlock),
126 '▏' => Ok(BlockElements::LeftOneEighthBlock),
127 '▐' => Ok(BlockElements::RightHalfBlock),
128 '░' => Ok(BlockElements::LightShade),
129 '▒' => Ok(BlockElements::MediumShade),
130 '▓' => Ok(BlockElements::DarkShade),
131 '▔' => Ok(BlockElements::UpperOneEighthBlock),
132 '▕' => Ok(BlockElements::RightOneEighthBlock),
133 '▖' => Ok(BlockElements::QuadrantLowerLeft),
134 '▗' => Ok(BlockElements::QuadrantLowerRight),
135 '▘' => Ok(BlockElements::QuadrantUpperLeft),
136 '▙' => Ok(BlockElements::QuadrantUpperLeftAndLowerLeftAndLowerRight),
137 '▚' => Ok(BlockElements::QuadrantUpperLeftAndLowerRight),
138 '▛' => Ok(BlockElements::QuadrantUpperLeftAndUpperRightAndLowerLeft),
139 '▜' => Ok(BlockElements::QuadrantUpperLeftAndUpperRightAndLowerRight),
140 '▝' => Ok(BlockElements::QuadrantUpperRight),
141 '▞' => Ok(BlockElements::QuadrantUpperRightAndLowerLeft),
142 _ => Err(()),
143 }
144 }
145}
146
147impl Into<u32> for BlockElements {
148 fn into(self) -> u32 {
149 let c: char = self.into();
150 let hex = c
151 .escape_unicode()
152 .to_string()
153 .replace("\\u{", "")
154 .replace("}", "");
155 u32::from_str_radix(&hex, 16).unwrap()
156 }
157}
158
159impl std::convert::TryFrom<u32> for BlockElements {
160 type Error = ();
161 fn try_from(u: u32) -> Result<Self, Self::Error> {
162 if let Ok(c) = char::try_from(u) {
163 Self::try_from(c)
164 } else {
165 Err(())
166 }
167 }
168}
169
170impl Iterator for BlockElements {
171 type Item = Self;
172 fn next(&mut self) -> Option<Self> {
173 let index: u32 = (*self).into();
174 use std::convert::TryFrom;
175 Self::try_from(index + 1).ok()
176 }
177}
178
179impl BlockElements {
180 pub fn new() -> Self {
182 BlockElements::UpperHalfBlock
183 }
184
185 pub fn name(&self) -> String {
187 let s = std::format!("BlockElements{:#?}", self);
188 string_morph::to_sentence_case(&s)
189 }
190}