From 5830056962a891696b5f3268634a8d609cd5c72b Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Sun, 8 Dec 2024 14:52:34 +0900 Subject: [PATCH] gio: manually implement content_type_guess --- gio/Gir.toml | 6 +++--- gio/src/auto/functions.rs | 18 ------------------ gio/src/content_type.rs | 26 ++++++++++++++++++++++++++ gio/src/lib.rs | 2 ++ gio/tests/content_type.rs | 15 +++++++++++++++ 5 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 gio/src/content_type.rs create mode 100644 gio/tests/content_type.rs diff --git a/gio/Gir.toml b/gio/Gir.toml index 0fcf3cc6044f..3b3f4603547c 100644 --- a/gio/Gir.toml +++ b/gio/Gir.toml @@ -249,9 +249,9 @@ status = "generate" ignore = true [[object.function]] name = "content_type_guess" - [[object.function.parameter]] - name = "filename" - string_type = "filename" + # implemented manually until gir generates nullable array parameters + # https://github.com/gtk-rs/gir/issues/1133 + manual = true [[object]] name = "Gio.ActionGroup" diff --git a/gio/src/auto/functions.rs b/gio/src/auto/functions.rs index aeecbd2a3814..5a3df411f7dc 100644 --- a/gio/src/auto/functions.rs +++ b/gio/src/auto/functions.rs @@ -178,24 +178,6 @@ pub fn content_type_get_symbolic_icon(type_: &str) -> Icon { } } -#[doc(alias = "g_content_type_guess")] -pub fn content_type_guess( - filename: Option>, - data: &[u8], -) -> (glib::GString, bool) { - let data_size = data.len() as _; - 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.to_glib_none().0, - data_size, - result_uncertain.as_mut_ptr(), - )); - (ret, from_glib(result_uncertain.assume_init())) - } -} - #[doc(alias = "g_content_type_guess_for_tree")] pub fn content_type_guess_for_tree(root: &impl IsA) -> Vec { unsafe { diff --git a/gio/src/content_type.rs b/gio/src/content_type.rs new file mode 100644 index 000000000000..37a8d13bad08 --- /dev/null +++ b/gio/src/content_type.rs @@ -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>, + data: impl Into>, +) -> (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())) + } +} diff --git a/gio/src/lib.rs b/gio/src/lib.rs index da084ca7d6a3..7e6645ddedd2 100644 --- a/gio/src/lib.rs +++ b/gio/src/lib.rs @@ -21,6 +21,7 @@ mod async_initable; mod cancellable; mod cancellable_future; pub use crate::cancellable_future::{CancellableFuture, Cancelled}; +mod content_type; mod converter; mod credentials; mod data_input_stream; @@ -115,6 +116,7 @@ pub mod builders { pub mod functions { pub use super::auto::functions::*; + pub use super::content_type::content_type_guess; } pub use crate::auto::*; diff --git a/gio/tests/content_type.rs b/gio/tests/content_type.rs new file mode 100644 index 000000000000..b05193430e72 --- /dev/null +++ b/gio/tests/content_type.rs @@ -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"); +}