Skip to content

Current Context: Complexity Reduction Refactoring

Session Objective

Reduce code duplication in exiftool-rs makernote parsers from ~100-900% to 0% using TagRegistry pattern, to achieve Codacy's <10% complex files goal.

Background

  • Original Problem: 23% of files (125/538) are complex, goal is <10% (54 files)
  • First Refactoring Attempt: Used const_decoder! macros - achieved partial success
    • 5 files: 0% duplication (operations, format_detector, icc_parser, pdf/info_parser, sony)
    • 5 files: Still high duplication (samsung 906%, gopro 136%, qualcomm 153%, photoshop 108%, dji 113%)
  • Root Cause: const_decoder! reduced decoder definitions but not repetitive match arm usage
  • Solution: Use TagRegistry pattern (table-driven approach) to eliminate match arms entirely

Current Refactoring Status

Completed (3/5 files)

  1. samsung.rs: F(0) grade, 906% dup → 0% dup ✅

    • Created SAMSUNG_TAGS registry with 16 tags
    • Replaced 124-line parse_entry with 11-line registry lookup
    • Added decode_binary_onoff() helper
    • 13/13 tests passing
  2. gopro.rs: C(68) grade, 136% dup → 0% dup ✅

    • Created GOPRO_TAGS registry with 37 tags
    • Replaced 109-line match with 3-line registry lookup
    • 13/13 tests passing
  3. qualcomm.rs: C(53) grade, 153% dup → 0% dup ✅

    • Created QUALCOMM_TAGS registry with 15 tags
    • Replaced 141-line parse_entry with 34-line registry lookup
    • Added 3 helper functions
    • 11/11 tests passing

Pending (2/5 files)

  1. photoshop.rs: B(71) grade, 108% dup → target 0%

    • Status: Modified (git shows changes)
    • Subagent reported permission issues mid-refactoring
    • Need to verify completion status
  2. dji.rs: C(63) grade, 113% dup → target 0%

    • Status: Not modified (git shows no changes)
    • Subagent blocked by permissions
    • Not started

TagRegistry Pattern

Architecture:

rust
use once_cell::sync::Lazy;
use super::shared::tag_registry::TagRegistry;

// Centralized registry (single source of truth)
static CAMERA_TAGS: Lazy<TagRegistry> = Lazy::new(|| {
    TagRegistry::with_capacity(20)
        .register_simple_i16(TAG_ID, "TagName", &DECODER)
        .register_i16(TAG_ID, "TagName", custom_function)
        .register_raw(TAG_ID, "TagName")
});

// Simplified parse method (eliminates repetitive match arms)
fn parse_entry(...) {
    if let Some(tag_name) = CAMERA_TAGS.get_tag_name(entry.tag_id) {
        let decoded = CAMERA_TAGS.decode_i16(entry.tag_id, value);
        tags.insert(format!("Prefix:{}", tag_name), decoded);
    }
}

Benefits:

  • O(1) HashMap lookup vs O(n) match scanning
  • Adding tags: 1 line vs 5+ lines
  • Zero code duplication in parse methods
  • Type-safe decoder registration
  • Self-documenting structure

Dependencies Added

  • once_cell = "1.19" in Cargo.toml (for Lazy static initialization)

Shared Utilities Available

  • src/parsers/tiff/makernotes/shared/tag_registry.rs - Registry system
  • src/parsers/tiff/makernotes/shared/generic_decoders.rs - Pre-built decoders (ON_OFF, YES_NO, etc.)
  • src/parsers/tiff/makernotes/shared/decoder_macros.rs - const_decoder! macro

Next Steps

  1. ✅ Verify photoshop.rs refactoring completion status
  2. ⏸️ Complete dji.rs refactoring
  3. ⏸️ Run full test suite to verify all changes
  4. ⏸️ Commit changes with descriptive message
  5. ⏸️ Push to GitHub to trigger Codacy analysis
  6. ⏸️ Verify Codacy shows duplication reduction

Expected Codacy Results

After completion:

  • samsung.rs: F(0) → A/B grade, 0% duplication
  • gopro.rs: C(68) → A/B grade, 0% duplication
  • qualcomm.rs: C(53) → A/B grade, 0% duplication
  • photoshop.rs: B(71) → A grade, 0% duplication
  • dji.rs: C(63) → A/B grade, 0% duplication

Repository-level impact: ~5 files improved out of 125 complex files = marginal improvement in 23% metric, but establishes pattern for future refactoring.

Technical Notes

  • Permission issues encountered during refactoring (macOS SIP/extended attributes)
  • Resolved by user running sudo xattr -rc and sudo chmod -R u+rw
  • All subagents used clean-code-writer type with explicit TagRegistry instructions
  • Tests preserved: All functionality maintained, zero behavioral changes

Released under the GPL-3.0 License.