Skip to content

Commit 93a073e

Browse files
authored
Merge pull request #1591 from davidmhewitt/add_content_type_guess_for_filename
gio: manually implement content_type_guess
2 parents 3161dee + 5830056 commit 93a073e

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

gio/Gir.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ status = "generate"
249249
ignore = true
250250
[[object.function]]
251251
name = "content_type_guess"
252-
[[object.function.parameter]]
253-
name = "filename"
254-
string_type = "filename"
252+
# implemented manually until gir generates nullable array parameters
253+
# https://github.com/gtk-rs/gir/issues/1133
254+
manual = true
255255

256256
[[object]]
257257
name = "Gio.ActionGroup"

gio/src/auto/functions.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,6 @@ pub fn content_type_get_symbolic_icon(type_: &str) -> Icon {
178178
}
179179
}
180180

181-
#[doc(alias = "g_content_type_guess")]
182-
pub fn content_type_guess(
183-
filename: Option<impl AsRef<std::path::Path>>,
184-
data: &[u8],
185-
) -> (glib::GString, bool) {
186-
let data_size = data.len() as _;
187-
unsafe {
188-
let mut result_uncertain = std::mem::MaybeUninit::uninit();
189-
let ret = from_glib_full(ffi::g_content_type_guess(
190-
filename.as_ref().map(|p| p.as_ref()).to_glib_none().0,
191-
data.to_glib_none().0,
192-
data_size,
193-
result_uncertain.as_mut_ptr(),
194-
));
195-
(ret, from_glib(result_uncertain.assume_init()))
196-
}
197-
}
198-
199181
#[doc(alias = "g_content_type_guess_for_tree")]
200182
pub fn content_type_guess_for_tree(root: &impl IsA<File>) -> Vec<glib::GString> {
201183
unsafe {

gio/src/content_type.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use std::ptr;
4+
5+
use glib::translate::*;
6+
7+
use crate::ffi;
8+
9+
#[doc(alias = "g_content_type_guess")]
10+
pub fn content_type_guess<'a>(
11+
filename: Option<impl AsRef<std::path::Path>>,
12+
data: impl Into<Option<&'a [u8]>>,
13+
) -> (glib::GString, bool) {
14+
let data = data.into();
15+
let data_size = data.map_or(0, |d| d.len());
16+
unsafe {
17+
let mut result_uncertain = std::mem::MaybeUninit::uninit();
18+
let ret = from_glib_full(ffi::g_content_type_guess(
19+
filename.as_ref().map(|p| p.as_ref()).to_glib_none().0,
20+
data.map_or(ptr::null(), |d| d.to_glib_none().0),
21+
data_size,
22+
result_uncertain.as_mut_ptr(),
23+
));
24+
(ret, from_glib(result_uncertain.assume_init()))
25+
}
26+
}

gio/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod cancellable;
2222
pub use cancellable::CancelledHandlerId;
2323
mod cancellable_future;
2424
pub use crate::cancellable_future::{CancellableFuture, Cancelled};
25+
mod content_type;
2526
mod converter;
2627
mod credentials;
2728
mod data_input_stream;
@@ -116,6 +117,7 @@ pub mod builders {
116117

117118
pub mod functions {
118119
pub use super::auto::functions::*;
120+
pub use super::content_type::content_type_guess;
119121
}
120122

121123
pub use crate::auto::*;

gio/tests/content_type.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
#[cfg(unix)]
4+
#[test]
5+
fn test_content_type_guess() {
6+
// We only test for directory and file without extension here as we can't guarantee the
7+
// CI runners will have any mimetypes installed.
8+
let ret: (glib::GString, bool) =
9+
gio::functions::content_type_guess(Some(std::path::Path::new("test/")), None);
10+
assert_eq!(ret.0, "inode/directory");
11+
12+
let ret: (glib::GString, bool) =
13+
gio::functions::content_type_guess(Some(std::path::Path::new("test")), None);
14+
assert_eq!(ret.0, "application/octet-stream");
15+
}

0 commit comments

Comments
 (0)