pakem/core/hardware/
scanner.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct BarcodeScanEvent {
5 pub barcode: String,
6 pub format: Option<String>,
7 pub timestamp_ms: u64,
8}
9
10impl BarcodeScanEvent {
11 pub fn new(barcode: impl Into<String>) -> Self {
12 let now = std::time::SystemTime::now()
13 .duration_since(std::time::UNIX_EPOCH)
14 .unwrap_or_default()
15 .as_millis() as u64;
16
17 Self {
18 barcode: barcode.into(),
19 format: None,
20 timestamp_ms: now,
21 }
22 }
23
24 pub fn with_format(mut self, format: impl Into<String>) -> Self {
25 self.format = Some(format.into());
26 self
27 }
28
29 pub fn is_valid_ean13(&self) -> bool {
31 let digits: Vec<u32> = self.barcode.chars().filter_map(|c| c.to_digit(10)).collect();
32 if digits.len() != 13 {
33 return false;
34 }
35 let sum: u32 = digits[..12]
36 .iter()
37 .enumerate()
38 .map(|(i, &d)| if i % 2 == 0 { d } else { d * 3 })
39 .sum();
40 let check = (10 - (sum % 10)) % 10;
41 check == digits[12]
42 }
43}