From a937379cdfab48adb813f167e76f316c3eb1a2a7 Mon Sep 17 00:00:00 2001 From: Sarah Allen Date: Fri, 28 Jul 2023 12:31:59 -0700 Subject: [PATCH] simple example of common use case --- Cargo.toml | 1 + examples/ext_guess.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 examples/ext_guess.rs diff --git a/Cargo.toml b/Cargo.toml index 7ca2b13..764f19f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ keywords = ["mime", "filesystem", "extension"] repository = "https://github.com/abonander/mime_guess" documentation = "https://docs.rs/mime_guess/" readme = "README.md" +autoexamples = true [features] default = ["rev-mappings"] diff --git a/examples/ext_guess.rs b/examples/ext_guess.rs new file mode 100644 index 0000000..7f28c51 --- /dev/null +++ b/examples/ext_guess.rs @@ -0,0 +1,20 @@ +// simple example illustrating how diffent extensions +// can match zero or multiple mime types + +fn print_guess_from_path(path: &str) { + let guess: mime_guess::MimeGuess = mime_guess::from_path(path); + if guess.count() == 0 { + println!("unable to guess mime type from path: {}", path); + } else { + println!("guessing from path: {}", path); + guess.iter().for_each(|s| { + println!(" mime: {}", s); + }); + } +} + +fn main() { + print_guess_from_path("/path/to/file"); + print_guess_from_path("/path/to/file.gif"); + print_guess_from_path("/path/to/file.md"); +}