lib.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. use proc_macro::TokenStream;
  2. use quote::quote;
  3. mod entity;
  4. mod index;
  5. mod modelable;
  6. fn parse_microrm_ref(attrs: &[syn::Attribute]) -> proc_macro2::TokenStream {
  7. for attr in attrs {
  8. if attr.path.segments.is_empty() {
  9. continue;
  10. }
  11. if attr.tokens.is_empty() && attr.path.segments.last().unwrap().ident == "microrm_internal"
  12. {
  13. return quote! { crate };
  14. }
  15. }
  16. quote! { ::microrm }
  17. }
  18. /// Turns a serializable/deserializable struct into a microrm entity model.
  19. ///
  20. /// There are two important visible effects:
  21. /// - Provides an implementation of `microrm::entity::Entity`
  22. /// - Defines a <struct-name>Columns enum
  23. ///
  24. /// Note that names are converted from CamelCase to snake_case and vice versa
  25. /// where applicable, so a struct named `TestModel` is given a table name `test_model`
  26. /// and a struct field named `field_name` is given a variant name of `FieldName`.
  27. ///
  28. /// The `#[microrm...]` attributes can be used to control the derivation somewhat.
  29. /// The following are understood for the Entity struct:
  30. /// - `#[microrm_internal]`: this is internal to the microrm crate (of extremely limited usefulness
  31. /// outside the microrm library)
  32. ///
  33. /// The following are understood on individual fields:
  34. /// - `#[microrm_foreign]`: this is a foreign key (and the field must be of a type implementing `EntityID`)
  35. #[proc_macro_derive(Entity, attributes(microrm_internal, microrm_foreign))]
  36. pub fn derive_entity(tokens: TokenStream) -> TokenStream {
  37. entity::derive(tokens)
  38. }
  39. /// Marks a struct or enum as able to be directly used in an Entity to correspond to a single database column.
  40. #[proc_macro_derive(Modelable, attributes(microrm_internal))]
  41. pub fn derive_modelable(tokens: TokenStream) -> TokenStream {
  42. modelable::derive(tokens)
  43. }
  44. /// Defines a struct to represent a optionally-unique index on a table.
  45. ///
  46. /// Suppose the following `Entity` definition is used:
  47. ///
  48. /// ```ignore
  49. /// #[derive(Entity,Serialize,Deserialize)]
  50. /// struct SystemUser {
  51. /// username: String,
  52. /// hashed_password: String
  53. /// }
  54. /// ```
  55. ///
  56. /// We can now use `make_index!` to define an index on the username field:
  57. /// ```ignore
  58. /// make_index!(SystemUsernameIndex, SystemUserColumns::Username)
  59. /// ```
  60. ///
  61. /// This index can be made unique by adding a `!` prior to the type name, as:
  62. /// ```ignore
  63. /// make_index!(!SystemUsernameUniqueIndex, SystemUserColumns::Username)
  64. /// ```
  65. #[proc_macro]
  66. pub fn make_index(tokens: TokenStream) -> TokenStream {
  67. index::do_make_index(tokens, quote! { ::microrm })
  68. }
  69. /// For internal use inside the microrm library. See `make_index`.
  70. #[proc_macro]
  71. pub fn make_index_internal(tokens: TokenStream) -> TokenStream {
  72. index::do_make_index(tokens, quote! { crate })
  73. }
  74. /*#[proc_macro]
  75. pub fn query(tokens: TokenStream) -> TokenStream {
  76. query::do_query(tokens)
  77. }
  78. */