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
713impl 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+ }
0 commit comments