mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-10 06:04:20 +00:00
Add example for mime crate
This commit is contained in:
parent
333c8a2bfa
commit
aa5ca8c7c8
4 changed files with 68 additions and 0 deletions
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
<a name="ex-json-value"></a>
|
||||
|
@ -688,6 +689,68 @@ fn run() -> Result<()> {
|
|||
# quick_main!(run);
|
||||
```
|
||||
|
||||
|
||||
[ex-mime-from-filename]: #ex-mime-from-filename
|
||||
<a name="ex-mime-from-filename"></a>
|
||||
## 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}}
|
||||
|
||||
<!-- API Reference -->
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue