aboutsummaryrefslogtreecommitdiff
path: root/tosin-macros/src/lib.rs
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-06-30 19:55:47 -0600
committerMelody Horn <melody@boringcactus.com>2021-06-30 19:55:47 -0600
commit36b8d3d0a8c97726d4413ae005b36e6288119ed4 (patch)
treed52e5670cbf38e1fafdff2986543cae8495461ef /tosin-macros/src/lib.rs
parent85d53a3358819d15788e1196d110fb6fd7199c67 (diff)
downloadtosin-36b8d3d0a8c97726d4413ae005b36e6288119ed4.tar.gz
tosin-36b8d3d0a8c97726d4413ae005b36e6288119ed4.zip
use the table name matching what actually exists
Diffstat (limited to 'tosin-macros/src/lib.rs')
-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,)*
}