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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Encoding modes for a QR code.
use regex::Regex;
use bitvec::*;
use lazy_static::lazy_static;
/// Encoding modes.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Mode {
/// Numeric mode can encode numbers, 0 to 9.
Numeric = 0,
/// Alphanumeric mode can encode numbers, A-Z (only uppercase),
/// the characters $%*+-./: and space.
Alphanumeric,
/// Byte mode supports the ISO-8859-1 character set.
/// It is possible to use this mode to encode unicode,
/// but it depends heavily on the reader if it's supported or not.
Byte,
//ECI, // specifies the character set directly (like UTF-8)
//Kanji, // more efficient storage than ECI
}
impl Mode {
/// Create Mode from string, decide from content.
pub fn from_str(s: &str) -> Mode {
if Mode::in_numeric(s) {
Mode::Numeric
} else if Mode::in_alphanumeric(s) {
Mode::Alphanumeric
} else if Mode::in_byte(s) {
Mode::Byte
} else {
// Should never happen.
panic!("Unsupported mode for string {}", s);
}
}
/// Is this a valid mode for a string?
pub fn matches(&self, s: &str) -> bool {
match self {
Mode::Numeric => Mode::in_numeric(s),
Mode::Alphanumeric => Mode::in_alphanumeric(s),
Mode::Byte => Mode::in_byte(s),
}
}
/// BitVec representation.
pub fn to_bitvec(&self) -> BitVec {
match self {
Mode::Numeric => bitvec![0, 0, 0, 1],
Mode::Alphanumeric => bitvec![0, 0, 1, 0],
Mode::Byte => bitvec![0, 1, 0, 0],
}
}
/// Returns true if contents can be represented by the numeric mode.
pub fn in_numeric(s: &str) -> bool {
NUMERIC_RX.is_match(s)
}
/// Returns true if contents can be represented by the alphanumeric mode.
pub fn in_alphanumeric(s: &str) -> bool {
ALPHANUMERIC_RX.is_match(s)
}
/// Returns true if contents can be represented by the byte mode.
pub fn in_byte(_s: &str) -> bool {
true
}
}
lazy_static! {
static ref NUMERIC_RX: Regex = Regex::new(r"^[0-9]+$").unwrap();
static ref ALPHANUMERIC_RX: Regex = Regex::new(r"^[0-9A-Z$%*+-./: ]+$").unwrap();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn creation() {
assert_eq!(Mode::from_str("0123456789"),
Mode::Numeric);
assert_eq!(Mode::from_str("ABCXYZ 0123456789$%*+-./:"),
Mode::Alphanumeric);
assert_eq!(Mode::from_str("☃"),
Mode::Byte);
}
#[test]
fn internal() {
assert_eq!(Mode::Numeric.to_bitvec(), bitvec![0, 0, 0, 1]);
}
}