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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use super::utils;
use syn::*;
use quote::Tokens;
pub trait TyHandler {
fn rust_ty( &self ) -> Ty;
fn com_ty( &self ) -> Ty
{
self.rust_ty()
}
fn com_to_rust(
&self, ident : &Ident
) -> Tokens
{
quote!( #ident.into() )
}
fn rust_to_com(
&self, ident : &Ident
) -> Tokens
{
quote!( #ident.into() )
}
fn default_value( &self) -> Tokens
{
match self.rust_ty() {
Ty::Path( _, ref p ) => {
let name : &str = p.segments.last().unwrap().ident.as_ref();
match name {
"c_void"
| "RawComPtr"
| "ComRc"
=> quote!( ::std::ptr::null_mut() ),
_ => quote!( Default::default() )
}
},
_ => quote!( Default::default() )
}
}
}
struct IdentityParam( Ty );
impl TyHandler for IdentityParam {
fn rust_ty( &self ) -> Ty { self.0.clone() }
}
struct ComRcParam( Ty );
impl TyHandler for ComRcParam {
fn rust_ty( &self ) -> Ty { self.0.clone() }
fn com_ty( &self ) -> Ty
{
parse_type( "::intercom::RawComPtr" ).unwrap()
}
fn rust_to_com( &self, ident : &Ident ) -> Tokens
{
let comrc_params = match self.0 {
Ty::Path( _, ref p ) => {
let last_segment = &p.segments.last().unwrap();
match last_segment.parameters {
PathParameters::AngleBracketed( ref data ) => data,
_ => panic!( "ComRc doesn't have <> params" ),
}
}
_ => unreachable!( "ComRcParam should only be used for Ty::Path" ),
};
let itf_ty = match comrc_params.types.first() {
Some( ty ) => ty,
_ => panic!( "ComRc doesn't have type parameters" ),
};
let itf_ident = match utils::get_ty_ident( itf_ty ) {
Some( ty_ident ) => ty_ident,
_ => panic!( "Could not resolve name of {:?}", itf_ty ),
};
let iid_ident = super::idents::iid( itf_ident );
quote!( ::intercom::ComRc::query_interface( &#ident, &#iid_ident )
.expect( "ComRc<T> does not support interface T" ) )
}
}
struct StringParam( Ty );
impl TyHandler for StringParam
{
fn rust_ty( &self ) -> Ty { self.0.clone() }
fn com_ty( &self ) -> Ty
{
parse_type( "::intercom::BStr" ).unwrap()
}
fn com_to_rust( &self, ident : &Ident ) -> Tokens
{
quote!( #ident.into() )
}
fn rust_to_com( &self, ident : &Ident ) -> Tokens
{
quote!( #ident.into() )
}
}
pub fn get_ty_handler(
arg_ty : &Ty,
) -> Box<TyHandler>
{
let ty = arg_ty.clone();
match *arg_ty {
Ty::Path( .., ref p ) => {
let name : &str = p.segments.last().unwrap().ident.as_ref();
match name {
"ComRc" => Box::new( ComRcParam( ty ) ),
"String" => Box::new( StringParam( ty ) ),
_ => Box::new( IdentityParam( ty ) )
}
},
_ => Box::new( IdentityParam( ty ) )
}
}