potk_macros/lib.rs
1mod add_assign;
2mod command;
3mod enum_code;
4mod enum_int;
5mod legacy;
6mod perms;
7
8use proc_macro::TokenStream;
9
10/// Example:
11/// ```ignore
12/// #[derive(Debug, Default, potk::Command)]
13/// enum MyCommands {
14/// #[default]
15/// Help,
16/// Run,
17/// Master { gene: shah::models::Gene }
18/// }
19/// ```
20#[proc_macro_derive(Command)]
21pub fn command(code: TokenStream) -> TokenStream {
22 command::command(code)
23}
24
25#[proc_macro_derive(PotkAddAssign)]
26pub fn add_assign(code: TokenStream) -> TokenStream {
27 let item = syn::parse_macro_input!(code as syn::ItemStruct);
28 add_assign::add_assign(item)
29 .unwrap_or_else(syn::Error::into_compile_error)
30 .into()
31}
32
33/// Example:
34/// ```
35/// #[derive(Debug, potk_macros::EnumCode)]
36/// #[enum_code(u8)]
37/// pub enum Schema {
38/// Model(String), // 0u8
39/// Array { len: usize }, // 1u8
40/// U8, // 2u8
41/// Gene // 3u8
42/// }
43/// ```
44#[proc_macro_derive(EnumCode, attributes(enum_code))]
45pub fn enum_code(code: TokenStream) -> TokenStream {
46 enum_code::enum_code(code)
47}
48
49/// Example:
50/// ```ignore
51/// #[potk::legacy]
52/// mod items {
53///
54/// /// the derive macros from **Base** are set for all children
55/// #[derive(Debug, Serialize, Deserialize, ToSchema)]
56/// pub struct Base {
57/// gene: Gene,
58/// is_alive: bool,
59/// }
60///
61/// impl From<&Review> for Base {
62/// fn from(value: &Review) -> Self {
63/// Self {
64/// gene: value.gene,
65/// is_alive: value.is_alive(),
66/// }
67/// }
68/// }
69///
70/// // child 1
71/// pub struct ReviewInfo {
72/// // gene: Gene,
73/// // is_alive: bool,
74/// user: Gene,
75/// }
76///
77/// impl From<&Review> for ReviewInfo {
78/// fn from(value: &Review) -> Self {
79/// Self { user: value.user }
80/// }
81/// }
82///
83/// // child 2
84/// pub struct EateryReviewInfo {
85/// // gene: Gene,
86/// // is_alive: bool,
87/// user: Option<EateryReviewUserInfo>,
88/// }
89///
90/// impl From<&Review> for EateryReviewInfo {
91/// fn from(review: &Review) -> Self {
92/// Self { user: None }
93/// }
94/// }
95/// }
96/// ```
97#[proc_macro_attribute]
98pub fn legacy(_args: TokenStream, code: TokenStream) -> TokenStream {
99 let item = syn::parse_macro_input!(code as syn::ItemMod);
100 legacy::legacy(item).unwrap_or_else(syn::Error::into_compile_error).into()
101}
102
103/// enum_ini is a two way conversion enum `<->` u16
104/// default **start** is `0` and default **ty** is `u8`
105/// Example:
106/// ```
107/// #[potk_macros::enum_int(u16)]
108/// #[derive(Debug, Default, Clone, Copy)]
109/// pub enum ExampleError {
110/// #[default]
111/// Unknown = 0,
112/// UserNotFound,
113/// BadPhone,
114/// BadStr,
115/// }
116/// ```
117#[proc_macro_attribute]
118pub fn enum_int(args: TokenStream, code: TokenStream) -> TokenStream {
119 let item = syn::parse_macro_input!(code as syn::ItemEnum);
120 enum_int::enum_int(item, args)
121 .unwrap_or_else(syn::Error::into_compile_error)
122 .into()
123}
124
125/// perms is for using an array of `u8` for booleans
126/// ```ignore
127/// pub mod my_perms {
128/// potk::perms! {
129/// VIEW_USERS, ADD_USERS, EDIT_USERS, DEL_USERS
130/// }
131/// }
132///
133/// let mut user_perms = [0u8; 32];
134/// assert_eq!(user_perms.perm_get(my_perms::VIEW_USERS), false);
135/// user_perms.perm_set(my_perms::VIEW_USERS, true);
136/// assert_eq!(user_perms.perm_get(my_perms::VIEW_USERS), true);
137/// ```
138#[proc_macro]
139pub fn perms(code: TokenStream) -> TokenStream {
140 perms::perms(code)
141}
142
143fn crate_ident() -> syn::Ident {
144 // let found_crate = crate_name("potk").unwrap();
145 // let name = match &found_crate {
146 // FoundCrate::Itself => "potk",
147 // FoundCrate::Name(name) => name,
148 // };
149 ident!("potk")
150}
151
152macro_rules! ident {
153 ($name:literal) => {
154 syn::Ident::new($name, proc_macro2::Span::call_site())
155 };
156 ($name:expr) => {
157 syn::Ident::new($name, proc_macro2::Span::call_site())
158 };
159}
160pub(crate) use ident;
161
162macro_rules! err {
163 ($span:expr, $($msg:literal),*) => {
164 Err(syn::Error::new($span, concat!( $($msg),* )))
165 };
166 ($span:expr, $msg:expr) => {
167 Err(syn::Error::new($span, $msg))
168 };
169}
170
171pub(crate) use err;