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
use ::prelude::*;
use super::*;
use super::macros::*;
use ::guid::GUID;
use ::syn::{Ident, Visibility};
intercom_attribute!(
ComStructAttr< ComStructAttrParam, Ident > {
clsid : StrOption,
}
);
#[derive(Debug, PartialEq)]
pub struct ComStruct
{
name : Ident,
clsid : Option<GUID>,
visibility : Visibility,
interfaces : Vec<Ident>,
}
impl ComStruct
{
pub fn parse(
crate_name : &str,
attr_params : TokenStream,
item : &str,
) -> ParseResult< ComStruct >
{
let item : ::syn::ItemStruct = ::syn::parse_str( item )
.map_err( |_| ParseError::ComStruct(
"<Unknown>".into(),
"Item syntax error".into() ) )?;
Self::from_ast( crate_name, attr_params, &item )
}
pub fn from_ast(
crate_name : &str,
attr_params : TokenStream,
item : &::syn::ItemStruct,
) -> ParseResult< ComStruct >
{
let attr : ComStructAttr = ::syn::parse2( attr_params )
.map_err( |e| ParseError::ComStruct(
item.ident.to_string(),
format!( "Attribute syntax error: {}", e ) ) )?;
let clsid_attr = attr.clsid()
.map_err( |msg| ParseError::ComStruct(
item.ident.to_string(), msg ) )?;
let clsid = match clsid_attr {
None => Some( ::utils::generate_clsid(
crate_name, &item.ident.to_string() ) ),
Some( StrOption::Str( clsid ) ) =>
Some( GUID::parse( &clsid.value() )
.map_err( |_| ParseError::ComStruct(
item.ident.to_string(),
"Bad CLSID format".into() ) )? ),
Some( StrOption::None ) => None,
};
let interfaces = attr.args().into_iter().cloned().collect();
Ok( ComStruct {
name: item.ident.clone(),
visibility: item.vis.clone(),
clsid,
interfaces,
} )
}
pub fn name( &self ) -> &Ident { &self.name }
pub fn clsid( &self ) -> &Option<GUID> { &self.clsid }
pub fn visibility( &self ) -> &::syn::Visibility { &self.visibility }
pub fn interfaces( &self ) -> &[Ident] { &self.interfaces }
}
#[cfg(test)]
mod test
{
use super::*;
#[test]
fn parse_com_class() {
let cls = ComStruct::parse(
"not used",
quote!( clsid = "12345678-1234-1234-1234-567890ABCDEF", Foo, Bar ),
"struct S;" )
.expect( "com_class attribute parsing failed" );
assert_eq!( cls.name(), "S" );
assert_eq!( cls.clsid(), &Some(
GUID::parse( "12345678-1234-1234-1234-567890ABCDEF" ).unwrap() ) );
assert_eq!( cls.interfaces().len(), 2 );
assert_eq!( cls.interfaces()[0], "Foo" );
assert_eq!( cls.interfaces()[1], "Bar" );
}
#[test]
fn parse_com_class_with_auto_guid() {
let cls = ComStruct::parse(
"not used",
quote!( MyStruct, IThings, IStuff ),
"struct MyStruct { a: u32 }" )
.expect( "com_class attribute parsing failed" );
assert_eq!( cls.name(), "MyStruct" );
assert_eq!( cls.clsid(), &Some(
GUID::parse( "28F57CBA-6AF4-3D3F-7C55-1CF1394D5C7A" ).unwrap() ) );
assert_eq!( cls.interfaces().len(), 3 );
assert_eq!( cls.interfaces()[0], "MyStruct" );
assert_eq!( cls.interfaces()[1], "IThings" );
assert_eq!( cls.interfaces()[2], "IStuff" );
}
#[test]
fn parse_com_class_with_no_data() {
let cls = ComStruct::parse(
"not used",
quote!( clsid = None ),
"struct EmptyType;" )
.expect( "com_class attribute parsing failed" );
assert_eq!( cls.name(), "EmptyType" );
assert_eq!( cls.clsid(), &None );
assert_eq!( cls.interfaces().len(), 0 );
}
#[test]
fn parse_com_class_with_no_guid_with_interface() {
let cls = ComStruct::parse(
"not used",
quote!( clsid = None, ITestInterface ),
"struct EmptyType;" )
.expect( "com_class attribute parsing failed" );
assert_eq!( cls.name(), "EmptyType" );
assert_eq!( cls.clsid(), &None );
assert_eq!( cls.interfaces().len(), 1 );
}
}