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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
//! This crate provides `domain_patterns` derive macros. //! //! # Entity macro //! The `Entity` derive macro can be used to automatically implement all methods of the `Entity` trait //! from the `domain_patterns` crate. This only works if certain preconditions are met: //! //! 1. You are applying this to a struct. //! 2. Your struct has an `id` field of type `Uuid`. //! 3. Your struct has a `version` field which is some integer type. //! //! ```edition2018 //! #[macro_use] //! extern crate domain_derive; //! use uuid::Uuid; //! //! #[derive(Entity)] //! struct User { //! id: Uuid, //! version: u64 //! }; //! ``` //! //! # ValueSetup macro //! The `ValueSetup` derive macro can be used to setup as much boilerplate as possible //! for your choosen value object. It checks some preconditions: //! //! 1. You are applying this to a struct. //! 2. Your struct has a single field called `value` of any type that is clonable. //! //! Once you've used this macro, you will still need to implement the `ValueObject` trait, //! but you will not have to implement `TryFrom` (or create the validation error for `TryFrom`, this //! is handled by the macro), or implement `PartialEq` or `Clone`. //! //! In case you need to use the validation error elsewhere, the created validation error will be the //! name of your struct with ValidationError appended. For example, if you have an `Email` struct, //! then the generated validation error will be called `EmailValidationError`. //! //! ```edition2018 //! #[macro_use] //! extern crate domain_derive; //! //! use domain_patterns::ValueObject; //! use regex::Regex; //! //! #[derive(ValueSetup)] //! pub struct Email { //! pub value: String, //! } //! //! impl ValueObject<String> for Email { //! fn validate(value: &String) -> bool { //! let email_rx = Regex::new( //! r"^(?i)[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$" //! ).unwrap(); //! //! email_rx.is_match(value) //! } //! //! fn value(&self) -> &String { //! return &self.value; //! } //! } //! ``` //! //! # DomainEvent macro //! The `DomainEvent` macro should be applied to a struct that represents a DomainEvent. It completely //! implements all methods of the `DomainEvent` trait, as long as some preconditions are met: //! //! 1. You are applying this to a struct. //! 2. There needs to be an `id` field of type `Uuid`. //! 3. There needs to be a version field of any integer type (floating point not allowed). //! 4. There needs to be an `aggregate_id` field of type `Uuid`. //! 5. There needs to be an `occurred` field of type `i64`. //! //! ```edition2018 //! #[macro_use] //! extern crate domain_derive; //! //! use uuid::Uuid; //! use domain_patterns::event::DomainEvent; //! //! #[derive(Serialize, Clone, DomainEvent)] //! pub struct FirstNameUpdatedEvent { //! pub aggregate_id: Uuid, //! pub first_name: String, //! pub version: u64, //! pub id: Uuid, //! pub occurred: i64, //! } //! ``` //! //! # DomainEvents macro //! The `DomainEvents` macro should be applied to an enum that holds variants which are all Domain Events. //! This is a very thin wrapper, and all the macro does is check that the structure is an enum, and then applies //! the trait, which has no methods. //! //! ```edition2018 //! #[macro_use] //! extern crate domain_derive; //! //! use uuid::Uuid; //! use domain_patterns::event::{DomainEvent, DomainEvents}; //! //! #[derive(Serialize, Clone, DomainEvent)] //! pub struct FirstNameUpdatedEvent { //! pub id: Uuid, //! pub aggregate_id: String, //! pub first_name: String, //! pub version: u64, //! pub occurred: i64, //! } //! //! #[derive(Clone, DomainEvents)] //! pub enum UserEvents { //! FirstNameUpdated(FirstNameUpdatedEvent), //! } //! ``` #![recursion_limit = "128"] extern crate proc_macro; #[macro_use] extern crate quote; #[macro_use] extern crate syn; mod entity; mod value_object; mod domain_event; mod domain_events; mod type_checks; use crate::proc_macro::TokenStream; use syn::DeriveInput; use syn::spanned::Spanned; use crate::domain_events::create_inner_match_for_getter; /// The `Entity` derive macro can be used to automatically implement all methods of the `Entity` trait /// from the `domain_patterns` crate. This only works if certain preconditions are met: /// /// 1. You are applying this to a struct. /// 2. Your struct has an `id` field of type `Uuid`. /// 3. Your struct has a `version` field which is some integer type. /// /// ```edition2018 /// #[macro_use] /// extern crate domain_derive; /// use uuid::Uuid; /// /// #[derive(Entity)] /// struct User { /// id: Uuid, /// version: u64 /// }; /// ``` #[proc_macro_derive(Entity)] pub fn entity_derive(input: TokenStream) -> TokenStream { let input: DeriveInput = parse_macro_input!(input as DeriveInput); entity::precondition(&input).expect("Entity procedural macro failed preconditions"); // Struct name let name = &input.ident; let mut streams = vec![]; streams.push(quote! { impl domain_patterns::models::Entity for #name { fn id(&self) -> String { self.id.to_string() } } impl std::cmp::PartialEq for #name { fn eq(&self, other: &Self) -> bool { self.id == other.id } } }); let getters = entity::produce_getters(&input).expect("Entity macro failed to produce getters"); streams.push(getters); let expanded = quote! { #(#streams)* }; TokenStream::from(expanded) } /// The `ValueSetup` derive macro can be used to setup as much boilerplate as possible /// for your choosen value object. It checks some preconditions: /// /// 1. You are applying this to a struct. /// 2. Your struct has a single field called `value` of any type that is clonable. /// /// Once you've used this macro, you will still need to implement the `ValueObject` trait, /// but you will not have to implement `TryFrom` (or create the validation error for `TryFrom`, this /// is handled by the macro), or implement `PartialEq` or `Clone`. /// /// In case you need to use the validation error elsewhere, the created validation error will be the /// name of your struct with ValidationError appended. For example, if you have an `Email` struct, /// then the generated validation error will be called `EmailValidationError`. /// /// ```edition2018 /// #[macro_use] /// extern crate domain_derive; /// /// use domain_patterns::ValueObject; /// use regex::Regex; /// /// #[derive(ValueSetup)] /// pub struct Email { /// pub value: String, /// } /// /// impl ValueObject<String> for Email { /// // This error would be the root error for the crate, using failure most likely. /// type ValueError = Error; /// /// fn validate(value: &String) -> Result<(), Self::ValueError> { /// let email_rx = Regex::new( /// r"^(?i)[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$" /// ).unwrap(); /// /// if !email_rx.is_match(value) { /// return Err(EmailValidationError) /// } /// /// Ok(()) /// } /// /// fn value(&self) -> &String { /// return &self.value; /// } /// } /// ``` #[proc_macro_derive(ValueSetup)] pub fn value_object_derive(input: TokenStream) -> TokenStream { let input: DeriveInput = parse_macro_input!(input as DeriveInput); // Struct name let name = &input.ident; let name_str = name.to_string(); value_object::precondition(&input).expect("ValueSetup macro failed preconditions"); // safe to unwrap because we check for existence of value field in precondition. let type_name = &value_object::value_type_name(&input.data).unwrap(); // TODO: Likely remove this, as we use a parent enum now which is predicatably named. // let error_struct_name = &value_object::error_name_from_type(name, input.span()); let expanded = quote! { impl std::fmt::Display for #name { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{}", self.value) } } impl std::cmp::PartialEq for #name { fn eq(&self, other: &Self) -> bool { self.value == other.value } } impl std::clone::Clone for #name { fn clone(&self) -> Self { #name { value: self.value.clone() } } } impl std::convert::TryFrom<#type_name> for #name { type Error = crate::Error; fn try_from(value: #type_name) -> std::result::Result<Self, Self::Error> { Self::validate(&value)?; Ok(#name { value, }) } } }; TokenStream::from(expanded) } /// The `DomainEvent` macro should be applied to a struct that represents a DomainEvent. It completely /// implements all methods of the `DomainEvent` trait, as long as some preconditions are met: /// /// 1. You are applying this to a struct. /// 2. There needs to be an `id` field of type `Uuid`. /// 3. There needs to be a version field of any integer type (floating point not allowed). /// 4. There needs to be an `aggregate_id` field of type `Uuid`. /// 5. There needs to be an `occurred` field of type `i64`. /// /// ```edition2018 /// #[macro_use] /// extern crate domain_derive; /// /// use uuid::Uuid; /// use domain_patterns::event::DomainEvent; /// /// #[derive(Serialize, Clone, DomainEvent)] /// pub struct FirstNameUpdatedEvent { /// pub id: Uuid, /// pub aggregate_id: String, /// pub first_name: String, /// pub version: u64, /// pub occurred: i64, /// } /// ``` #[proc_macro_derive(DomainEvent)] pub fn domain_event_derive(input: TokenStream) -> TokenStream { let input: DeriveInput = parse_macro_input!(input as DeriveInput); // Struct name let name = &input.ident; domain_event::precondition(&input).expect("DomainEvent macro failed preconditions"); let expanded = quote! { impl DomainEvent for #name { fn occurred(&self) -> i64 { self.occurred } fn id(&self) -> String { self.id.to_string() } fn aggregate_id(&self) -> String { self.aggregate_id.clone() } fn version(&self) -> u64 { self.version as u64 } } impl Message for #name {} }; TokenStream::from(expanded) } /// The `DomainEvents` macro should be applied to an enum that holds variants which are all Domain Events. /// This is a very thin wrapper, and all the macro does is check that the structure is an enum, and then applies /// the trait, which has no methods. /// /// ```edition2018 /// #[macro_use] /// extern crate domain_derive; /// /// use uuid::Uuid; /// use domain_patterns::event::{DomainEvent, DomainEvents}; /// /// #[derive(Serialize, Clone, DomainEvent)] /// pub struct FirstNameUpdatedEvent { /// pub id: Uuid, /// pub aggregate_id: String, /// pub first_name: String, /// pub version: u64, /// pub occurred: i64, /// } /// /// #[derive(Clone, DomainEvents)] /// pub enum UserEvents { /// FirstNameUpdated(FirstNameUpdatedEvent), /// } /// ``` #[proc_macro_derive(DomainEvents)] pub fn domain_events_derive(input: TokenStream) -> TokenStream { let input: DeriveInput = parse_macro_input!(input as DeriveInput); // Struct name let name = &input.ident; domain_events::precondition(&input).expect("DomainEvents macro failed preconditions"); // Get match statements for each function, so we automatically call function of inner variant. let occurred_match = create_inner_match_for_getter(&input, "occurred".to_string()); let id_match = create_inner_match_for_getter(&input, "id".to_string()); let aggregate_id_match = create_inner_match_for_getter(&input, "aggregate_id".to_string()); let version_match = create_inner_match_for_getter(&input, "version".to_string()); let expanded = quote! { impl DomainEvent for #name { fn occurred(&self) -> i64 { #occurred_match } fn id(&self) -> String { #id_match } fn aggregate_id(&self) -> String { #aggregate_id_match } fn version(&self) -> u64 { #version_match } } impl Message for #name {} }; TokenStream::from(expanded) } /// The `Command` derive macro can be used to automatically implement the Command and Message marker traits /// from the `domain_patterns` crate. #[proc_macro_derive(Command)] pub fn command_derive(input: TokenStream) -> TokenStream { let input: DeriveInput = parse_macro_input!(input as DeriveInput); // No precondition checks that I can think of since these are just simple marker traits // Struct name let name = &input.ident; let expanded = quote! { impl Command for #name {} impl Message for #name {} }; TokenStream::from(expanded) } /// The `Query` derive macro can be used to automatically implement the Query marker trait /// from the `domain_patterns` crate. #[proc_macro_derive(Query)] pub fn query_derive(input: TokenStream) -> TokenStream { let input: DeriveInput = parse_macro_input!(input as DeriveInput); // No precondition checks that I can think of since these are just simple marker traits // Struct name let name = &input.ident; let expanded = quote! { impl Query for #name {} }; TokenStream::from(expanded) }