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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431

use ::prelude::*;
use std::rc::Rc;
use syn::*;

use ast_converters::*;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Direction { In, Out, Retval }

pub struct TypeConversion {

    /// Possible temporary values that need to be kept alive for the duration
    /// of the conversion result usage.
    pub temporary: Option<TokenStream>,

    /// Conversion result value. Possibly referencing the temporary value.
    pub value : TokenStream,
}

#[derive(PartialEq, Eq, Debug, Hash)]
pub struct ModelTypeSystemConfig {
    pub effective_system : ModelTypeSystem,
    pub is_default : bool,
}

impl ModelTypeSystemConfig {
    pub fn get_unique_name( &self, base : &str ) -> String {
        match self.is_default {
            true => base.to_string(),
            false => format!( "{}_{:?}", base, self.effective_system ),
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
pub enum ModelTypeSystem {

    /// COM Automation compatible type system.
    Automation,

    /// Raw type system.
    Raw,
}

impl ModelTypeSystem {

    /// Converts the model type system into public type system tokens.
    pub fn as_typesystem_tokens( self ) -> TokenStream {
        match self {
            ModelTypeSystem::Automation =>
                    quote!( ::intercom::TypeSystem::Automation ),
            ModelTypeSystem::Raw =>
                    quote!( ::intercom::TypeSystem::Raw ),
        }
    }
}

/// Type usage context.
pub struct TypeContext {
    type_system: ModelTypeSystem,
}

impl TypeContext {
    pub fn new( type_system : ModelTypeSystem ) -> TypeContext {
        TypeContext { type_system }
    }
}

/// Defines Type-specific logic for handling the various parameter types in the
/// Rust/COM interface.
pub trait TypeHandler {

    /// The Rust type.
    fn rust_ty( &self ) -> Type;

    /// The COM type.
    fn com_ty( &self, _dir: Direction ) -> Type
    {
        self.rust_ty()
    }

    /// Converts a COM parameter named by the ident into a Rust type.
    fn com_to_rust(
        &self,
        ident : &Ident,
        _dir: Direction,
    ) -> TypeConversion
    {
        TypeConversion {
            temporary: None,
            value: quote!( #ident.into() ),
        }
    }

    /// Converts a Rust parameter named by the ident into a COM type.
    fn rust_to_com(
        &self, ident : &Ident, _dir: Direction
    ) -> TypeConversion
    {
        TypeConversion {
            temporary: None,
            value: quote!( #ident.into() )
        }
    }

    /// Gets the default value for the type.
    fn default_value( &self ) -> TokenStream
    {
        match self.rust_ty() {
            Type::Path( ref p ) => {
                let ident = p.path.get_ident().unwrap();
                let name = ident.to_string();
                match name.as_ref() {
                    "RawComPtr" => quote!( ::std::ptr::null_mut() ),
                    _ => quote!( Default::default() )
                }
            },
            Type::Ptr( .. ) => quote!( ::std::ptr::null_mut() ),
            _ => quote!( Default::default() )
        }
    }
}

/// Identity parameter handler.
///
/// No special logic.
struct IdentityParam( Type );

impl TypeHandler for IdentityParam {
    fn rust_ty( &self ) -> Type { self.0.clone() }
}


/// `ComItf` parameter handler. Supports `ComItf` Rust type and ensures the this
/// to/from `RawComPtr` COM type.
struct ComItfParam { ty: Type, context: TypeContext }

impl TypeHandler for ComItfParam {

    fn rust_ty( &self ) -> Type { self.ty.clone() }

    /// The COM type.
    fn com_ty( &self, _dir: Direction ) -> Type
    {
        let rust_ty = self.rust_ty();

        // Extract the interface type T from the ComItf<T> type definition
        let itf_ty = match rust_ty {
            syn::Type::Path( path ) =>
                match path.path.segments.last().unwrap().value().arguments {
                    syn::PathArguments::AngleBracketed( ref ab ) =>
                            match ab.args.last().unwrap().value() {
                                syn::GenericArgument::Type( ref t ) => t.clone(),
                                _ => panic!( "ComItf generic argument must be type" ),
                            },
                    _ => panic!( "ComItf type parameter must be angle bracketed" ),
                },
            _ => panic!( "ComItf type parameter must be a type path" ),
        };

        // Construct the final InterfacePtr<T> type.
        parse_quote!( ::intercom::raw::InterfacePtr< #itf_ty > )
    }

    fn default_value( &self ) -> TokenStream
    {
        quote!( ::intercom::raw::InterfacePtr::new( ::std::ptr::null_mut() ) )
    }

    /// Converts a COM parameter named by the ident into a Rust type.
    fn com_to_rust(
        &self, ident : &Ident, _dir: Direction
    ) -> TypeConversion
    {
        let ts = self.context.type_system.as_typesystem_tokens();
        TypeConversion {
            temporary: None,
            value: quote!( ::intercom::ComItf::wrap( #ident, #ts ) ),
        }
    }

    /// Converts a Rust parameter named by the ident into a COM type.
    fn rust_to_com(
        &self, ident : &Ident, _dir: Direction
    ) -> TypeConversion
    {
        let ts = self.context.type_system.as_typesystem_tokens();
        TypeConversion {
            temporary: None,
            value: quote!( ::intercom::ComItf::ptr( &#ident.into(), #ts ) )
        }
    }
}

/// `bool` parameter handler. Supports `VARIANT_BOOL` for automation type system.
struct BoolParam { context: TypeContext }

impl TypeHandler for BoolParam {

    fn rust_ty( &self ) -> Type { parse_quote!( bool ) }

    /// The COM type.
    fn com_ty( &self, _dir: Direction ) -> Type
    {
        match self.context.type_system {
            ModelTypeSystem::Automation => parse_quote!( ::intercom::raw::VariantBool ),
            ModelTypeSystem::Raw => parse_quote!( bool ),
        }
    }

    fn default_value( &self ) -> TokenStream
    {
        match self.context.type_system {
            ModelTypeSystem::Automation => quote!( false.into() ),
            ModelTypeSystem::Raw => quote!( false ),
        }
    }

    /// Converts a COM parameter named by the ident into a Rust type.
    fn com_to_rust(
        &self, ident : &Ident, _dir: Direction
    ) -> TypeConversion
    {
        TypeConversion {
            temporary: None,
            value: quote!( #ident.into() )
        }
    }

    /// Converts a Rust parameter named by the ident into a COM type.
    fn rust_to_com(
        &self, ident : &Ident, _dir: Direction
    ) -> TypeConversion
    {
        TypeConversion {
            temporary: None,
            value: quote!( #ident.into() )
        }
    }
}

/// `Variant` parameter handler. Supports `VARIANT` for automation type system.
struct VariantParam { ty: Type }

impl TypeHandler for VariantParam {

    fn rust_ty( &self ) -> Type { self.ty.clone() }

    /// The COM type.
    fn com_ty( &self, _dir: Direction ) -> Type
    {
        parse_quote!( ::intercom::raw::Variant )
    }

    /// Converts a COM parameter named by the ident into a Rust type.
    fn com_to_rust(
        &self, ident : &Ident, _dir: Direction
    ) -> TypeConversion
    {
        TypeConversion {
            temporary: None,
            value: quote!( #ident.into() )
        }
    }

    /// Converts a Rust parameter named by the ident into a COM type.
    fn rust_to_com(
        &self, ident : &Ident, _dir: Direction
    ) -> TypeConversion
    {
        TypeConversion {
            temporary: None,
            value: quote!( #ident.com_into()? )
        }
    }
}

/// String parameter handler. Converts between Rust String and COM BSTR types.
struct StringParam { ty: Type, context: TypeContext }
impl TypeHandler for StringParam
{
    fn rust_ty( &self ) -> Type { self.ty.clone() }

    fn com_ty( &self, dir: Direction ) -> Type
    {
        match self.context.type_system {
            ModelTypeSystem::Automation => match dir {
                Direction::In => parse_quote!( ::intercom::raw::InBSTR ),
                Direction::Out | Direction::Retval => parse_quote!( ::intercom::raw::OutBSTR ),
            },
            ModelTypeSystem::Raw => match dir {
                Direction::In => parse_quote!( ::intercom::raw::InCStr ),
                Direction::Out | Direction::Retval => parse_quote!( ::intercom::raw::OutCStr ),
            },
        }
    }

    fn com_to_rust( &self, ident : &Ident, dir: Direction ) -> TypeConversion
    {
        match dir {

            Direction::In => {

                let str_type = match self.context.type_system {
                    ModelTypeSystem::Automation => quote!( BStr ),
                    ModelTypeSystem::Raw => quote!( CStr ),
                };

                let target_ty = self.rust_ty();
                let intermediate_ty = quote!( &::intercom::#str_type );
                let to_intermediate = quote!( ::intercom::#str_type::from_ptr( #ident ) );
                let as_trait = quote!( < #target_ty as ::intercom::FromWithTemporary< #intermediate_ty > > );

                let temp_ident = Ident::new( &format!( "__{}_temporary", ident.to_string() ), Span::call_site() );
                TypeConversion {
                    temporary: Some( quote!( let mut #temp_ident = #as_trait::to_temporary( #to_intermediate )?; ) ),
                    value: quote!( #as_trait::from_temporary( &mut #temp_ident )? ),
                }
            },
            Direction::Out | Direction::Retval => {

                // The type system input 'ident' should represent either a BString or CString
                // depending on the type system.
                let str_type = match self.context.type_system {
                    ModelTypeSystem::Automation => quote!( BString::from_ptr ),
                    ModelTypeSystem::Raw => quote!( CString::from_raw ),
                };

                // Get the type system string as Rust string.
                let ts_string = quote!( ::intercom::#str_type( #ident ) );

                // Convert the TS string into whatever string type the method requires.
                TypeConversion {
                    temporary: None,
                    value: quote!( #ts_string.com_into()? ),
                }
            },
        }
    }

    fn rust_to_com( &self, ident : &Ident, dir: Direction ) -> TypeConversion
    {
        match dir {

            Direction::In => {

                let str_type = match self.context.type_system {
                    ModelTypeSystem::Automation => quote!( BStr ),
                    ModelTypeSystem::Raw => quote!( CStr ),
                };

                let target_ty = self.rust_ty();
                let intermediate_ty = quote!( &::intercom::#str_type );
                let as_trait = quote!( < #intermediate_ty as ::intercom::FromWithTemporary< #target_ty > > );

                let temp_ident = Ident::new( &format!( "__{}_temporary", ident.to_string() ), Span::call_site() );
                TypeConversion {
                    temporary: Some( quote!( let mut #temp_ident = #as_trait::to_temporary( #ident )?; ) ),
                    value: quote!( #as_trait::from_temporary( &mut #temp_ident )?.as_ptr() ),
                }
            },
            Direction::Out | Direction::Retval => {

                // The Rust string value `ident` must be first converted into a type system
                // compatible Rust string, either BString or CSTring.
                // depending on the type system.
                let str_type = match self.context.type_system {
                    ModelTypeSystem::Automation => quote!( BString ),
                    ModelTypeSystem::Raw => quote!( CString ),
                };

                // Convert the `ident` into the required string type.
                let ts_string = quote!( ::intercom::ComInto::< ::intercom::#str_type >::com_into( #ident )? );

                TypeConversion {
                    temporary: None,
                    value: match self.context.type_system {
                        ModelTypeSystem::Automation =>
                            quote!( #ts_string.into_ptr() ),
                        ModelTypeSystem::Raw =>
                            quote!( #ts_string.into_raw() ),
                    }
                }
            },
        }
    }

    fn default_value( &self ) -> TokenStream
    {
        quote!( ::std::ptr::null_mut() )
    }
}

/// Resolves the `TypeHandler` to use.
pub fn get_ty_handler(
    arg_ty : &Type,
    context : TypeContext,
) -> Rc<dyn TypeHandler>
{
    let type_info = ::type_parser::parse( arg_ty )
            .unwrap_or_else( || panic!( "Type {:?} could not be parsed.", arg_ty ) );

    map_by_name(
            type_info.get_name().as_ref(), type_info.original.clone(),
            context )
}

/// Selects type handler based on the name of the type.
fn map_by_name(
    name: &str,
    original_type: Type,
    context: TypeContext,
) -> Rc<dyn TypeHandler> {

    match name {

        "ComItf" => Rc::new( ComItfParam { ty: original_type, context } ),
        "CString" | "CStr" | "BString" | "BStr" | "String" | "str" =>
            Rc::new( StringParam { ty: original_type, context } ),
        "bool" =>
            Rc::new( BoolParam { context } ),
        "Variant" =>
            Rc::new( VariantParam { ty: original_type } ),
        // "str" => Rc::new( StringRefParam( original_type ) ),

        // Unknown. Use IdentityParam.
        _ => Rc::new( IdentityParam( original_type ) )
    }

}