Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions crates/neomacs-display-protocol/src/image_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -44,28 +45,15 @@ pub enum ImageFormatName {
Postscript,
NativeImage,
/// A type this build has no table entry for, under the Lisp symbol's name.
#[strum(default)]
Other(String),
}

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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <TYPE> file:` arm in GNU; a loader that has
Expand Down
Loading