LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Never crash: rational tag lists show 16 values like every other list (#2579)
darthnut84951ff57967
diff --git a/crates/meta/src/tags.rs b/crates/meta/src/tags.rsindex 526348b15..8c8270f46 100644--- a/crates/meta/src/tags.rs+++ b/crates/meta/src/tags.rs@@ -156,7 +156,12 @@ fn rational(n: f64, d: f64) -> String {}fn list<T: ToString>(v: &[T]) -> String {- let shown: Vec<String> = v.iter().take(16).map(|x| x.to_string()).collect();+ list_with(v, T::to_string)+}++/// The first 16 values of `v` written by `f`, and how many there are when that's not all of them.+fn list_with<T>(v: &[T], f: impl Fn(&T) -> String) -> String {+ let shown: Vec<String> = v.iter().take(16).map(f).collect();if v.len() > 16 { format!("{} … ({} values)", shown.join(", "), v.len()) } else { shown.join(", ") }}@@ -249,8 +254,8 @@ fn value_text(group: &str, tag: u16, v: &Value) -> String {}Value::Rational(a) if tag == 0x829d && group != "GPS" && a.len() == 1 => format!("f/{}", rational(a[0].0 as f64, a[0].1 as f64)),Value::Rational(a) if tag == 0x920a && group != "GPS" && a.len() == 1 => format!("{} mm", rational(a[0].0 as f64, a[0].1 as f64)),- Value::Rational(a) => a.iter().map(|(n, d)| rational(*n as f64, *d as f64)).collect::<Vec<_>>().join(", "),- Value::SRational(a) => a.iter().map(|(n, d)| rational(*n as f64, *d as f64)).collect::<Vec<_>>().join(", "),+ Value::Rational(a) => list_with(a, |(n, d)| rational(*n as f64, *d as f64)),+ Value::SRational(a) => list_with(a, |(n, d)| rational(*n as f64, *d as f64)),Value::Byte(b) | Value::Undefined(b) => {// version tags and short printable values read as textlet printable = b.len() <= 64 && b.iter().all(|c| c.is_ascii_graphic() || *c == b' ' || *c == 0);@@ -387,6 +392,18 @@ mod tests {assert_eq!(rational(5.0, 2.0), "2.5");}+ /// Rational lists were joined whole, unlike every other list (#2579): a tag of millions of+ /// fractions became a string as long in the tag panel.+ #[test]+ fn rational_lists_show_sixteen_values_like_other_lists() {+ let first16 = (1..=16).map(|i| i.to_string()).collect::<Vec<_>>().join(", ");+ let many: Vec<(u32, u32)> = (1..=1000).map(|i| (i, 1)).collect();+ assert_eq!(value_text("EXIF", 0x9999, &Value::Rational(many)), format!("{first16} … (1000 values)"));+ let signed: Vec<(i32, i32)> = (1..=1000).map(|i| (-i, 1)).collect();+ assert!(value_text("EXIF", 0x9999, &Value::SRational(signed)).ends_with("-16 … (1000 values)"));+ assert_eq!(value_text("EXIF", 0x9999, &Value::Rational(vec![(1, 2), (3, 4)])), "0.5, 0.75");+ }+#[test]fn rows_from_written_exif() {let m = crate::Metadata {