Skip to content

Commit 25eeadb

Browse files
committed
gio: Add builders for DBus introspection types
1 parent 48b262e commit 25eeadb

7 files changed

Lines changed: 368 additions & 2 deletions

File tree

gio/src/dbus_annotation_info.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::ffi::CString;
2+
use std::mem;
3+
4+
use glib::translate::*;
5+
use smallvec::SmallVec;
6+
7+
use crate::{DBusAnnotationInfo, ffi};
8+
9+
impl DBusAnnotationInfo {
10+
pub fn builder<'a>() -> DBusAnnotationInfoBuilder<'a> {
11+
DBusAnnotationInfoBuilder::default()
12+
}
13+
}
14+
15+
#[derive(Default)]
16+
pub struct DBusAnnotationInfoBuilder<'a> {
17+
key: Option<&'a str>,
18+
value: Option<&'a str>,
19+
annotations: SmallVec<[DBusAnnotationInfo; 2]>,
20+
}
21+
22+
impl<'a> DBusAnnotationInfoBuilder<'a> {
23+
/// Required
24+
pub fn key(mut self, key: &'a str) -> Self {
25+
self.key = Some(key);
26+
self
27+
}
28+
29+
/// Required
30+
pub fn value(mut self, value: &'a str) -> Self {
31+
self.value = Some(value);
32+
self
33+
}
34+
35+
pub fn annotations<I: IntoIterator<Item = DBusAnnotationInfo>>(
36+
mut self,
37+
annotations: I,
38+
) -> Self {
39+
self.annotations = annotations.into_iter().collect();
40+
self
41+
}
42+
43+
pub fn build(self) -> DBusAnnotationInfo {
44+
let key = self.value.expect("`key` should be set");
45+
let value = self.value.expect("`value` should be set");
46+
unsafe {
47+
let ptr = { glib::ffi::g_malloc0(mem::size_of::<ffi::GDBusAnnotationInfo>()) }
48+
as *mut ffi::GDBusAnnotationInfo;
49+
(*ptr).ref_count = 1;
50+
(*ptr).key = CString::new(key).unwrap().to_glib_full();
51+
(*ptr).value = CString::new(value).unwrap().to_glib_full();
52+
(*ptr).annotations = self.annotations.to_glib_full();
53+
from_glib_full(ptr)
54+
}
55+
}
56+
}

gio/src/dbus_arg_info.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::ffi::CString;
2+
use std::mem;
3+
4+
use glib::translate::*;
5+
use smallvec::SmallVec;
6+
7+
use crate::{DBusAnnotationInfo, DBusArgInfo, ffi};
8+
9+
impl DBusArgInfo {
10+
pub fn builder<'a>() -> DBusArgInfoBuilder<'a> {
11+
DBusArgInfoBuilder::default()
12+
}
13+
}
14+
15+
#[derive(Default)]
16+
pub struct DBusArgInfoBuilder<'a> {
17+
name: Option<&'a str>,
18+
signature: Option<&'a str>,
19+
annotations: SmallVec<[DBusAnnotationInfo; 2]>,
20+
}
21+
22+
impl<'a> DBusArgInfoBuilder<'a> {
23+
pub fn name<N: Into<Option<&'a str>>>(mut self, name: N) -> Self {
24+
self.name = name.into();
25+
self
26+
}
27+
28+
/// Required
29+
pub fn signature(mut self, signature: &'a str) -> Self {
30+
self.signature = Some(signature);
31+
self
32+
}
33+
34+
pub fn annotations<I: IntoIterator<Item = DBusAnnotationInfo>>(
35+
mut self,
36+
annotations: I,
37+
) -> Self {
38+
self.annotations = annotations.into_iter().collect();
39+
self
40+
}
41+
42+
pub fn build(self) -> DBusArgInfo {
43+
let signature = self.signature.expect("`signature` should be set");
44+
let name = self.name.map(|name| CString::new(name).unwrap());
45+
unsafe {
46+
let ptr = { glib::ffi::g_malloc0(mem::size_of::<ffi::GDBusArgInfo>()) }
47+
as *mut ffi::GDBusArgInfo;
48+
(*ptr).ref_count = 1;
49+
(*ptr).name = name.to_glib_full();
50+
(*ptr).signature = CString::new(signature).unwrap().to_glib_full();
51+
(*ptr).annotations = self.annotations.to_glib_full();
52+
from_glib_full(ptr)
53+
}
54+
}
55+
}

gio/src/dbus_interface_info.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::ffi::CStr;
3+
use std::ffi::{CStr, CString};
4+
use std::mem;
45

5-
use crate::DBusInterfaceInfo;
6+
use glib::translate::*;
7+
use smallvec::SmallVec;
8+
9+
use crate::{
10+
DBusAnnotationInfo, DBusInterfaceInfo, DBusMethodInfo, DBusPropertyInfo, DBusSignalInfo, ffi,
11+
};
612

713
impl DBusInterfaceInfo {
14+
pub fn builder<'a>() -> DBusInterfaceInfoBuilder<'a> {
15+
DBusInterfaceInfoBuilder::default()
16+
}
17+
818
pub fn name(&self) -> &str {
919
unsafe {
1020
let c_obj = self.as_ptr();
@@ -15,3 +25,58 @@ impl DBusInterfaceInfo {
1525
}
1626
}
1727
}
28+
29+
#[derive(Default)]
30+
pub struct DBusInterfaceInfoBuilder<'a> {
31+
name: Option<&'a str>,
32+
methods: SmallVec<[DBusMethodInfo; 4]>,
33+
signals: SmallVec<[DBusSignalInfo; 4]>,
34+
properties: SmallVec<[DBusPropertyInfo; 4]>,
35+
annotations: SmallVec<[DBusAnnotationInfo; 2]>,
36+
}
37+
38+
impl<'a> DBusInterfaceInfoBuilder<'a> {
39+
/// Required
40+
pub fn name(mut self, name: &'a str) -> Self {
41+
self.name = Some(name);
42+
self
43+
}
44+
45+
pub fn methods<I: IntoIterator<Item = DBusMethodInfo>>(mut self, methods: I) -> Self {
46+
self.methods = methods.into_iter().collect();
47+
self
48+
}
49+
50+
pub fn signals<I: IntoIterator<Item = DBusSignalInfo>>(mut self, args: I) -> Self {
51+
self.signals = args.into_iter().collect();
52+
self
53+
}
54+
55+
pub fn properties<I: IntoIterator<Item = DBusPropertyInfo>>(mut self, args: I) -> Self {
56+
self.properties = args.into_iter().collect();
57+
self
58+
}
59+
60+
pub fn annotations<I: IntoIterator<Item = DBusAnnotationInfo>>(
61+
mut self,
62+
annotations: I,
63+
) -> Self {
64+
self.annotations = annotations.into_iter().collect();
65+
self
66+
}
67+
68+
pub fn build(self) -> DBusInterfaceInfo {
69+
let name = self.name.expect("`name` should be set");
70+
unsafe {
71+
let ptr = { glib::ffi::g_malloc0(mem::size_of::<ffi::GDBusInterfaceInfo>()) }
72+
as *mut ffi::GDBusInterfaceInfo;
73+
(*ptr).ref_count = 1;
74+
(*ptr).name = CString::new(name).unwrap().to_glib_full();
75+
(*ptr).methods = self.methods.to_glib_full();
76+
(*ptr).signals = self.signals.to_glib_full();
77+
(*ptr).properties = self.properties.to_glib_full();
78+
(*ptr).annotations = self.annotations.to_glib_full();
79+
from_glib_full(ptr)
80+
}
81+
}
82+
}

gio/src/dbus_method_info.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::ffi::CString;
2+
use std::mem;
3+
4+
use glib::translate::*;
5+
use smallvec::SmallVec;
6+
7+
use crate::{DBusAnnotationInfo, DBusArgInfo, DBusMethodInfo, ffi};
8+
9+
impl DBusMethodInfo {
10+
pub fn builder<'a>() -> DBusMethodInfoBuilder<'a> {
11+
DBusMethodInfoBuilder::default()
12+
}
13+
}
14+
15+
#[derive(Default)]
16+
pub struct DBusMethodInfoBuilder<'a> {
17+
name: Option<&'a str>,
18+
in_args: SmallVec<[DBusArgInfo; 4]>,
19+
out_args: SmallVec<[DBusArgInfo; 2]>,
20+
annotations: SmallVec<[DBusAnnotationInfo; 2]>,
21+
}
22+
23+
impl<'a> DBusMethodInfoBuilder<'a> {
24+
/// Required
25+
pub fn name(mut self, name: &'a str) -> Self {
26+
self.name = Some(name);
27+
self
28+
}
29+
30+
pub fn in_args<I: IntoIterator<Item = DBusArgInfo>>(mut self, args: I) -> Self {
31+
self.in_args = args.into_iter().collect();
32+
self
33+
}
34+
35+
pub fn out_args<I: IntoIterator<Item = DBusArgInfo>>(mut self, args: I) -> Self {
36+
self.out_args = args.into_iter().collect();
37+
self
38+
}
39+
40+
pub fn annotations<I: IntoIterator<Item = DBusAnnotationInfo>>(
41+
mut self,
42+
annotations: I,
43+
) -> Self {
44+
self.annotations = annotations.into_iter().collect();
45+
self
46+
}
47+
48+
pub fn build(self) -> DBusMethodInfo {
49+
let name = self.name.expect("`name` should be set");
50+
unsafe {
51+
let ptr = { glib::ffi::g_malloc0(mem::size_of::<ffi::GDBusMethodInfo>()) }
52+
as *mut ffi::GDBusMethodInfo;
53+
(*ptr).ref_count = 1;
54+
(*ptr).name = CString::new(name).unwrap().to_glib_full();
55+
(*ptr).in_args = self.in_args.to_glib_full();
56+
(*ptr).out_args = self.out_args.to_glib_full();
57+
(*ptr).annotations = self.annotations.to_glib_full();
58+
from_glib_full(ptr)
59+
}
60+
}
61+
}

gio/src/dbus_property_info.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::ffi::CString;
2+
use std::mem;
3+
4+
use glib::translate::*;
5+
use smallvec::SmallVec;
6+
7+
use crate::{DBusAnnotationInfo, DBusPropertyInfo, DBusPropertyInfoFlags, ffi};
8+
9+
impl DBusPropertyInfo {
10+
pub fn builder<'a>() -> DBusPropertyInfoBuilder<'a> {
11+
DBusPropertyInfoBuilder::default()
12+
}
13+
}
14+
15+
#[derive(Default)]
16+
pub struct DBusPropertyInfoBuilder<'a> {
17+
name: Option<&'a str>,
18+
signature: Option<&'a str>,
19+
flags: Option<DBusPropertyInfoFlags>,
20+
annotations: SmallVec<[DBusAnnotationInfo; 2]>,
21+
}
22+
23+
impl<'a> DBusPropertyInfoBuilder<'a> {
24+
/// Required
25+
pub fn name(mut self, name: &'a str) -> Self {
26+
self.name = Some(name);
27+
self
28+
}
29+
30+
/// Required
31+
pub fn signature(mut self, signature: &'a str) -> Self {
32+
self.signature = Some(signature);
33+
self
34+
}
35+
36+
pub fn flags(mut self, flags: DBusPropertyInfoFlags) -> Self {
37+
self.flags = Some(flags);
38+
self
39+
}
40+
41+
pub fn annotations<I: IntoIterator<Item = DBusAnnotationInfo>>(
42+
mut self,
43+
annotations: I,
44+
) -> Self {
45+
self.annotations = annotations.into_iter().collect();
46+
self
47+
}
48+
49+
pub fn build(self) -> DBusPropertyInfo {
50+
let name = self.name.expect("`name` should be set");
51+
let signature = self.signature.expect("`name` should be set");
52+
let flags = self.flags.unwrap_or(DBusPropertyInfoFlags::NONE);
53+
unsafe {
54+
let ptr = { glib::ffi::g_malloc0(mem::size_of::<ffi::GDBusPropertyInfo>()) }
55+
as *mut ffi::GDBusPropertyInfo;
56+
(*ptr).ref_count = 1;
57+
(*ptr).name = CString::new(name).unwrap().to_glib_full();
58+
(*ptr).signature = CString::new(signature).unwrap().to_glib_full();
59+
(*ptr).flags = flags.into_glib();
60+
(*ptr).annotations = self.annotations.to_glib_full();
61+
from_glib_full(ptr)
62+
}
63+
}
64+
}

gio/src/dbus_signal_info.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::ffi::CString;
2+
use std::mem;
3+
4+
use glib::translate::*;
5+
use smallvec::SmallVec;
6+
7+
use crate::{DBusAnnotationInfo, DBusArgInfo, DBusSignalInfo, ffi};
8+
9+
impl DBusSignalInfo {
10+
pub fn builder<'a>() -> DBusSignalInfoBuilder<'a> {
11+
DBusSignalInfoBuilder::default()
12+
}
13+
}
14+
15+
#[derive(Default)]
16+
pub struct DBusSignalInfoBuilder<'a> {
17+
name: Option<&'a str>,
18+
args: SmallVec<[DBusArgInfo; 4]>,
19+
annotations: SmallVec<[DBusAnnotationInfo; 2]>,
20+
}
21+
22+
impl<'a> DBusSignalInfoBuilder<'a> {
23+
/// Required
24+
pub fn name(mut self, name: &'a str) -> Self {
25+
self.name = Some(name);
26+
self
27+
}
28+
29+
pub fn args<I: IntoIterator<Item = DBusArgInfo>>(mut self, args: I) -> Self {
30+
self.args = args.into_iter().collect();
31+
self
32+
}
33+
34+
pub fn annotations<I: IntoIterator<Item = DBusAnnotationInfo>>(
35+
mut self,
36+
annotations: I,
37+
) -> Self {
38+
self.annotations = annotations.into_iter().collect();
39+
self
40+
}
41+
42+
pub fn build(self) -> DBusSignalInfo {
43+
let name = self.name.expect("`name` should be set");
44+
unsafe {
45+
let ptr = { glib::ffi::g_malloc0(mem::size_of::<ffi::GDBusSignalInfo>()) }
46+
as *mut ffi::GDBusSignalInfo;
47+
(*ptr).ref_count = 1;
48+
(*ptr).name = CString::new(name).unwrap().to_glib_full();
49+
(*ptr).args = self.args.to_glib_full();
50+
(*ptr).annotations = self.annotations.to_glib_full();
51+
from_glib_full(ptr)
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)