From aa5ca8c7c8bb824959d366eb40319776524a3fd1 Mon Sep 17 00:00:00 2001 From: Julien Delange Date: Sun, 25 Feb 2018 11:28:06 -0800 Subject: [PATCH 1/3] Add example for mime crate --- Cargo.toml | 1 + src/encoding.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ src/intro.md | 2 ++ src/links.md | 2 ++ 4 files changed, 68 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index a6333be..63bfada 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ threadpool = "1.6" toml = "0.4" url = "1.6" walkdir = "2.0" +mime = "0.3.5" [target.'cfg(target_os = "linux")'.dependencies] syslog = "3.3.0" diff --git a/src/encoding.md b/src/encoding.md index 5ecc0fa..18a1f44 100644 --- a/src/encoding.md +++ b/src/encoding.md @@ -14,6 +14,7 @@ | [Handle invalid CSV data with Serde][ex-invalid-csv] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] | | [Serialize records to CSV][ex-serialize-csv] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] | | [Serialize records to CSV using Serde][ex-csv-serde] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] | +| [Get MIME type from filename][ex-mime-from-filename] | [![mime-badge]][mime] | [![cat-encoding-badge]][cat-encoding] | [ex-json-value]: #ex-json-value @@ -688,6 +689,68 @@ fn run() -> Result<()> { # quick_main!(run); ``` + +[ex-mime-from-filename]: #ex-mime-from-filename + +## Get MIME type from filename + +[![mime-badge]][mime] [![cat-encoding-badge]][cat-encoding] + +The following example shows how to return the correct MIME type from a given +filename using the [mime] crate. + +```rust +# #[macro_use] +# extern crate error_chain; +#[macro_use] + +use std::io; +# +# error_chain! { +# foreign_links { +# IOError(std::io::Error); +# } +# } + +extern crate mime; +use mime::Mime; + +/// Return the MIME type according to file extension. Default is text/plain +fn find_mimetype (filename : &String) -> Mime{ + + let parts : Vec<&str> = filename.split('.').collect(); + + /// Filter on the file extension + let res = match parts.last() { + Some(v) => + match *v { + "png" => mime::IMAGE_PNG, + "jpg" => mime::IMAGE_JPEG, + "json" => mime::APPLICATION_JSON, + &_ => mime::TEXT_PLAIN, + }, + None => mime::TEXT_PLAIN, + }; + return res; +} + +fn run() -> Result<()> { + let filenames = vec!("foobar.jpg", "foo.bar", "foobar.png"); + for file in filenames { + let mime = find_mimetype(&file.to_owned()); + println!("MIME for {:?}: {:?}", file, mime); + } + + + Ok(()) +} +# +# quick_main!(run); +``` + + + + {{#include links.md}} diff --git a/src/intro.md b/src/intro.md index 45602b3..c09284e 100644 --- a/src/intro.md +++ b/src/intro.md @@ -72,6 +72,7 @@ community. It needs and welcomes help. For details see | [Handle invalid CSV data with Serde][ex-invalid-csv] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] | | [Serialize records to CSV][ex-serialize-csv] | [![csv-badge]][csv] | [![cat-encoding-badge]][cat-encoding] | | [Serialize records to CSV using Serde][ex-csv-serde] | [![csv-badge]][csv] [![serde-badge]][serde] | [![cat-encoding-badge]][cat-encoding] | +| [Get MIME type from filename][ex-mime-from-filename] | [![mime-badge]][mime] | [![cat-encoding-badge]][cat-encoding] | ## [Concurrency](concurrency.html) @@ -176,6 +177,7 @@ community. It needs and welcomes help. For details see [ex-csv-filter]: encoding.html#ex-csv-filter [ex-csv-read]: encoding.html#ex-csv-read [ex-csv-serde]: encoding.html#ex-csv-serde +[ex-mime-from-filename]: encoding.html#ex-mime-from-filename [ex-datetime-arithmetic]: basics.html#ex-datetime-arithmetic [ex-dedup-filenames]: app.html#ex-dedup-filenames [ex-error-chain-avoid-discarding]: basics.html#ex-error-chain-avoid-discarding diff --git a/src/links.md b/src/links.md index fc77ef8..f7e84a6 100644 --- a/src/links.md +++ b/src/links.md @@ -63,7 +63,9 @@ Keep lines sorted. [crossbeam-badge]: https://badge-cache.kominick.com/crates/v/crossbeam.svg?label=crossbeam [crossbeam]: https://docs.rs/crossbeam/ [csv-badge]: https://badge-cache.kominick.com/crates/v/csv.svg?label=csv +[mime-badge]: https://badge-cache.kominick.com/crates/v/csv.svg?label=mime [csv]: https://docs.rs/csv/ +[mime]: https://docs.rs/mime/ [data-encoding-badge]: https://badge-cache.kominick.com/crates/v/data-encoding.svg?label=data-encoding [data-encoding]: https://docs.rs/data-encoding/ [env_logger-badge]: https://badge-cache.kominick.com/crates/v/env_logger.svg?label=env_logger From a3052fbcd83c820a9c2430757599794dd2b60a14 Mon Sep 17 00:00:00 2001 From: Julien Delange Date: Sun, 25 Feb 2018 11:36:38 -0800 Subject: [PATCH 2/3] Fix names ordering in Cargo.toml and links.md --- Cargo.toml | 2 +- src/links.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 63bfada..e9e6471 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ log4rs = "0.7" image = "0.18" lazy_static = "1.0" memmap = "0.6" +mime = "0.3.5" num = "0.1" num_cpus = "1.8" petgraph = "0.4" @@ -46,7 +47,6 @@ threadpool = "1.6" toml = "0.4" url = "1.6" walkdir = "2.0" -mime = "0.3.5" [target.'cfg(target_os = "linux")'.dependencies] syslog = "3.3.0" diff --git a/src/links.md b/src/links.md index f7e84a6..62ed48b 100644 --- a/src/links.md +++ b/src/links.md @@ -63,9 +63,7 @@ Keep lines sorted. [crossbeam-badge]: https://badge-cache.kominick.com/crates/v/crossbeam.svg?label=crossbeam [crossbeam]: https://docs.rs/crossbeam/ [csv-badge]: https://badge-cache.kominick.com/crates/v/csv.svg?label=csv -[mime-badge]: https://badge-cache.kominick.com/crates/v/csv.svg?label=mime [csv]: https://docs.rs/csv/ -[mime]: https://docs.rs/mime/ [data-encoding-badge]: https://badge-cache.kominick.com/crates/v/data-encoding.svg?label=data-encoding [data-encoding]: https://docs.rs/data-encoding/ [env_logger-badge]: https://badge-cache.kominick.com/crates/v/env_logger.svg?label=env_logger @@ -88,6 +86,8 @@ Keep lines sorted. [log]: https://docs.rs/log/ [memmap-badge]: https://badge-cache.kominick.com/crates/v/memmap.svg?label=memmap [memmap]: https://docs.rs/memmap/ +[mime-badge]: https://badge-cache.kominick.com/crates/v/csv.svg?label=mime +[mime]: https://docs.rs/mime/ [num-badge]: https://badge-cache.kominick.com/crates/v/num.svg?label=num [num]: https://docs.rs/num/ [num_cpus-badge]: https://badge-cache.kominick.com/crates/v/num_cpus.svg?label=num_cpus From ef082e79e3da02093c7e0bf480c88bd266e6fef0 Mon Sep 17 00:00:00 2001 From: Julien Delange Date: Sun, 25 Feb 2018 11:38:03 -0800 Subject: [PATCH 3/3] Fix ordering in intro.md --- src/intro.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/intro.md b/src/intro.md index c09284e..9fff3ac 100644 --- a/src/intro.md +++ b/src/intro.md @@ -177,7 +177,6 @@ community. It needs and welcomes help. For details see [ex-csv-filter]: encoding.html#ex-csv-filter [ex-csv-read]: encoding.html#ex-csv-read [ex-csv-serde]: encoding.html#ex-csv-serde -[ex-mime-from-filename]: encoding.html#ex-mime-from-filename [ex-datetime-arithmetic]: basics.html#ex-datetime-arithmetic [ex-dedup-filenames]: app.html#ex-dedup-filenames [ex-error-chain-avoid-discarding]: basics.html#ex-error-chain-avoid-discarding @@ -211,6 +210,7 @@ community. It needs and welcomes help. For details see [ex-log-syslog]: logging.html#ex-log-syslog [ex-log-timestamp]: logging.html#ex-log-timestamp [ex-measure-elapsed-time]: basics.html#ex-measure-elapsed-time +[ex-mime-from-filename]: encoding.html#ex-mime-from-filename [ex-paginated-api]: net.html#ex-paginated-api [ex-parse-datetime]: basics.html#ex-parse-datetime [ex-parse-subprocess-input]: basics.html#ex-parse-subprocess-input