Skip to main content

qrcode_rust_shared/
qr_8bit_byte.rs

1//! QR Code 8-bit Byte Mode
2
3pub struct QR8bitByte {
4    pub data: String,
5}
6
7impl QR8bitByte {
8    pub fn new(data: &str) -> Self {
9        QR8bitByte {
10            data: data.to_string(),
11        }
12    }
13
14    pub fn get_length(&self) -> usize {
15        self.data.len()
16    }
17
18    pub fn write(&self, buffer: &mut crate::qr_bit_buffer::BitBuffer) {
19        for byte in self.data.bytes() {
20            buffer.put(byte as i32, 8);
21        }
22    }
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use crate::qr_bit_buffer::BitBuffer;
29
30    #[test]
31    fn test_qr8bit_byte() {
32        let data = QR8bitByte::new("Hi");
33        assert_eq!(data.get_length(), 2);
34
35        let mut buf = BitBuffer::new();
36        data.write(&mut buf);
37        assert_eq!(buf.length, 16); // 2 bytes * 8 bits
38    }
39}