1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// This file was generated by gir (4b09025) from gir-files (71d73f0)
// DO NOT EDIT

use Error;
use ResourceLookupFlags;
use ffi;
use glib;
use glib::translate::*;
use std;
use std::mem;
use std::ptr;

glib_wrapper! {
    pub struct Resource(Shared<ffi::GResource>);

    match fn {
        ref => |ptr| ffi::g_resource_ref(ptr),
        unref => |ptr| ffi::g_resource_unref(ptr),
    }
}

impl Resource {
    pub fn new_from_data(data: &glib::Bytes) -> Result<Resource, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_resource_new_from_data(data.to_glib_none().0, &mut error);
            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }

    pub fn enumerate_children(&self, path: &str, lookup_flags: ResourceLookupFlags) -> Result<Vec<String>, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_resource_enumerate_children(self.to_glib_none().0, path.to_glib_none().0, lookup_flags.to_glib(), &mut error);
            if error.is_null() { Ok(FromGlibPtrContainer::from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }

    pub fn get_info(&self, path: &str, lookup_flags: ResourceLookupFlags) -> Result<(usize, u32), Error> {
        unsafe {
            let mut size = mem::uninitialized();
            let mut flags = mem::uninitialized();
            let mut error = ptr::null_mut();
            let _ = ffi::g_resource_get_info(self.to_glib_none().0, path.to_glib_none().0, lookup_flags.to_glib(), &mut size, &mut flags, &mut error);
            if error.is_null() { Ok((size, flags)) } else { Err(from_glib_full(error)) }
        }
    }

    pub fn lookup_data(&self, path: &str, lookup_flags: ResourceLookupFlags) -> Result<glib::Bytes, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_resource_lookup_data(self.to_glib_none().0, path.to_glib_none().0, lookup_flags.to_glib(), &mut error);
            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }

    //pub fn open_stream(&self, path: &str, lookup_flags: ResourceLookupFlags) -> Result</*Ignored*/InputStream, Error> {
    //    unsafe { TODO: call ffi::g_resource_open_stream() }
    //}

    pub fn load<P: AsRef<std::path::Path>>(filename: P) -> Result<Resource, Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_resource_load(filename.as_ref().to_glib_none().0, &mut error);
            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }
}