aboutsummaryrefslogtreecommitdiff
path: root/tosin-macros
diff options
context:
space:
mode:
authorMelody Horn / boringcactus <melody@boringcactus.com>2021-06-14 20:54:57 -0600
committerMelody Horn / boringcactus <melody@boringcactus.com>2021-06-14 20:54:57 -0600
commit92bf14bb7cc0c10f67a9a67e7512138c15db6ec0 (patch)
tree4a18d50fe97f41671e4b4fd82483bc9c49333d70 /tosin-macros
parent6bcb3620d8be2f3bcfe72432d17955827894bb6e (diff)
downloadtosin-92bf14bb7cc0c10f67a9a67e7512138c15db6ec0.tar.gz
tosin-92bf14bb7cc0c10f67a9a67e7512138c15db6ec0.zip
start working on migrations
Diffstat (limited to 'tosin-macros')
-rw-r--r--tosin-macros/Cargo.toml2
-rw-r--r--tosin-macros/src/lib.rs31
2 files changed, 32 insertions, 1 deletions
diff --git a/tosin-macros/Cargo.toml b/tosin-macros/Cargo.toml
index 4a58a20..836461c 100644
--- a/tosin-macros/Cargo.toml
+++ b/tosin-macros/Cargo.toml
@@ -10,5 +10,5 @@ edition = "2018"
proc-macro = true
[dependencies]
-syn = "1.0"
+syn = { version = "1.0", features = ["full", "extra-traits"] }
quote = "1.0"
diff --git a/tosin-macros/src/lib.rs b/tosin-macros/src/lib.rs
index c0651fa..7efbac6 100644
--- a/tosin-macros/src/lib.rs
+++ b/tosin-macros/src/lib.rs
@@ -1,3 +1,5 @@
+#![feature(proc_macro_span)]
+
extern crate proc_macro;
use proc_macro::TokenStream;
@@ -24,3 +26,32 @@ fn impl_model(ast: &syn::DeriveInput) -> TokenStream {
};
gen.into()
}
+
+#[proc_macro]
+pub fn gather_migrations(_input: TokenStream) -> TokenStream {
+ 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 migrations_dir = call_site_path.parent().unwrap();
+ let migrations: Vec<syn::Ident> = migrations_dir.read_dir()
+ .unwrap()
+ .map(Result::unwrap)
+ .map(|x| x.path().file_stem().unwrap().to_string_lossy().into_owned())
+ .filter(|x| x != "mod")
+ .map(|x| syn::parse_str(&x).unwrap())
+ .collect();
+
+ let gen = quote! {
+ #( mod #migrations; )*
+
+ pub const migrations: &[Migration] = &[
+ #(#migrations::MIGRATION),*
+ ];
+ };
+
+ gen.into()
+}