orchestra_proc_macro/
lib.rs

1// Copyright (C) 2021 Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15#![allow(clippy::all)]
16#![deny(unused_crate_dependencies)]
17#![deny(unused_imports)]
18#![deny(unused_import_braces)]
19
20use proc_macro2::{Ident, Span, TokenStream};
21use syn::{parse_quote, spanned::Spanned, Path};
22
23mod graph;
24mod impl_builder;
25mod impl_channels_out;
26mod impl_message_wrapper;
27mod impl_orchestra;
28mod impl_subsystem_ctx_sender;
29mod orchestra;
30mod parse;
31mod subsystem;
32
33#[cfg(test)]
34mod tests;
35
36use impl_builder::*;
37use impl_channels_out::*;
38use impl_message_wrapper::*;
39use impl_orchestra::*;
40use impl_subsystem_ctx_sender::*;
41use parse::*;
42
43use self::{orchestra::*, subsystem::*};
44
45/// Obtain the support crate `Path` as `TokenStream`.
46pub(crate) fn support_crate() -> Result<Path, proc_macro_crate::Error> {
47	Ok(if cfg!(test) {
48		parse_quote! {crate}
49	} else {
50		use proc_macro_crate::{crate_name, FoundCrate};
51		let crate_name = crate_name("orchestra")?;
52		match crate_name {
53			FoundCrate::Itself => parse_quote! {crate},
54			FoundCrate::Name(name) => Ident::new(&name, Span::call_site()).into(),
55		}
56	})
57}
58
59#[proc_macro_attribute]
60pub fn orchestra(
61	attr: proc_macro::TokenStream,
62	item: proc_macro::TokenStream,
63) -> proc_macro::TokenStream {
64	let attr: TokenStream = attr.into();
65	let item: TokenStream = item.into();
66	impl_orchestra_gen(attr, item)
67		.unwrap_or_else(|err| err.to_compile_error())
68		.into()
69}
70
71#[proc_macro_attribute]
72pub fn subsystem(
73	attr: proc_macro::TokenStream,
74	item: proc_macro::TokenStream,
75) -> proc_macro::TokenStream {
76	let attr: TokenStream = attr.into();
77	let item: TokenStream = item.into();
78	impl_subsystem_context_trait_bounds(attr, item, MakeSubsystem::ImplSubsystemTrait)
79		.unwrap_or_else(|err| err.to_compile_error())
80		.into()
81}
82
83#[proc_macro_attribute]
84pub fn contextbounds(
85	attr: proc_macro::TokenStream,
86	item: proc_macro::TokenStream,
87) -> proc_macro::TokenStream {
88	let attr: TokenStream = attr.into();
89	let item: TokenStream = item.into();
90	impl_subsystem_context_trait_bounds(attr, item, MakeSubsystem::AddContextTraitBounds)
91		.unwrap_or_else(|err| err.to_compile_error())
92		.into()
93}