From 8c91e9775d6d6c05b0335e50323484b8f57fdecc Mon Sep 17 00:00:00 2001 From: Eval Exec Date: Thu, 8 Oct 2026 01:53:56 +0800 Subject: [PATCH] refactor(protocol): derive image format name parsing --- .../src/image_diagnostic.rs | 20 ++------ .../image_diagnostic/tests/diagnostic_test.rs | 48 ++++++++++++++++--- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/crates/neomacs-display-protocol/src/image_diagnostic.rs b/crates/neomacs-display-protocol/src/image_diagnostic.rs index fe99fcaca5..4ef20f285d 100644 --- a/crates/neomacs-display-protocol/src/image_diagnostic.rs +++ b/crates/neomacs-display-protocol/src/image_diagnostic.rs @@ -29,7 +29,8 @@ use std::fmt::{self, Display, Formatter}; /// (`"Not a PNG file: `%s'"`, `"PNG error: %s"`), and the two do not always /// agree. The declared type is what matters, not the bytes — an image declared /// `png` whose bytes are a JPEG is a PNG the loader refused, and GNU says so. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, strum::EnumString)] +#[strum(serialize_all = "kebab-case")] pub enum ImageFormatName { Png, Jpeg, @@ -44,6 +45,7 @@ pub enum ImageFormatName { Postscript, NativeImage, /// A type this build has no table entry for, under the Lisp symbol's name. + #[strum(default)] Other(String), } @@ -51,21 +53,7 @@ impl ImageFormatName { /// Map a Lisp image type symbol (`png`, `native-image`) to GNU's spelling. #[must_use] pub fn from_lisp_type(name: &str) -> Self { - match name { - "png" => Self::Png, - "jpeg" => Self::Jpeg, - "gif" => Self::Gif, - "tiff" => Self::Tiff, - "xpm" => Self::Xpm, - "xbm" => Self::Xbm, - "pbm" => Self::Pbm, - "webp" => Self::Webp, - "svg" => Self::Svg, - "imagemagick" => Self::Imagemagick, - "postscript" => Self::Postscript, - "native-image" => Self::NativeImage, - other => Self::Other(other.to_owned()), - } + Self::from(name) } #[must_use] diff --git a/crates/neomacs-display-protocol/src/image_diagnostic/tests/diagnostic_test.rs b/crates/neomacs-display-protocol/src/image_diagnostic/tests/diagnostic_test.rs index b981ede503..29a1cef0e9 100644 --- a/crates/neomacs-display-protocol/src/image_diagnostic/tests/diagnostic_test.rs +++ b/crates/neomacs-display-protocol/src/image_diagnostic/tests/diagnostic_test.rs @@ -74,13 +74,47 @@ fn oversize_refusal_is_word_for_word_gnu() { /// name as a literal, and `native-image` is spelled with a dash. #[test] fn declared_types_map_to_gnus_spelling() { - assert_eq!(ImageFormatName::from_lisp_type("png").as_str(), "PNG"); - assert_eq!(ImageFormatName::from_lisp_type("jpeg").as_str(), "JPEG"); - assert_eq!( - ImageFormatName::from_lisp_type("native-image").as_str(), - "NATIVE-IMAGE" - ); - assert_eq!(ImageFormatName::from_lisp_type("bmp").as_str(), "bmp"); + for (symbol, format, diagnostic_name) in [ + ("png", ImageFormatName::Png, "PNG"), + ("jpeg", ImageFormatName::Jpeg, "JPEG"), + ("gif", ImageFormatName::Gif, "GIF"), + ("tiff", ImageFormatName::Tiff, "TIFF"), + ("xpm", ImageFormatName::Xpm, "XPM"), + ("xbm", ImageFormatName::Xbm, "XBM"), + ("pbm", ImageFormatName::Pbm, "PBM"), + ("webp", ImageFormatName::Webp, "WEBP"), + ("svg", ImageFormatName::Svg, "SVG"), + ("imagemagick", ImageFormatName::Imagemagick, "IMAGEMAGICK"), + ("postscript", ImageFormatName::Postscript, "POSTSCRIPT"), + ("native-image", ImageFormatName::NativeImage, "NATIVE-IMAGE"), + ] { + assert_eq!(ImageFormatName::from_lisp_type(symbol), format); + assert_eq!(ImageFormatName::from(symbol), format); + assert_eq!(format.as_str(), diagnostic_name); + assert_eq!(format.to_string(), diagnostic_name); + } +} + +#[test] +fn unknown_image_type_names_are_preserved_without_normalization() { + for symbol in [ + "bmp", + "PNG", + "Png", + "NativeImage", + "native_image", + " png", + "png ", + "other", + "", + "未知", + ] { + let format = ImageFormatName::from_lisp_type(symbol); + assert_eq!(format, ImageFormatName::Other(symbol.to_owned())); + assert_eq!(ImageFormatName::from(symbol), format); + assert_eq!(format.as_str(), symbol); + assert_eq!(format.to_string(), symbol); + } } /// Only PNG and PBM have a `Not a file:` arm in GNU; a loader that has