orchestra_proc_macro/
orchestra.rs

1// Copyright (C) 2022 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
16use proc_macro2::TokenStream;
17use syn::{parse2, Result};
18
19use super::{parse::*, *};
20
21pub(crate) fn impl_orchestra_gen(
22	attr: TokenStream,
23	orig: TokenStream,
24) -> Result<proc_macro2::TokenStream> {
25	let args: OrchestraAttrArgs = parse2(attr)?;
26	let message_wrapper = args.message_wrapper;
27
28	let of: OrchestraGuts = parse2(orig)?;
29
30	let support_crate = support_crate().expect("The crate this macro is run for, includes the proc-macro support as dependency, otherwise it could not be run in the first place. qed");
31	let info = OrchestraInfo {
32		support_crate,
33		subsystems: of.subsystems,
34		baggage: of.baggage,
35		orchestra_name: of.name,
36		message_wrapper,
37		message_channel_capacity: args.message_channel_capacity,
38		signal_channel_capacity: args.signal_channel_capacity,
39		extern_event_ty: args.extern_event_ty,
40		extern_signal_ty: args.extern_signal_ty,
41		extern_error_ty: args.extern_error_ty,
42		outgoing_ty: args.outgoing_ty,
43		boxed_messages: args.boxed_messages,
44	};
45
46	let mut additive = impl_orchestra_struct(&info);
47	additive.extend(impl_builder(&info));
48
49	additive.extend(impl_orchestrated_subsystem(&info));
50	additive.extend(impl_channels_out_struct(&info));
51	additive.extend(impl_subsystem_types_all(&info)?);
52
53	additive.extend(impl_message_wrapper_enum(&info)?);
54
55	let ts = expander::Expander::new("orchestra-expansion")
56		.add_comment("Generated orchestra code by `#[orchestra(..)]`".to_owned())
57		.dry(!cfg!(feature = "expand"))
58		.verbose(true)
59		// once all our needed format options are available on stable
60		// we should enabled this again, until then too many warnings
61		// are generated
62		// .fmt(expander::Edition::_2021)
63		.write_to_out_dir(additive)
64		.expect("Expander does not fail due to IO in OUT_DIR. qed");
65
66	Ok(ts)
67}