1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use super::codes::{Code, Label, BLANK, LETTER_MASK, UNINIT};
use super::{Item, Tile};
use crate::error::Error;
use std::convert::TryFrom;
use std::num::NonZeroU8;
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Letter(pub(super) NonZeroU8);
impl Default for Letter {
fn default() -> Self {
Self::new(UNINIT)
}
}
impl Item for Letter {
fn code(&self) -> Code {
self.0.get()
}
}
impl Letter {
fn new(code: Code) -> Letter {
let label = NonZeroU8::new(code).expect("label can't be 0");
Letter(label)
}
pub fn from_tile(tile: Tile) -> Letter {
Letter::new(tile.label())
}
pub fn blank() -> Letter {
Letter(NonZeroU8::new(BLANK).unwrap())
}
pub fn is_blank(&self) -> bool {
self.code() == BLANK
}
pub fn label(&self) -> Label {
self.code() & LETTER_MASK
}
}
impl TryFrom<Code> for Letter {
type Error = Error;
fn try_from(code: Code) -> Result<Self, Self::Error> {
match code {
1..=31 | BLANK => Ok(Self::new(code)),
_ => Err(Self::Error::InvalidLetterCode(code)),
}
}
}
impl Into<Code> for Letter {
fn into(self) -> Code {
self.0.get()
}
}