# Codebase Consolidation / Refactoring TODOs

Here are a few noticeable areas where the codebase could be heavily consolidated through refactoring without changing the underlying logic:

1. **`src/maps.rs` — Inverse Hash Mapping (Lines ~246–269)**: 
The logic for creating the index mappings of `ccid_to_xvuid` and `cid_to_xvuid` inside `rebuild_memcache_maps` is exactly duplicated. In both cases, you convert `cid` or `ccid` to a hex string, build a two-element FFI array `[xvuid_bytes, index]`, check if the key exists in the KV map, and either append to the existing wrapper array or allocate a new wrapper array and put it in the map. This 12-line chunk is copy-pasted back to back and could be cleanly consolidated into an inline lambda closure (e.g. `let mut insert_hash_map = |target_map, ptr, len| { ... }`).

2. **`src/maps.rs` — Boilerplate Lookups (`lookup_latest_target`, `lookup_snapshot_offset`, `lookup_title`)**:
All of these accessor functions repeatedly perform the exact same generic memory-lifecycle routine: 
    *   Load string into `map_file_path`
    *   Call `decode_dtob_file` and return `None` if it fails
    *   Wrap an `unsafe` block calling `ffi_kvset_get`
    *   Cleanly free the memory map tree at the end via `ffi_dtob_free(root)`
This entire structural pattern could be condensed into a single higher-order function or generic like `fn lookup_map<T>(path: &Path, map_name: &str, key: &str, extract_closure: impl Fn(*mut DtobValue) -> Option<T>) -> Option<T>`.

3. **`src/netdoc.rs` — KV Initialization Block**:
In `write_netdoc_dif`, you individually allocate FFI values and cast the null-terminated strings sequentially (`ffi_raw_new`, `ffi_kvset_put(meta, b"prop\0"...`). This long wall of allocations could be significantly shrunk by creating a tiny declarative Rust macro like `put_kv!(map, "title", value_ptr)` that automatically handles the null termination casts and raw pointers under the hood to clean up the visual nesting.
