lib.rs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //! This crate contains procedural macros to simplify the use of the `microrm` crate.
  2. use proc_macro::TokenStream;
  3. mod database;
  4. mod entity;
  5. mod index;
  6. mod value;
  7. /// `Entity` trait derivation procedural macro.
  8. ///
  9. /// This macro performs most of the heavy lifting, type-system wise, of the entire microrm library:
  10. /// - Derives `Entity` for the given structure,
  11. /// - Defines and derives `EntityPart` for each field in the source structure,
  12. /// - Defines an `EntityID` type,
  13. /// - Derives a `std::fmt::Debug` implementation for the structure that handles some of the
  14. /// `microrm::schema` types nicely.
  15. ///
  16. /// The prerequisites for this macro are:
  17. /// - Must be applied on a struct with named fields,
  18. /// - All fields of the given struct must derive the `Datum` trait.
  19. ///
  20. /// Three attributes, applied to fields, modify the behaviour of the generation as follows:
  21. /// - The `unique` attribute causes the resulting sqlite table to attach a unique constraint for
  22. /// the given column. Note that this is a restriction that is applied per-column, not over all
  23. /// columns tagged with `#[unique]`.
  24. /// - The `elide` attribute removes the datum field from several end-user-visible iterations, such
  25. /// as the `Debug` impl. Useful for hiding fields that hold secret information that shouldn't be
  26. /// visible in log files.
  27. /// - The `key` attribute adds a field to the search key defined for this entity, for smoother
  28. /// access. This attribute can be applied to multiple fields, and the key-uniqueness constraint is
  29. /// applied across the tuple of all columns with the attribute. Note that this is not _quite_ the
  30. /// primary key for the table, as internally the integral ID is used as the primary key; it can be
  31. /// thought of as the 'secondary key' for the table.
  32. ///
  33. /// For example:
  34. ///
  35. /// ```ignore
  36. /// #[derive(Entity)]
  37. /// struct ExampleEntity {
  38. /// /// UUID for this entity
  39. /// #[unique]
  40. /// id: String,
  41. ///
  42. /// /// Stored email address, part of lookup key
  43. /// #[key]
  44. /// email: String,
  45. ///
  46. /// /// Stored name, part of lookup key
  47. /// #[key]
  48. /// name: String,
  49. ///
  50. /// /// Sensitive data that should never be in log files
  51. /// #[elide]
  52. /// sin: String,
  53. /// }
  54. /// ```
  55. #[proc_macro_derive(Entity, attributes(unique, elide, key))]
  56. pub fn derive_entity(tokens: TokenStream) -> TokenStream {
  57. entity::derive(tokens)
  58. }
  59. /// `Database` trait derivation procedural macro.
  60. ///
  61. /// This macro sets up some helper types required for implementing the `Database` trait, in
  62. /// addition to actually implementing the `Database` trait itself.
  63. ///
  64. /// The prerequisites for this macro are:
  65. /// - Must be applied on a struct with named fields,
  66. /// - All fields of the given struct must derive the `DatabaseItem` trait.
  67. ///
  68. /// Refer to the `microrm` examples for example usage.
  69. #[proc_macro_derive(Database)]
  70. pub fn derive_database(tokens: TokenStream) -> TokenStream {
  71. database::derive(tokens)
  72. }
  73. /// Index columns specification macro.
  74. ///
  75. /// This macro uses the type indirection set up in microrm to take a list of struct fields, such as
  76. /// `SomeStruct::field_a, SomeStruct::field_b`, and converts it to an EntityPartList for the
  77. /// relevant entity.
  78. #[proc_macro]
  79. pub fn index_cols(tokens: TokenStream) -> TokenStream {
  80. index::index_cols(tokens)
  81. }
  82. /// Value specification
  83. #[proc_macro_derive(Value)]
  84. pub fn value(tokens: TokenStream) -> TokenStream {
  85. value::derive(tokens)
  86. }