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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
use message::{extract_externally_notifiable_event_req, render_event_visibility, render_message_token};
// use outbox::render_outbox_token;
use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput, ItemFn};
#[macro_use]
extern crate quote;
mod command;
mod construct;
mod domain;
mod generic_helpers;
mod handler;
mod message;
mod repository;
mod result;
mod utils;
#[proc_macro_derive(TEvent, attributes(internally_notifiable, externally_notifiable, identifier, aggregate))]
pub fn message_derive(attr: TokenStream) -> TokenStream {
let mut ast: DeriveInput = syn::parse(attr.clone()).unwrap();
let externally_notifiable_event_req = extract_externally_notifiable_event_req(&mut ast);
let visibilities = render_event_visibility(&ast);
render_message_token(&ast, visibilities, externally_notifiable_event_req).into()
}
/// Define TAggregate root
/// ## Example
/// ```rust,no_run
/// #[aggregate]
/// #[derive(Debug, Default, Serialize, Deserialize)]
/// pub struct TestAggregate {
/// pub(crate) age: i64,
/// }
///
/// fn test_aggregate() {
/// let aggregate = TestAggregate::default().set_age(1);
/// assert_eq!(aggregate.version, 0);
/// assert!(!aggregate.is_existing);
/// assert_eq!(aggregate.events.len(), 0);
/// assert_eq!(aggregate.age, 1)
/// }
/// ```
///
/// ```rust,no_run
/// #[aggregate]
/// #[derive(Debug, Default, Serialize, Deserialize)]
/// pub struct TestAggregate {
/// pub(crate) age: i64,
/// pub(crate) name: String,
/// }
/// ```
///
/// Likewise, not specifying `identifier` will also error out
/// ```rust,no_run
/// #[aggregate]
/// #[derive(Debug, Default, Serialize, Deserialize)]
/// pub struct TestAggregate {
/// pub(crate) age: i64,
/// pub(crate) name: String,
/// }
/// ```
///
/// `{your aggregate name}Adapter` will be generated automatically so you can use it to adapt it to database
/// ```rust,no_run```
///
/// #[aggregate]
/// #[derive(Debug, Clone, Serialize, Default)]
/// pub struct AggregateStruct {
/// #[adapter_ignore]
/// id: i32,
/// #[serde(skip_serializing)]
/// name: String,
/// some_other_field: i32,
/// }
/// let aggregate = AggregateStruct::default();
/// let serialized = serde_json::to_string(&aggregate).unwrap();
/// assert_eq!(serialized, "{\"id\":0,\"some_other_field\":0,\"version\":0}");
///
/// let adapter = AggregateStructAdapter::default();
/// let serialized = serde_json::to_string(&adapter).unwrap();
/// assert_eq!(serialized, "{\"some_other_field\":0}");
///
/// ```
///
/// Conversion is automatically done as follows:
/// ```rust,no_run```
/// let aggregate = AggregateStruct {
/// name: "migo".into(),
/// some_other_field: 2,
/// id: 1,
/// ..Default::default()
/// };
/// let converted_adapter = AggregateStructAdapter::from(aggregate);
/// assert_eq!(converted_adapter.name, "migo");
/// assert_eq!(converted_adapter.some_other_field, 2);
/// let converted_struct = AggregateStruct::from(converted_adapter);
/// assert_eq!(converted_struct.name, "migo");
/// assert_eq!(converted_struct.some_other_field, 2);
/// ```
///
/// Generic can also be used for aggregate:
/// ```rust,no_run
/// #[derive(Default, Debug, Serialize, Deserialize)]
/// struct Unset;
///
/// #[aggregate]
/// #[derive(Default, Debug, Serialize, Clone)]
/// struct MyStruct<T = Unset>
/// where
/// T: Send + Sync + Default + 'static,
/// {
/// name: String,
/// age: i32,
///
/// #[adapter_ignore]
/// sub_type: T,
/// }
///
/// impl MyStruct<String> {
/// fn do_something_with_string(&self) -> String {
/// self.sub_type.clone()
/// }
/// }
///
/// impl MyStruct<i32> {
/// fn do_something_with_i32(&self) -> i32 {
/// self.sub_type
/// }
/// }
///
/// let adapter = MyStructAdapter {
/// name: "hello".to_string(),
/// age: 10,
/// };
///
/// let _my_unset_struct = Into::<MyStruct>::into(adapter.clone()); // default type is set which has no method attached.
///
/// let my_string_struct = Into::<MyStruct<String>>::into(adapter.clone());
/// let my_int32_struct = Into::<MyStruct<i32>>::into(adapter.clone());
///
/// assert_eq!(my_string_struct.do_something_with_string(), String::default());
/// assert_eq!(my_int32_struct.do_something_with_i32(), i32::default());
///
/// ```
#[proc_macro_attribute]
pub fn aggregate(_: TokenStream, input: TokenStream) -> TokenStream {
domain::render_aggregate(input)
}
/// Define ApplicationResponse so that could be recognized by messagebus
/// ## Example
///
/// ```rust,no_run
/// #[derive(Debug, ApplicationResponse)]
/// enum ServiceResponse{
/// Response1
/// Response2
/// }
/// ```
#[proc_macro_derive(ApplicationResponse)]
pub fn response_derive(attr: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(attr.clone()).unwrap();
result::render_response_token(&ast)
}
/// Attribute macro for marking repository methods that collect events
/// ## Example
/// ```rust,no_run
///
/// #[aggregate]
/// #[derive(Default, Serialize, Deserialize)]
/// struct TestAggregate {
///
/// pub age: i64,
/// }
///
/// #[async_trait]
/// impl TRepository< TestAggregate> for SqlRepository<TestAggregate> {
/// fn new(executor: Arc<RwLock<SQLExecutor>>) -> Self {
/// ...
/// }
///
/// #[event_hook]
/// async fn update(
/// &mut self,
/// aggregate: &mut TestAggregate,
/// ) -> Result<(), BaseError> {
/// Ok(())
/// }
/// }
///
/// async fn test_event_hook() {
/// '_given: {
/// let mut repo = SqlRepository::new(SQLExecutor::new());
/// let mut aggregate = TestAggregate::default().set_age(64);
/// aggregate.raise_event(SomeEvent { id: aggregate.age }.to_message());
///
/// '_when: {
/// let _ = repo.update(&mut aggregate).await;
/// let events = repo.get_events();
///
/// '_then: {
/// assert!(!events.is_empty())
/// }
/// }
/// }
/// }
/// ```
#[proc_macro_attribute]
pub fn event_hook(_: TokenStream, input: TokenStream) -> TokenStream {
let ast: ItemFn = syn::parse_macro_input!(input as ItemFn);
message::event_hook(ast).into()
}
/// Define a Application Error type that can be used in the ruva.
///
/// Before deriving this, you must impl `Debug`traits.
///
/// This macro can be only used in enum.
///
/// ## Attributes
///
/// - `#[crates(...)]` - Specify the name of root of ruva crate. (Default is `ruva`)
/// - `#[stop_sentinel]` - Specify the error matching for `BaseError::StopSentinel`.
/// - `#[stop_sentinel_with_event]` - Specify the error matching for `BaseError::StopSentinelWithEvent`.
/// - `#[database_error]` - Specify the error matching for `BaseError::DatabaseError`.
///
/// ## Example
/// ```rust,no_run
/// #[derive(Debug, ApplicationError)]
/// #[crates(crate::imports::ruva)]
/// enum TestError {
/// #[stop_sentinel]
/// Stop,
/// #[stop_sentinel_with_event]
/// StopWithEvent(Box<AnyError>),
/// #[database_error]
/// DatabaseError(Box<AnyError>),
/// }
/// ```
#[proc_macro_derive(ApplicationError, attributes(stop_sentinel, stop_sentinel_with_event, database_error, crates))]
pub fn error_derive(attr: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(attr).unwrap();
result::render_error_token(&ast)
}
#[proc_macro_attribute]
pub fn entity(_: TokenStream, input: TokenStream) -> TokenStream {
domain::render_entity_token(input)
}
#[proc_macro_derive(TCommand)]
pub fn command_derive(attr: TokenStream) -> TokenStream {
command::declare_command(attr).into()
}
#[proc_macro_derive(IntoCommand, attributes(required_input))]
pub fn into_command_derive(attr: TokenStream) -> TokenStream {
let mut ast: DeriveInput = syn::parse(attr.clone()).unwrap();
let quote = command::derive_into_command(&mut ast);
quote!(
#quote
)
.into()
}
#[proc_macro_derive(TRepository)]
pub fn repository_derive(attr: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(attr.clone()).unwrap();
repository::render_repository_token(&ast)
}
// what if I want attribute to be #[ruva(except)]?
#[proc_macro_derive(TConstruct, attributes(except))]
pub fn derive_construct(input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as DeriveInput);
construct::expand_derive_construct(&mut input).unwrap_or_else(syn::Error::into_compile_error).into()
}