image icon indicating copy to clipboard operation
image copied to clipboard

Would ImageFormat::to_mime_type be valuable for this crate?

Open asaaki opened this issue 2 years ago • 2 comments

This crate already provides a ImageFormat::from_mime_type function, but there is no counterpart (like to_mime_type).

There are circumstances this would be very handy, like image manipulation web services, which need to provide MIME types as request/response headers.

If maintainers agree but don't have time, I can propose a PR.

Draft

impl ImageFormat {
  // <snip>

  pub fn to_mime_type(&self) -> Option<&'static str>
    {
      match self {
        ImageFormat::Png => Some("image/png"),
        // <snip>
    }
}

asaaki avatar Jun 06 '22 17:06 asaaki

Sounds good, please do. Directly discussing a PR has usually low friction for small changes that don't require larger design work / affect multiple components. In this case it seems fine. (The largest question is whether we expose it as a static string or a String, or returning something that implements Display/ToString similar to std::path::Path::display(). But it's a lot simpler to return a static str and that covers all options, so I don't see any reason not to go with the draft interface).

HeroicKatora avatar Jun 06 '22 18:06 HeroicKatora

This would be super helpful. I need to write out images and am managing this in my application.

andymac4182 avatar Sep 13 '22 09:09 andymac4182