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
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::syntax::path::path_compare_str;
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::parse::{Parse, ParseStream};
use syn::{Attribute, Item, ItemMod, Result, Token, Visibility};

#[derive(Clone, PartialEq, Eq)]
// This warning is triggered when running clippy on crates that depend on cxx-qt-gen,
// but not when running clippy on cxx-qt-gen.
#[allow(clippy::large_enum_variant)]
pub enum CxxQtItem {
    /// A normal syntax item that we pass through
    Item(Item),
    /// A CXX module that we need to generate code for
    Cxx(ItemMod),
    /// A CxxQt module block that we need to parse and later generate code for
    CxxQt(ItemMod),
}

impl std::fmt::Debug for CxxQtItem {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            CxxQtItem::Item(v0) => {
                let mut formatter = formatter.debug_tuple("Item");
                formatter.field(v0);
                formatter.finish()
            }
            CxxQtItem::Cxx(v0) => {
                let mut formatter = formatter.debug_tuple("Cxx");
                formatter.field(v0);
                formatter.finish()
            }
            CxxQtItem::CxxQt(v0) => {
                let mut formatter = formatter.debug_tuple("CxxQt");
                formatter.field(v0);
                formatter.finish()
            }
        }
    }
}

impl Parse for CxxQtItem {
    fn parse(input: ParseStream) -> Result<Self> {
        // Fork and skip over the attributes as we want to read the next token
        let ahead = input.fork();
        let attributes = ahead.call(Attribute::parse_outer)?;

        // See if the next token is a mod
        ahead.parse::<Visibility>()?;
        ahead.parse::<Option<Token![unsafe]>>()?;

        if ahead.peek(Token![mod]) {
            for attribute in &attributes {
                if path_compare_str(&attribute.path, &["cxx", "bridge"]) {
                    return input.parse().map(CxxQtItem::Cxx);
                } else if path_compare_str(&attribute.path, &["cxx_qt", "bridge"]) {
                    return input.parse().map(CxxQtItem::CxxQt);
                }
            }
        }

        // Fallback to using normal Item
        input.parse().map(CxxQtItem::Item)
    }
}

impl ToTokens for CxxQtItem {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            CxxQtItem::Item(item) => {
                item.to_tokens(tokens);
            }
            CxxQtItem::Cxx(module) => {
                module.to_tokens(tokens);
            }
            CxxQtItem::CxxQt(module) => {
                module.to_tokens(tokens);
            }
        }
    }
}