| 1 | /// Module is a single pixel in the QR code. |
| 2 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 3 | #[repr(u8)] |
| 4 | pub enum ModuleType { |
| 5 | /// The module is part of the data (Encoded data) |
| 6 | Data = 0 << 1, |
| 7 | /// The module is part of the finder pattern (bigger cubes) |
| 8 | FinderPattern = 1 << 1, |
| 9 | /// The module is part of the alignment pattern (smaller cubes) |
| 10 | Alignment = 2 << 1, |
| 11 | /// The module is part of the timing pattern (Line between finder patterns) |
| 12 | Timing = 3 << 1, |
| 13 | /// The module is part of the format information |
| 14 | Format = 4 << 1, |
| 15 | /// The module is part of the version information |
| 16 | Version = 5 << 1, |
| 17 | /// Dark module |
| 18 | DarkModule = 6 << 1, |
| 19 | /// Space between finder patterns |
| 20 | Empty = 7 << 1, |
| 21 | } |
| 22 | |
| 23 | impl From<u8> for ModuleType { |
| 24 | fn from(value: u8) -> Self { |
| 25 | match value { |
| 26 | 0 => ModuleType::Data, |
| 27 | 1 => ModuleType::FinderPattern, |
| 28 | 2 => ModuleType::Alignment, |
| 29 | 3 => ModuleType::Timing, |
| 30 | 4 => ModuleType::Format, |
| 31 | 5 => ModuleType::Version, |
| 32 | 6 => ModuleType::DarkModule, |
| 33 | 7 => ModuleType::Empty, |
| 34 | _ => unreachable!(), |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// Module is a single pixel in the QR code. |
| 40 | /// Module uses u8 to store value and type. |
| 41 | #[derive(Copy, Clone, Debug)] |
| 42 | pub struct Module(pub u8); |
| 43 | |
| 44 | impl Module { |
| 45 | /// Represents a dark module, which is a black pixel. |
| 46 | pub const DARK: bool = true; |
| 47 | /// Represents a light module, which is a white pixel. |
| 48 | pub const LIGHT: bool = false; |
| 49 | |
| 50 | /// Creates a new module with the given type and value. |
| 51 | #[must_use] |
| 52 | pub const fn new(value: bool, module_type: ModuleType) -> Self { |
| 53 | let value = value as u8; |
| 54 | Module(value | (module_type as u8)) |
| 55 | } |
| 56 | |
| 57 | /// Creates a new module with the given value with type data. |
| 58 | #[must_use] |
| 59 | pub const fn data(value: bool) -> Self { |
| 60 | Module::new(value, ModuleType::Data) |
| 61 | } |
| 62 | |
| 63 | /// Creates a new module with the given value with type finder pattern. |
| 64 | #[must_use] |
| 65 | pub const fn finder_pattern(value: bool) -> Self { |
| 66 | Module::new(value, ModuleType::FinderPattern) |
| 67 | } |
| 68 | |
| 69 | /// Creates a new module with the given value with type alignment. |
| 70 | #[must_use] |
| 71 | pub const fn alignment(value: bool) -> Self { |
| 72 | Module::new(value, ModuleType::Alignment) |
| 73 | } |
| 74 | |
| 75 | /// Creates a new module with the given value with type timing. |
| 76 | #[must_use] |
| 77 | pub const fn timing(value: bool) -> Self { |
| 78 | Module::new(value, ModuleType::Timing) |
| 79 | } |
| 80 | |
| 81 | /// Creates a new module with the given value with type format. |
| 82 | #[must_use] |
| 83 | pub const fn format(value: bool) -> Self { |
| 84 | Module::new(value, ModuleType::Format) |
| 85 | } |
| 86 | |
| 87 | /// Creates a new module with the given value with type version. |
| 88 | #[must_use] |
| 89 | pub const fn version(value: bool) -> Self { |
| 90 | Module::new(value, ModuleType::Version) |
| 91 | } |
| 92 | |
| 93 | /// Creates a new module with the given value with type dark module. |
| 94 | #[must_use] |
| 95 | pub const fn dark(value: bool) -> Self { |
| 96 | Module::new(value, ModuleType::DarkModule) |
| 97 | } |
| 98 | |
| 99 | /// Creates a new module with the given value with type empty. |
| 100 | #[must_use] |
| 101 | pub const fn empty(value: bool) -> Self { |
| 102 | Module::new(value, ModuleType::Empty) |
| 103 | } |
| 104 | |
| 105 | /// Returns the boolean value of the module. |
| 106 | #[must_use] |
| 107 | pub const fn value(self) -> bool { |
| 108 | self.0 & 1 == 1 |
| 109 | } |
| 110 | |
| 111 | /// Returns the type of the module. |
| 112 | #[must_use] |
| 113 | pub fn module_type(self) -> ModuleType { |
| 114 | ModuleType::from(self.0 >> 1) |
| 115 | } |
| 116 | |
| 117 | /// Sets the boolean value of the module. |
| 118 | pub fn set(&mut self, value: bool) { |
| 119 | self.0 = if value { self.0 | 1 } else { self.0 & !1 }; |
| 120 | } |
| 121 | |
| 122 | /// Toggles the boolean value of the module. |
| 123 | pub fn toggle(&mut self) { |
| 124 | self.0 ^= 1; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl From<bool> for Module { |
| 129 | fn from(value: bool) -> Self { |
| 130 | Module::empty(value) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl PartialEq<bool> for Module { |
| 135 | fn eq(&self, other: &bool) -> bool { |
| 136 | self.value() == *other |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | impl PartialEq<Self> for Module { |
| 141 | fn eq(&self, other: &Self) -> bool { |
| 142 | self.0 == other.0 |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | impl Eq for Module {} |
| 147 | |
| 148 | #[cfg(test)] |
| 149 | mod test { |
| 150 | use super::*; |
| 151 | |
| 152 | #[test] |
| 153 | fn byte_size() { |
| 154 | assert_eq!(std::mem::size_of::<Module>(), 1); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn data() { |
| 159 | let module = Module::data(Module::LIGHT); |
| 160 | assert_eq!(module.module_type(), ModuleType::Data); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn finder_pattern() { |
| 165 | let module = Module::finder_pattern(Module::LIGHT); |
| 166 | assert_eq!(module.module_type(), ModuleType::FinderPattern); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn alignment() { |
| 171 | let module = Module::alignment(Module::LIGHT); |
| 172 | assert_eq!(module.module_type(), ModuleType::Alignment); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn timing() { |
| 177 | let module = Module::timing(Module::LIGHT); |
| 178 | assert_eq!(module.module_type(), ModuleType::Timing); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn format() { |
| 183 | let module = Module::format(Module::LIGHT); |
| 184 | assert_eq!(module.module_type(), ModuleType::Format); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn version() { |
| 189 | let module = Module::version(Module::LIGHT); |
| 190 | assert_eq!(module.module_type(), ModuleType::Version); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn dark() { |
| 195 | let module = Module::dark(Module::LIGHT); |
| 196 | assert_eq!(module.module_type(), ModuleType::DarkModule); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn value_light() { |
| 201 | let module = Module::data(Module::LIGHT); |
| 202 | assert_eq!(module.value(), Module::LIGHT); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn value_dark() { |
| 207 | let module = Module::data(Module::DARK); |
| 208 | assert_eq!(module.value(), Module::DARK); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn set() { |
| 213 | let mut module = Module::data(Module::LIGHT); |
| 214 | module.set(Module::DARK); |
| 215 | assert_eq!(module.value(), Module::DARK); |
| 216 | } |
| 217 | } |