Cotton code
44 lines · 1.9 KB · RustRaw
//! One photo, several files (coordinator's rule E): a RAW+JPEG pair, or a//! photo and its edited copy, are **one item with parts, never rivals**. The//! part is never grouped, offered or binned on its own; an act on the main//! item carries its parts.//!//! On the iPhone a RAW+JPEG pair and an edit are already one `PHAsset`, so//! nothing pairs there. On Android each is its own MediaStore row: the same//! file name stem taken within the same second, one RAW (`.dng`, `.arw`,//! `.cr2`, `.cr3`, `.nef`, `.orf`, `.raf`, `.rw2`, `.raw`) beside a JPEG or//! HEIC; or a stem ending `-edited` / `_edited` beside its unedited stem.//! The JPEG or HEIC (what he sees) is the main item; the edit is the main//! item over its unedited original.use super::Item;const RAW: [&str; 9] = ["dng", "arw", "cr2", "cr3", "nef", "orf", "raf", "rw2", "raw"];const SHOWN: [&str; 4] = ["jpg", "jpeg", "heic", "heif"];fn stem_ext(item: &Item) -> Option<(String, String)> {    let path = item.path.as_deref()?;    let name = path.rsplit('/').next()?.to_ascii_lowercase();    let (stem, ext) = name.rsplit_once('.')?;    Some((stem.to_string(), ext.to_string()))}/// Whether `part` is a part of `main`: `Some(true)` when `part` belongs to/// `main`, judged both ways by the caller.pub fn is_part_of(part: &Item, main: &Item) -> bool {    if part.id == main.id || part.taken_at_ms.div_euclid(1000) != main.taken_at_ms.div_euclid(1000) {        return false;    }    let (Some((ps, pe)), Some((ms, me))) = (stem_ext(part), stem_ext(main)) else { return false };    // RAW beside the JPEG/HEIC of the same stem    if ps == ms && RAW.contains(&pe.as_str()) && SHOWN.contains(&me.as_str()) {        return true;    }    // the unedited original beside its edit    for suffix in ["-edited", "_edited"] {        if ms.strip_suffix(suffix) == Some(ps.as_str()) && SHOWN.contains(&pe.as_str()) {            return true;        }    }    false}