aboutsummaryrefslogtreecommitdiff
path: root/tosin-macros/src
diff options
context:
space:
mode:
Diffstat (limited to 'tosin-macros/src')
-rw-r--r--tosin-macros/src/lib.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/tosin-macros/src/lib.rs b/tosin-macros/src/lib.rs
index f9d8fc0..9c1f650 100644
--- a/tosin-macros/src/lib.rs
+++ b/tosin-macros/src/lib.rs
@@ -148,6 +148,34 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream {
} else {
panic!("not on a struct");
};
+ let app_name = {
+ // ugly hack because span::module_path() doesn't exist
+ let call_site = proc_macro::Span::call_site();
+ let call_site_file = call_site.source_file();
+ let call_site_path = call_site_file.path();
+ if !call_site_file.is_real() {
+ panic!("call site does not have a real path");
+ }
+
+ let app_dir = if call_site_path.ends_with("models.rs") {
+ call_site_path.parent().unwrap()
+ } else {
+ todo!("not in models.rs what is this")
+ };
+
+ // oh this is so fucking disgusting
+ if app_dir.starts_with("examples") {
+ app_dir
+ .components()
+ .skip(1) // drop the "examples/", keep the example name
+ .map(|c| c.as_os_str().to_string_lossy())
+ .collect::<Vec<_>>()
+ .join("::")
+ } else {
+ todo!("what's a {}", call_site_path.display())
+ }
+ };
+ let real_table_name = format!("{}-{}", app_name, lowercase_name);
let real_db_types: Vec<_> = ast_data
.fields
.iter()
@@ -277,6 +305,7 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream {
// this means users need #[macro_use] extern crate diesel; but fuck doing it ourselves
table! {
+ #[sql_name = #real_table_name]
#lowercase_name {
#(#diesel_columns,)*
}