roopes_derive/lib.rs
1//! This crate implements macros supporting some
2//! patterns:
3//! - [`Builder`]
4//! - [`Visitor`]
5
6#![feature(proc_macro_internals)]
7#![feature(trait_alias)]
8#![feature(associated_type_bounds)]
9#![deny(
10 clippy::pedantic,
11 clippy::style,
12 clippy::correctness,
13 clippy::complexity
14)]
15
16use proc_macro::TokenStream;
17
18mod builder;
19mod common;
20mod publisher_subscriber;
21mod visitor;
22
23/// Creates a new type on the specified `struct`,
24/// which allows fields to be set one at a time.
25/// The new builder type's name is the specified
26/// type, appended with "Builder".
27///
28/// # Examples
29/// ``` rust
30/// #[macro_use]
31/// use roopes::prelude::*;
32///
33/// #[derive(Builder)]
34/// struct TestObj
35/// {
36/// field: i32,
37/// }
38///
39/// let builder = TestObjBuilder::new().set_field(10);
40///
41/// let test_obj = builder.build();
42///
43/// assert_eq!(test_obj.field, 10);
44/// ```
45#[proc_macro_derive(Builder)]
46pub fn derive_builder(input: TokenStream) -> TokenStream
47{
48 builder::derive(input)
49}
50
51/// Creates a new trait for user code to implement
52/// on the specified `enum`. This trait requires
53/// implementors to implement handlers for all the
54/// specified variants in the given enum.
55///
56/// # Examples
57/// ``` rust
58/// #[macro_use]
59/// use roopes::prelude::*;
60///
61/// #[derive(Visitor)]
62/// enum TestEnum
63/// {
64/// Integer
65/// {
66/// value: i32,
67/// },
68/// Nothing,
69/// }
70///
71/// struct Implementor;
72/// impl TestEnumVisitor for Implementor
73/// {
74/// fn visit_integer(
75/// &self,
76/// value: &i32,
77/// )
78/// {
79/// println!("got {value}");
80/// }
81///
82/// fn visit_nothing(&self)
83/// {
84/// println!("got Nothing");
85/// }
86/// }
87///
88/// let test_visitor = TestEnumAcceptor::new(Implementor);
89/// test_visitor.accept(&TestEnum::Integer { value: 10 });
90/// test_visitor.accept(&TestEnum::Nothing);
91/// ```
92#[proc_macro_derive(Visitor)]
93pub fn derive_visitor(input: TokenStream) -> TokenStream
94{
95 visitor::derive(input)
96}
97
98/// Creates a Publisher and Subscriber for the given message type.
99#[proc_macro_derive(PubSub)]
100pub fn derive_pubsub(input: TokenStream) -> TokenStream
101{
102 publisher_subscriber::derive(input)
103}
104
105#[test]
106fn macro_tests()
107{
108 let t = trybuild::TestCases::new();
109 t.compile_fail("src/*/test/*_fail.rs");
110 t.pass("src/*/test/*_pass.rs");
111}