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
69
70
71
72
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::translate::*;

use std::fmt;
use std::mem;
use std::ptr;

glib::wrapper! {
    #[doc(alias = "GFileAttributeInfo")]
    pub struct FileAttributeInfo(BoxedInline<ffi::GFileAttributeInfo>);

    match fn {
        copy => |ptr| {
            let copy = glib::ffi::g_malloc0(mem::size_of::<ffi::GFileAttributeInfo>()) as *mut ffi::GFileAttributeInfo;
            if !(*ptr).name.is_null() {
                (*copy).name = glib::ffi::g_strdup((*ptr).name);
            }
            copy
        },
        free => |ptr| {
            if !(*ptr).name.is_null() {
                glib::ffi::g_free((*ptr).name as *mut _);
            }
            glib::ffi::g_free(ptr as *mut _);
        },
        init => |ptr| {
            *ptr = mem::zeroed();
        },
        copy_into => |dest, src| {
            ptr::copy_nonoverlapping(src, dest, 1);
            if !(*dest).name.is_null() {
                (*dest).name = glib::ffi::g_strdup((*dest).name);
            }
        },
        clear => |ptr| {
            if !(*ptr).name.is_null() {
                glib::ffi::g_free((*ptr).name as *mut _);
            }
        },
    }
}

impl FileAttributeInfo {
    pub fn name(&self) -> &str {
        unsafe {
            use std::ffi::CStr;

            CStr::from_ptr(self.inner.name)
                .to_str()
                .expect("non-UTF-8 string")
        }
    }

    pub fn type_(&self) -> crate::FileAttributeType {
        unsafe { from_glib(self.inner.type_) }
    }

    pub fn flags(&self) -> crate::FileAttributeInfoFlags {
        unsafe { from_glib(self.inner.flags) }
    }
}

impl fmt::Debug for FileAttributeInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FileAttributeInfo")
            .field("name", &self.name())
            .field("type", &self.type_())
            .field("flags", &self.flags())
            .finish()
    }
}