gio/
file_attribute_value.rs1use glib::{
4 object::ObjectType,
5 translate::{IntoGlib, ToGlibPtr},
6};
7
8use crate::FileAttributeType;
9
10use std::ffi::CStr;
11
12#[derive(Debug)]
13pub struct FileAttributeValue<'a>(FileAttributeValueInner<'a>);
14
15impl From<&str> for FileAttributeValue<'_> {
16 fn from(value: &str) -> Self {
17 Self(FileAttributeValueInner::String(
18 ToGlibPtr::<*mut libc::c_char>::to_glib_none(value).1,
19 ))
20 }
21}
22
23impl<'a> From<&'a CStr> for FileAttributeValue<'a> {
24 fn from(value: &'a CStr) -> Self {
25 Self(FileAttributeValueInner::ByteString(value))
26 }
27}
28
29impl From<bool> for FileAttributeValue<'_> {
30 fn from(value: bool) -> Self {
31 Self(FileAttributeValueInner::Boolean(value.into_glib()))
32 }
33}
34
35impl From<u32> for FileAttributeValue<'_> {
36 fn from(value: u32) -> Self {
37 Self(FileAttributeValueInner::Uint32(value))
38 }
39}
40
41impl From<i32> for FileAttributeValue<'_> {
42 fn from(value: i32) -> Self {
43 Self(FileAttributeValueInner::Int32(value))
44 }
45}
46
47impl From<u64> for FileAttributeValue<'_> {
48 fn from(value: u64) -> Self {
49 Self(FileAttributeValueInner::Uint64(value))
50 }
51}
52
53impl From<i64> for FileAttributeValue<'_> {
54 fn from(value: i64) -> Self {
55 Self(FileAttributeValueInner::Int64(value))
56 }
57}
58
59impl<'a, T: AsRef<glib::Object>> From<&'a T> for FileAttributeValue<'a> {
60 fn from(value: &'a T) -> Self {
61 Self(FileAttributeValueInner::Object(value.as_ref()))
62 }
63}
64
65impl<'a> From<&'a [&str]> for FileAttributeValue<'a> {
66 fn from(value: &'a [&str]) -> Self {
67 Self(FileAttributeValueInner::Stringv(value.into()))
68 }
69}
70
71impl FileAttributeValue<'_> {
72 pub(crate) fn type_(&self) -> FileAttributeType {
73 self.0.type_()
74 }
75
76 pub(crate) fn as_ptr(&self) -> glib::ffi::gpointer {
77 self.0.as_ptr()
78 }
79
80 pub(crate) fn for_pointer(type_: FileAttributeType, value_p: *mut std::ffi::c_void) -> Self {
81 Self(FileAttributeValueInner::Pointer(type_, value_p))
82 }
83}
84
85#[derive(Debug)]
86pub(crate) enum FileAttributeValueInner<'a> {
87 Pointer(FileAttributeType, glib::ffi::gpointer),
88 String(<&'a str as ToGlibPtr<'a, *mut libc::c_char>>::Storage),
89 ByteString(&'a CStr),
90 Boolean(glib::ffi::gboolean),
91 Uint32(u32),
92 Int32(i32),
93 Uint64(u64),
94 Int64(i64),
95 Object(&'a glib::Object),
96 Stringv(glib::StrV),
97}
98
99impl FileAttributeValueInner<'_> {
100 pub(crate) fn type_(&self) -> FileAttributeType {
101 match self {
102 Self::Pointer(type_, _) => *type_,
103 Self::String(_) => FileAttributeType::String,
104 Self::ByteString(_) => FileAttributeType::ByteString,
105 Self::Boolean(_) => FileAttributeType::Boolean,
106 Self::Uint32(_) => FileAttributeType::Uint32,
107 Self::Int32(_) => FileAttributeType::Int32,
108 Self::Uint64(_) => FileAttributeType::Uint64,
109 Self::Int64(_) => FileAttributeType::Int64,
110 Self::Object(_) => FileAttributeType::Object,
111 Self::Stringv(_) => FileAttributeType::Stringv,
112 }
113 }
114
115 pub(crate) fn as_ptr(&self) -> glib::ffi::gpointer {
116 match self {
117 Self::Pointer(_, s) => *s,
118 Self::String(s) => s.as_ptr() as _,
119 Self::ByteString(s) => s.as_ptr() as _,
120 Self::Boolean(s) => s as *const i32 as _,
121 Self::Uint32(s) => s as *const u32 as _,
122 Self::Int32(s) => s as *const i32 as _,
123 Self::Uint64(s) => s as *const u64 as _,
124 Self::Int64(s) => s as *const i64 as _,
125 Self::Object(s) => s.as_ptr() as _,
126 Self::Stringv(s) => s.as_ptr() as _,
127 }
128 }
129}