lib.rs (1084B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 extern crate mime_guess; 6 extern crate nserror; 7 extern crate nsstring; 8 9 use nserror::{nsresult, NS_ERROR_FAILURE, NS_ERROR_INVALID_ARG, NS_ERROR_NOT_AVAILABLE, NS_OK}; 10 use nsstring::{nsACString, nsCString}; 11 use std::path::Path; 12 use std::str; 13 14 #[no_mangle] 15 pub extern "C" fn mimeGuessFromPath(path: &nsACString, content_type: &mut nsCString) -> nsresult { 16 let path_data = str::from_utf8(path.as_ref()); 17 if path_data.is_err() { 18 return NS_ERROR_INVALID_ARG; // Not UTF8 19 } 20 21 let content_path = Path::new(path_data.unwrap()); 22 if content_path.extension().is_none() { 23 return NS_ERROR_NOT_AVAILABLE; // No mime type information 24 } 25 26 let maybe_mime = mime_guess::from_path(content_path).first_raw(); 27 if maybe_mime.is_none() { 28 return NS_ERROR_FAILURE; // Not recognized 29 } 30 31 content_type.assign(maybe_mime.unwrap()); 32 33 NS_OK 34 }