From 36b8d3d0a8c97726d4413ae005b36e6288119ed4 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 30 Jun 2021 19:55:47 -0600 Subject: use the table name matching what actually exists --- tosin-macros/src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'tosin-macros/src/lib.rs') 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::>() + .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,)* } -- cgit v1.2.3