zedbar 0.2.5

Pure Rust barcode and QR code scanning library supporting multiple formats
Documentation
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Type-safe configuration system for Zedbar decoders
//!
//! This module provides a compile-time verified configuration API that prevents
//! invalid configuration combinations. The type system ensures you can only
//! set configurations that are valid for each symbology.
//!
//! # Examples
//!
//! ## Valid Configuration
//!
//! ```
//! use zedbar::config::*;
//! use zedbar::DecoderConfig;
//!
//! let config = DecoderConfig::new()
//!     .enable(Ean13)
//!     .enable(Code39)
//!     .set_length_limits(Code39, 4, 20)   // ✓ Code39 supports variable length
//!     .position_tracking(true);
//! ```
//!
//! ## Type-Safe Compile Errors
//!
//! The following configurations will NOT compile:
//!
//! ```compile_fail
//! # use zedbar::config::*;
//! # use zedbar::DecoderConfig;
//! # let config = DecoderConfig::new();
//! // ❌ EAN-13 has fixed length, doesn't support length limits
//! config.set_length_limits(Ean13, 1, 20);
//! ```
//!
//! ```compile_fail
//! # use zedbar::config::*;
//! # use zedbar::DecoderConfig;
//! # let config = DecoderConfig::new();
//! // ❌ QR codes don't use length limits
//! config.set_length_limits(QrCode, 1, 100);
//! ```

use crate::SymbolType;
use std::collections::{HashMap, HashSet};

pub(crate) mod internal;
pub mod symbologies;

// Re-export symbology types for convenience
pub use symbologies::*;

// ============================================================================
// Configuration Value Types
// ============================================================================

/// Enable/disable flag for a symbology
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Enable(pub bool);

/// Add checksum validation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AddChecksum(pub bool);

/// Emit checksum in decoded data
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EmitChecksum(pub bool);

/// Binary mode (for 2D codes like QR)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Binary(pub bool);

/// Minimum symbol length
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MinLength(pub u32);

/// Maximum symbol length
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MaxLength(pub u32);

/// Edge detection uncertainty threshold
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Uncertainty(pub u32);

/// Position tracking enable/disable
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PositionTracking(pub bool);

/// Test inverted images
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TestInverted(pub bool);

/// Horizontal scan density
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct XDensity(pub u32);

/// Vertical scan density
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct YDensity(pub u32);

// ============================================================================
// Capability Traits
// ============================================================================

/// Marker trait for symbologies that can be enabled/disabled
pub trait SupportsEnable: Symbology {}

/// Marker trait for symbologies that support checksum configuration
pub trait SupportsChecksum: Symbology {}

/// Marker trait for symbologies that support variable length limits
pub trait SupportsLengthLimits: Symbology {}

/// Marker trait for symbologies that support uncertainty configuration
pub trait SupportsUncertainty: Symbology {}

/// Base trait that all symbology types must implement
pub trait Symbology: Sized {
    /// The corresponding SymbolType enum value
    const TYPE: SymbolType;

    /// Human-readable name
    const NAME: &'static str;
}

// ============================================================================
// User-Facing Configuration Builder
// ============================================================================

/// Type-safe configuration builder for zedbar decoders
///
/// This builder uses the type system to ensure only valid configurations
/// can be set for each symbology type.
///
/// # Example
/// ```no_run
/// use zedbar::config::*;
///
/// let config = DecoderConfig::new()
///     .enable(Ean13)
///     .enable(Code39)
///     .set_checksum(Code39, true, false)
///     .set_length_limits(Code39, 4, 20)
///     .position_tracking(true);
/// ```
#[derive(Debug, Clone)]
pub struct DecoderConfig {
    /// Which symbologies are enabled
    pub(crate) enabled: HashSet<SymbolType>,

    /// Checksum configuration: (add_check, emit_check)
    pub(crate) checksum_flags: HashMap<SymbolType, (bool, bool)>,

    /// Length limits: (min, max)
    pub(crate) length_limits: HashMap<SymbolType, (u32, u32)>,

    /// Uncertainty threshold per symbology
    pub(crate) uncertainty: HashMap<SymbolType, u32>,

    /// Global scanner configuration
    pub(crate) position_tracking: bool,
    pub(crate) test_inverted: bool,
    pub(crate) x_density: u32,
    pub(crate) y_density: u32,
    pub(crate) retry_undecoded_regions: bool,
}

impl Default for DecoderConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl DecoderConfig {
    /// Create a new configuration with sensible defaults
    ///
    /// By default:
    /// - EAN-13, EAN-8, I25, DataBar, Codabar, Code39, Code93, Code128, QR, SQ are enabled
    /// - UPC-A, UPC-E, ISBN-10, ISBN-13 can be emitted as variants (not separately enabled)
    /// - Position tracking is enabled
    /// - Scan density is 1x1
    /// - Inverted image testing is disabled
    pub fn new() -> Self {
        let mut config = Self {
            enabled: HashSet::new(),
            checksum_flags: HashMap::new(),
            length_limits: HashMap::new(),
            uncertainty: HashMap::new(),
            position_tracking: true,
            test_inverted: false,
            x_density: 1,
            y_density: 1,
            retry_undecoded_regions: false,
        };

        // Enable common symbologies by default
        config.enabled.insert(SymbolType::Ean13);
        config.enabled.insert(SymbolType::Ean8);
        // Note: UPC-A, UPC-E, ISBN-10, ISBN-13 are NOT enabled by default
        // They can be emitted as variants of EAN-13/EAN-8 when detected
        config.enabled.insert(SymbolType::I25);
        config.enabled.insert(SymbolType::Databar);
        config.enabled.insert(SymbolType::DatabarExp);
        config.enabled.insert(SymbolType::Codabar);
        config.enabled.insert(SymbolType::Code39);
        config.enabled.insert(SymbolType::Code93);
        config.enabled.insert(SymbolType::Code128);
        config.enabled.insert(SymbolType::QrCode);
        config.enabled.insert(SymbolType::SqCode);

        // Set default checksum behavior for EAN/UPC
        // EAN-13 and EAN-8 are enabled with emit_check
        for sym in [SymbolType::Ean13, SymbolType::Ean8] {
            config.checksum_flags.insert(sym, (false, true)); // don't add, do emit
        }

        // UPC-A, UPC-E, ISBN variants are not enabled but have emit_check
        // This allows them to be reported as variants when EAN decoding detects them
        for sym in [
            SymbolType::Upca,
            SymbolType::Upce,
            SymbolType::Isbn10,
            SymbolType::Isbn13,
        ] {
            config.checksum_flags.insert(sym, (false, true)); // don't add, do emit
        }

        // Set default length limits for variable-length symbologies
        config.length_limits.insert(SymbolType::I25, (6, 256));
        config.length_limits.insert(SymbolType::Codabar, (4, 256));
        config.length_limits.insert(SymbolType::Code39, (1, 256));

        // Set default uncertainty values
        config.uncertainty.insert(SymbolType::Codabar, 1);

        config
    }

    // ========================================================================
    // Per-Symbology Configuration
    // ========================================================================

    /// Enable a symbology
    pub fn enable<S: Symbology + SupportsEnable>(mut self, _: S) -> Self {
        self.enabled.insert(S::TYPE);
        self
    }

    /// Disable a symbology
    pub fn disable<S: Symbology + SupportsEnable>(mut self, _: S) -> Self {
        self.enabled.remove(&S::TYPE);
        self
    }

    /// Disable all symbologies
    ///
    /// Useful when you want to start with a clean slate and only enable
    /// specific symbologies.
    ///
    /// # Example
    /// ```
    /// use zedbar::config::*;
    /// use zedbar::DecoderConfig;
    ///
    /// let config = DecoderConfig::new()
    ///     .disable_all()
    ///     .enable(QrCode)
    ///     .enable(Code39);
    /// ```
    pub fn disable_all(mut self) -> Self {
        self.enabled.clear();
        self
    }

    /// Check if a symbology is enabled
    pub fn is_enabled(&self, sym: SymbolType) -> bool {
        self.enabled.contains(&sym)
    }

    /// Configure checksum behavior for a symbology
    ///
    /// # Arguments
    /// * `add_check` - Validate checksum during decoding
    /// * `emit_check` - Include checksum digit in decoded data
    pub fn set_checksum<S: Symbology + SupportsChecksum>(
        mut self,
        _: S,
        add_check: bool,
        emit_check: bool,
    ) -> Self {
        self.checksum_flags.insert(S::TYPE, (add_check, emit_check));
        self
    }

    /// Set minimum and maximum length limits
    ///
    /// Only valid for variable-length symbologies like Code39, Code128, etc.
    pub fn set_length_limits<S: Symbology + SupportsLengthLimits>(
        mut self,
        _: S,
        min: u32,
        max: u32,
    ) -> Self {
        assert!(min <= max, "min length must be <= max length");
        assert!(max <= 256, "max length must be <= 256");
        self.length_limits.insert(S::TYPE, (min, max));
        self
    }

    /// Set uncertainty threshold for edge detection
    ///
    /// Higher values are more tolerant of poor quality images but may
    /// produce more false positives.
    pub fn set_uncertainty<S: Symbology + SupportsUncertainty>(
        mut self,
        _: S,
        threshold: u32,
    ) -> Self {
        self.uncertainty.insert(S::TYPE, threshold);
        self
    }

    // ========================================================================
    // Global Scanner Configuration
    // ========================================================================

    /// Enable or disable position tracking
    ///
    /// When enabled, the scanner records the pixel coordinates of each
    /// detected symbol.
    pub fn position_tracking(mut self, enabled: bool) -> Self {
        self.position_tracking = enabled;
        self
    }

    /// Enable or disable inverted image testing
    ///
    /// When enabled, if no symbols are found in the normal image, the
    /// scanner will try again with an inverted (negative) image.
    pub fn test_inverted(mut self, enabled: bool) -> Self {
        self.test_inverted = enabled;
        self
    }

    /// Set scan density for both axes
    ///
    /// Higher density means more scan lines, which improves detection
    /// but increases processing time. A value of 1 means scan every line.
    pub fn scan_density(mut self, x: u32, y: u32) -> Self {
        assert!(x > 0, "x density must be > 0");
        assert!(y > 0, "y density must be > 0");
        self.x_density = x;
        self.y_density = y;
        self
    }

    /// Automatically retry undecoded QR finder regions by cropping and
    /// upscaling them.
    ///
    /// When enabled, if the initial scan detects QR finder patterns but
    /// fails to decode the QR code (e.g. because it is too small), the
    /// scanner will crop each undecoded region with padding, upscale it
    /// at several resolutions, and re-scan. Decoded symbol coordinates
    /// are mapped back to the original image frame.
    ///
    /// Default: `false`.
    pub fn retry_undecoded_regions(mut self, enabled: bool) -> Self {
        self.retry_undecoded_regions = enabled;
        self
    }

    /// Set horizontal scan density
    pub fn x_density(mut self, density: u32) -> Self {
        assert!(density > 0, "density must be > 0");
        self.x_density = density;
        self
    }

    /// Set vertical scan density
    pub fn y_density(mut self, density: u32) -> Self {
        assert!(density > 0, "density must be > 0");
        self.y_density = density;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = DecoderConfig::new();
        assert!(config.is_enabled(SymbolType::Ean13));
        assert!(config.is_enabled(SymbolType::Code39));
        assert!(config.position_tracking);
        assert!(!config.test_inverted);
        assert_eq!(config.x_density, 1);
        assert_eq!(config.y_density, 1);
    }

    #[test]
    fn test_builder_pattern() {
        let config = DecoderConfig::new()
            .enable(Ean13)
            .disable(Code39)
            .set_checksum(Code39, true, false)
            .position_tracking(false)
            .scan_density(2, 2);

        assert!(config.is_enabled(SymbolType::Ean13));
        assert!(!config.is_enabled(SymbolType::Code39));
        assert!(!config.position_tracking);
        assert_eq!(config.x_density, 2);
        assert_eq!(config.y_density, 2);
    }

    #[test]
    fn test_type_safe_length_limits() {
        // Variable-length symbologies can have length limits
        let config = DecoderConfig::new()
            .set_length_limits(Code39, 5, 20)
            .set_length_limits(Code128, 1, 50)
            .set_length_limits(I25, 6, 30);

        assert_eq!(
            config.length_limits.get(&SymbolType::Code39),
            Some(&(5, 20))
        );
        assert_eq!(
            config.length_limits.get(&SymbolType::Code128),
            Some(&(1, 50))
        );
        assert_eq!(config.length_limits.get(&SymbolType::I25), Some(&(6, 30)));
    }

    #[test]
    fn test_checksum_configuration() {
        let config = DecoderConfig::new()
            .set_checksum(Codabar, true, false) // validate but don't emit
            .set_checksum(Ean13, false, true); // don't validate, do emit

        assert_eq!(
            config.checksum_flags.get(&SymbolType::Codabar),
            Some(&(true, false))
        );
        assert_eq!(
            config.checksum_flags.get(&SymbolType::Ean13),
            Some(&(false, true))
        );
    }

    #[test]
    fn test_uncertainty_configuration() {
        let config = DecoderConfig::new()
            .set_uncertainty(Code39, 2)
            .set_uncertainty(QrCode, 0)
            .set_uncertainty(Codabar, 1);

        assert_eq!(config.uncertainty.get(&SymbolType::Code39), Some(&2));
        assert_eq!(config.uncertainty.get(&SymbolType::QrCode), Some(&0));
        assert_eq!(config.uncertainty.get(&SymbolType::Codabar), Some(&1));
    }

    #[test]
    fn test_config_to_state_conversion() {
        let config = DecoderConfig::new()
            .enable(Ean13)
            .set_checksum(Ean13, false, true)
            .position_tracking(true)
            .scan_density(2, 3);

        let state: internal::DecoderState = (&config).into();

        // Verify EAN is enabled
        assert!(state.is_enabled(SymbolType::Ean13));
        assert!(state.ean_enabled());

        // Verify checksum config
        let ean13_config = state.get(SymbolType::Ean13).unwrap();
        assert!(!ean13_config.checksum.add_check);
        assert!(ean13_config.checksum.emit_check);

        // Verify scanner config
        assert_eq!(state.scanner.x_density, 2);
        assert_eq!(state.scanner.y_density, 3);
        assert!(state.scanner.position_tracking);
    }
}