Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: convenience method to get mime type from string #39

Closed
yoshuawuyts opened this issue Oct 14, 2018 · 3 comments
Closed

feature: convenience method to get mime type from string #39

yoshuawuyts opened this issue Oct 14, 2018 · 3 comments

Comments

@yoshuawuyts
Copy link

It takes a bit of boilerplate to convert /foo/img.png to MIME image/png:

  pub fn get_mime(src: &str) -> String {
    let ext = Path::new(src)
      .extension()
      .and_then(OsStr::to_str)
      .unwrap_or("");
    let mime_type = mime_guess::get_mime_type(&ext);
    format!("{}/{}", mime_type.type_(), mime_type.subtype())
  }

It'd be nice if there was a convenience method built-in that would so something similar to the above.

Thanks!

@abonander
Copy link
Owner

I think #38 will cover this, I just keep forgetting to merge it.

@yoshuawuyts
Copy link
Author

I figured it out:

    let icon_type = mime_guess::guess_mime_type(&src).to_string();

What tripped me up here is that .to_string() didn't show up in the docs at all, so I didn't know it was available. Thanks for the quick reply tho!

@abonander
Copy link
Owner

abonander commented Oct 14, 2018

That's because Mime implements Display and ToString is implemented for all implementors of that.

However, the strings are available already so you don't have to round-trip like that. It just needs a function exposing them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants