1 | use super::*; |
2 | |
3 | #[derive (Copy, Clone, Debug, Hash, PartialEq, Eq)] |
4 | pub struct TypeName(pub &'static str, pub &'static str); |
5 | |
6 | impl Ord for TypeName { |
7 | fn cmp(&self, other: &Self) -> Ordering { |
8 | // Type names are sorted before namespaces. The `Type` sort order depends on this. |
9 | // This is more efficient in general as many types typically share a namespace. |
10 | (self.1, self.0).cmp(&(other.1, other.0)) |
11 | } |
12 | } |
13 | |
14 | impl PartialOrd for TypeName { |
15 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
16 | Some(self.cmp(other)) |
17 | } |
18 | } |
19 | |
20 | impl TypeName { |
21 | pub const Object: Self = Self("System" , "Object" ); |
22 | pub const GUID: Self = Self("System" , "Guid" ); |
23 | pub const Type: Self = Self("System" , "Type" ); |
24 | pub const IsConst: Self = Self("System.Runtime.CompilerServices" , "IsConst" ); |
25 | |
26 | pub const HResult: Self = Self("Windows.Foundation" , "HResult" ); |
27 | pub const EventRegistrationToken: Self = Self("Windows.Foundation" , "EventRegistrationToken" ); |
28 | |
29 | pub const IAsyncAction: Self = Self("Windows.Foundation" , "IAsyncAction" ); |
30 | pub const IAsyncActionWithProgress: Self = |
31 | Self("Windows.Foundation" , "IAsyncActionWithProgress" ); |
32 | pub const IAsyncOperation: Self = Self("Windows.Foundation" , "IAsyncOperation" ); |
33 | pub const IAsyncOperationWithProgress: Self = |
34 | Self("Windows.Foundation" , "IAsyncOperationWithProgress" ); |
35 | |
36 | pub const Matrix3x2: Self = Self("Windows.Foundation.Numerics" , "Matrix3x2" ); |
37 | pub const Matrix4x4: Self = Self("Windows.Foundation.Numerics" , "Matrix4x4" ); |
38 | |
39 | pub const IIterable: Self = Self("Windows.Foundation.Collections" , "IIterable" ); |
40 | pub const IIterator: Self = Self("Windows.Foundation.Collections" , "IIterator" ); |
41 | |
42 | pub const PWSTR: Self = Self("Windows.Win32.Foundation" , "PWSTR" ); |
43 | pub const PSTR: Self = Self("Windows.Win32.Foundation" , "PSTR" ); |
44 | pub const BSTR: Self = Self("Windows.Win32.Foundation" , "BSTR" ); |
45 | pub const HRESULT: Self = Self("Windows.Win32.Foundation" , "HRESULT" ); |
46 | pub const CHAR: Self = Self("Windows.Win32.Foundation" , "CHAR" ); |
47 | pub const BOOL: Self = Self("Windows.Win32.Foundation" , "BOOL" ); |
48 | pub const BOOLEAN: Self = Self("Windows.Win32.Foundation" , "BOOLEAN" ); |
49 | pub const NTSTATUS: Self = Self("Windows.Win32.Foundation" , "NTSTATUS" ); |
50 | pub const WIN32_ERROR: Self = Self("Windows.Win32.Foundation" , "WIN32_ERROR" ); |
51 | pub const RPC_STATUS: Self = Self("Windows.Win32.System.Rpc" , "RPC_STATUS" ); |
52 | |
53 | pub const D2D_MATRIX_3X2_F: Self = |
54 | Self("Windows.Win32.Graphics.Direct2D.Common" , "D2D_MATRIX_3X2_F" ); |
55 | pub const D3DMATRIX: Self = Self("Windows.Win32.Graphics.Direct3D" , "D3DMATRIX" ); |
56 | pub const IUnknown: Self = Self("Windows.Win32.System.Com" , "IUnknown" ); |
57 | pub const HSTRING: Self = Self("Windows.Win32.System.WinRT" , "HSTRING" ); |
58 | pub const IInspectable: Self = Self("Windows.Win32.System.WinRT" , "IInspectable" ); |
59 | |
60 | pub const VARIANT: Self = Self("Windows.Win32.System.Variant" , "VARIANT" ); |
61 | pub const PROPVARIANT: Self = Self("Windows.Win32.System.Com.StructuredStorage" , "PROPVARIANT" ); |
62 | |
63 | pub fn parse(full_name: &'static str) -> Self { |
64 | let index = full_name |
65 | .rfind('.' ) |
66 | .expect("Expected full name separated with `.`" ); |
67 | Self(&full_name[0..index], &full_name[index + 1..]) |
68 | } |
69 | |
70 | pub fn namespace(&self) -> &'static str { |
71 | self.0 |
72 | } |
73 | |
74 | pub fn name(&self) -> &'static str { |
75 | self.1 |
76 | } |
77 | |
78 | pub fn write(&self, writer: &Writer, generics: &[Type]) -> TokenStream { |
79 | let name = to_ident(self.name()); |
80 | let namespace = writer.write_namespace(*self); |
81 | |
82 | if generics.is_empty() { |
83 | quote! { #namespace #name } |
84 | } else { |
85 | let generics = generics.iter().map(|ty| ty.write_name(writer)); |
86 | quote! { #namespace #name < #(#generics),* > } |
87 | } |
88 | } |
89 | } |
90 | |
91 | impl std::fmt::Display for TypeName { |
92 | fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
93 | write!(fmt, " {}. {}" , self.0, self.1) |
94 | } |
95 | } |
96 | |