intercom_common/
idents.rs

1use crate::prelude::*;
2use crate::tyhandlers::ModelTypeSystem;
3use syn::{Ident, Path};
4
5pub trait SomeIdent
6{
7    fn get_some_ident(&self) -> Option<Ident>;
8}
9
10impl SomeIdent for Path
11{
12    fn get_some_ident(&self) -> Option<Ident>
13    {
14        self.get_ident()
15            .cloned()
16            .or_else(|| self.segments.last().map(|l| l.ident.clone()))
17    }
18}
19
20pub fn vtable(itf: &Ident, ts: ModelTypeSystem) -> Path
21{
22    let vtable_ident = format_ident!("__{}{}VTable", itf, ts);
23    parse_quote!(#vtable_ident)
24}
25
26pub fn com_to_rust_method_impl(itf: &Ident, method: &Ident, ts: ModelTypeSystem) -> Ident
27{
28    Ident::new(&format!("__{}_{}_{:?}", itf, method, ts), method.span())
29}
30
31pub fn with_ts(ident: &Ident, ts: ModelTypeSystem) -> Ident
32{
33    Ident::new(&format!("{}_{:?}", ident, ts), Span::call_site())
34}
35
36pub fn clsid_path(struct_path: &Path) -> Path
37{
38    let mut clsid_path = struct_path.clone();
39    if let Some(mut last) = clsid_path.segments.last_mut() {
40        last.ident = clsid(&last.ident);
41    }
42    clsid_path
43}
44
45pub fn clsid(struct_name: &Ident) -> Ident
46{
47    new_ident(&format!("CLSID_{}", struct_name))
48}
49
50pub fn iid(itf_name: &Ident, span: Span) -> Ident
51{
52    Ident::new(&format!("IID_{}", itf_name), span)
53}
54
55pub fn method_impl<TMethod: std::fmt::Display>(
56    struct_ident: &Ident,
57    itf_ident: &Ident,
58    method_name: TMethod,
59    ts: ModelTypeSystem,
60) -> Ident
61{
62    new_ident(&format!(
63        "__{}_{}_{}_{:?}",
64        struct_ident, itf_ident, method_name, ts
65    ))
66}
67
68fn new_ident(s: &str) -> Ident
69{
70    Ident::new(s, Span::call_site())
71}