Skip to content

Commit

Permalink
gio: manually implement content_type_guess
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmhewitt committed Dec 8, 2024
1 parent c8df619 commit 5830056
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
6 changes: 3 additions & 3 deletions gio/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 0 additions & 18 deletions gio/src/auto/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<impl AsRef<std::path::Path>>,
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<File>) -> Vec<glib::GString> {
unsafe {
Expand Down
26 changes: 26 additions & 0 deletions gio/src/content_type.rs
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()))
}
}
2 changes: 2 additions & 0 deletions gio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::*;
Expand Down
15 changes: 15 additions & 0 deletions gio/tests/content_type.rs
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");
}

0 comments on commit 5830056

Please sign in to comment.