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
extern crate std;

use prelude::*;
use model::ComCrate;
use syn;
use type_parser::*;

pub trait ForeignTypeHandler
{
    /// Gets the name for the 'ty'.
    fn get_name( &self, krate : &ComCrate, ty : &Ident ) -> String;

    /// Gets the COM type for a Rust type.
    fn get_ty<'a, 'b: 'a>(
        &self,
        ty : &'b syn::Type,
    ) -> Option< TypeInfo<'a> >;
}

pub struct CTypeHandler;

impl ForeignTypeHandler for CTypeHandler
{
    /// Tries to apply renaming to the name.
    fn get_name(
        &self,
        krate: &ComCrate,
        ident: &Ident,
    ) -> String
    {
        self.get_name_for_ty( krate, &ident.to_string() )
    }

    fn get_ty<'a, 'b: 'a>(
        &self,
        ty: &'b syn::Type,
    ) -> Option<TypeInfo<'a>>
    {
        ::type_parser::parse( ty )
    }
}

impl CTypeHandler
{
     fn get_name_for_ty(
        &self,
        krate : &ComCrate,
        ty_name : &str
    ) -> String
    {
        let itf = if let Some( itf ) = krate.interface_by_name( ty_name ) {
            itf
        } else {
            return ty_name.to_owned()
        };

        if itf.item_type() == ::utils::InterfaceType::Struct {
            format!( "I{}", ty_name )
        } else {
            ty_name.to_owned()
        }
    }
}