-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gio: manually implement content_type_guess
- Loading branch information
1 parent
c8df619
commit 5830056
Showing
5 changed files
with
46 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
use std::ptr; | ||
|
||
use glib::translate::*; | ||
|
||
use crate::ffi; | ||
|
||
#[doc(alias = "g_content_type_guess")] | ||
pub fn content_type_guess<'a>( | ||
filename: Option<impl AsRef<std::path::Path>>, | ||
data: impl Into<Option<&'a [u8]>>, | ||
) -> (glib::GString, bool) { | ||
let data = data.into(); | ||
let data_size = data.map_or(0, |d| d.len()); | ||
unsafe { | ||
let mut result_uncertain = std::mem::MaybeUninit::uninit(); | ||
let ret = from_glib_full(ffi::g_content_type_guess( | ||
filename.as_ref().map(|p| p.as_ref()).to_glib_none().0, | ||
data.map_or(ptr::null(), |d| d.to_glib_none().0), | ||
data_size, | ||
result_uncertain.as_mut_ptr(), | ||
)); | ||
(ret, from_glib(result_uncertain.assume_init())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
#[cfg(unix)] | ||
#[test] | ||
fn test_content_type_guess() { | ||
// We only test for directory and file without extension here as we can't guarantee the | ||
// CI runners will have any mimetypes installed. | ||
let ret: (glib::GString, bool) = | ||
gio::functions::content_type_guess(Some(std::path::Path::new("test/")), None); | ||
assert_eq!(ret.0, "inode/directory"); | ||
|
||
let ret: (glib::GString, bool) = | ||
gio::functions::content_type_guess(Some(std::path::Path::new("test")), None); | ||
assert_eq!(ret.0, "application/octet-stream"); | ||
} |