Last active
October 21, 2024 02:13
-
-
Save ravenclaw900/b2f8326ed35f30bd8e027a71361f2d9c to your computer and use it in GitHub Desktop.
Migration Macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! build_migration_chain { | |
($type:ident = $ver:literal) => { | |
impl $crate::Migrate for $type { | |
type From = Self; | |
const VERSION: i64 = $ver; | |
} | |
}; | |
($first_type:ident = $first_ver:literal, $($rest:tt)*) => { | |
build_migration_chain!($first_type = $first_ver); | |
build_migration_chain!(@internal $first_type, $($rest)*); | |
}; | |
(@internal $prev_type:ident, $type:ident = $ver:literal, $($rest:tt)*) => { | |
impl $crate::Migrate for $type { | |
type From = $prev_type; | |
const VERSION: i64 = $ver; | |
} | |
build_migration_chain!(@internal $type, $($rest)*); | |
}; | |
(@internal $prev_type:ident, $type:ident = $ver:literal) => { | |
impl $crate::Migrate for $type { | |
type From = $prev_type; | |
const VERSION: i64 = $ver; | |
} | |
}; | |
} | |
build_migration_chain!(TypeA = 0, TypeB = 1, TypeC = 2, TypeD = 3); | |
// Results in: | |
impl Migrate for TypeA { | |
type From = Self; | |
const VERSION: i64 = 0; | |
} | |
impl Migrate for TypeB { | |
type From = TypeA; | |
const VERSION: i64 = 1; | |
} | |
impl Migrate for TypeC { | |
type From = TypeB; | |
const VERSION: i64 = 2; | |
} | |
impl Migrate for TypeD { | |
type From = TypeC; | |
const VERSION: i64 = 3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment