#[non_exhaustive]pub enum ImageFormat {
Png,
Jpeg,
Gif,
Bmp,
Wmf,
Emf,
Unknown(String),
}Expand description
Supported image formats.
Marked #[non_exhaustive] so new formats can be added in future
phases without a breaking change.
§Examples
use hwpforge_core::image::ImageFormat;
let fmt = ImageFormat::Png;
assert_eq!(fmt.to_string(), "PNG");
let unknown = ImageFormat::Unknown("SVG".to_string());
assert_eq!(unknown.to_string(), "svg");Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Png
Portable Network Graphics.
Jpeg
JPEG.
Gif
Graphics Interchange Format.
Bmp
Windows Bitmap.
Wmf
Windows Metafile.
Emf
Enhanced Metafile.
Unknown(String)
Unrecognized format with its extension or MIME type.
Implementations§
Source§impl ImageFormat
impl ImageFormat
Sourcepub fn from_extension(path: &str) -> Self
pub fn from_extension(path: &str) -> Self
Infers an ImageFormat from a file path’s extension.
The extension is extracted from everything after the last '.' in the
path string and matched case-insensitively. If no dot is found, or the
extension is not recognized, ImageFormat::Unknown is returned
containing the lowercase extension (or an empty string when absent).
§Examples
use hwpforge_core::image::ImageFormat;
assert_eq!(ImageFormat::from_extension("photo.png"), ImageFormat::Png);
assert_eq!(ImageFormat::from_extension("image.JPG"), ImageFormat::Jpeg);
assert_eq!(ImageFormat::from_extension("file.jpeg"), ImageFormat::Jpeg);
assert_eq!(ImageFormat::from_extension("doc.gif"), ImageFormat::Gif);
assert_eq!(ImageFormat::from_extension("img.bmp"), ImageFormat::Bmp);
assert_eq!(ImageFormat::from_extension("chart.wmf"), ImageFormat::Wmf);
assert_eq!(ImageFormat::from_extension("dia.emf"), ImageFormat::Emf);
assert_eq!(
ImageFormat::from_extension("file.xyz"),
ImageFormat::Unknown("xyz".to_string()),
);
assert_eq!(
ImageFormat::from_extension("noext"),
ImageFormat::Unknown(String::new()),
);
assert_eq!(ImageFormat::from_extension("multi.dot.png"), ImageFormat::Png);Sourcepub fn sniff(bytes: &[u8]) -> Option<Self>
pub fn sniff(bytes: &[u8]) -> Option<Self>
Sniffs an ImageFormat from the leading magic bytes.
Extension-derived formats (Self::from_extension) are diagnostic
hints only — file names cannot be trusted. Byte sniffing is the
ground truth for admission decisions (e.g. the Markdown → HWPX image
embed loader only packages bytes whose magic identifies a format
HWPX BinData natively carries).
Returns None for empty, truncated, or unrecognized bytes — callers
must not guess from content. Magic table (shares the render-side
sniffer’s rules in smithy-pdf, with two deliberate divergences for
ingestion, W6 §12b·§12-r2):
- PNG
89 50 4E 47 0D 0A 1A 0A· JPEGFF D8 FF· GIFGIF87a/GIF89a - BMP
BMplus structural header checks —bfOffBits(offset 10) within14..=lenand a known DIB header size (offset 14 ∈ {12, 40, 52, 56, 64, 108, 124}). The 2-byte magic alone admits arbitrary bytes starting withBMinto packages (ingestion is stricter than the render sniffer, which only gates an error path). - EMF record type
01 00 00 00+" EMF"signature at offset 40 - WMF placeable only (
D7 CD C6 9A) — standard WMF’s magic is too weak and would misfire, so it stays unrecognized - WebP is intentionally absent (render sniffer knows it): HWPX
BinDatacarry of WebP is unverified against Hancom, so ingestion refuses it until a native fixture proves it (§12-r2).
§Examples
use hwpforge_core::image::ImageFormat;
let png = [0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A, 0, 0];
assert_eq!(ImageFormat::sniff(&png), Some(ImageFormat::Png));
assert_eq!(ImageFormat::sniff(b"GIF89a\x00"), Some(ImageFormat::Gif));
assert_eq!(ImageFormat::sniff(b"not an image"), None);
assert_eq!(ImageFormat::sniff(&[]), None);Sourcepub fn canonical_extension(&self) -> Option<&'static str>
pub fn canonical_extension(&self) -> Option<&'static str>
Canonical file extension for a sniffed format (used to build
synthetic package keys like image1.png).
Returns None for Self::Unknown — unknown formats never get a
synthetic key (they are not admitted into packages).
§Examples
use hwpforge_core::image::ImageFormat;
assert_eq!(ImageFormat::Jpeg.canonical_extension(), Some("jpg"));
assert_eq!(ImageFormat::Unknown("svg".into()).canonical_extension(), None);Trait Implementations§
Source§impl Clone for ImageFormat
impl Clone for ImageFormat
Source§fn clone(&self) -> ImageFormat
fn clone(&self) -> ImageFormat
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ImageFormat
impl Debug for ImageFormat
Source§impl<'de> Deserialize<'de> for ImageFormat
impl<'de> Deserialize<'de> for ImageFormat
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for ImageFormat
impl Display for ImageFormat
impl Eq for ImageFormat
Source§impl Hash for ImageFormat
impl Hash for ImageFormat
Source§impl JsonSchema for ImageFormat
impl JsonSchema for ImageFormat
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreSource§impl PartialEq for ImageFormat
impl PartialEq for ImageFormat
Source§impl Serialize for ImageFormat
impl Serialize for ImageFormat
impl StructuralPartialEq for ImageFormat
Auto Trait Implementations§
impl Freeze for ImageFormat
impl RefUnwindSafe for ImageFormat
impl Send for ImageFormat
impl Sync for ImageFormat
impl Unpin for ImageFormat
impl UnsafeUnpin for ImageFormat
impl UnwindSafe for ImageFormat
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.