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
// Copyright (c) 2017 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

//! Macros for [`vulkano`].
//!
//! [`vulkano`]: https://crates.io/crates/vulkano

use proc_macro::TokenStream;
use proc_macro_crate::{crate_name, FoundCrate};
use syn::{parse_macro_input, DeriveInput, Error};

mod derive_buffer_contents;
mod derive_vertex;

/// Derives the [`Vertex`] trait.
///
/// [`Vertex`]: https://docs.rs/vulkano/latest/vulkano/pipeline/graphics/vertex_input/trait.Vertex.html
#[proc_macro_derive(Vertex, attributes(name, format))]
pub fn derive_vertex(input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as DeriveInput);

    derive_vertex::derive_vertex(ast)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

/// Derives the [`BufferContents`] trait.
///
/// [`BufferContents`]: https://docs.rs/vulkano/latest/vulkano/buffer/trait.BufferContents.html
#[proc_macro_derive(BufferContents)]
pub fn derive_buffer_contents(input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as DeriveInput);

    derive_buffer_contents::derive_buffer_contents(ast)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

fn crate_ident() -> syn::Ident {
    let found_crate = crate_name("vulkano").unwrap();
    let name = match &found_crate {
        // We use `vulkano` by default as we are exporting crate as vulkano in vulkano/lib.rs.
        FoundCrate::Itself => "vulkano",
        FoundCrate::Name(name) => name,
    };

    syn::Ident::new(name, proc_macro2::Span::call_site())
}

macro_rules! bail {
    ($msg:expr $(,)?) => {
        return Err(syn::Error::new(proc_macro2::Span::call_site(), $msg))
    };
    ($span:expr, $msg:expr $(,)?) => {
        return Err(syn::Error::new_spanned($span, $msg))
    };
}
use bail;