Skip to content

Commit

Permalink
Make git_testament!(…) generate const
Browse files Browse the repository at this point in the history
This way the testament can be used in `const fn`.
  • Loading branch information
Kijewski committed Sep 12, 2023
1 parent 80799e3 commit 478accf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions git-testament-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ pub fn git_testament(input: TokenStream) -> TokenStream {
);
return (quote! {
#[allow(clippy::needless_update)]
static #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
const #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
commit: #crate_::CommitKind::NoRepository(#pkgver, #now),
.. #crate_::EMPTY_TESTAMENT
};
Expand All @@ -345,7 +345,7 @@ pub fn git_testament(input: TokenStream) -> TokenStream {
if gitinfo.commitinfo.is_none() {
return (quote! {
#[allow(clippy::needless_update)]
static #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
const #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
commit: #crate_::CommitKind::NoCommit(#pkgver, #now),
branch_name: #branch_name,
.. #crate_::EMPTY_TESTAMENT
Expand Down Expand Up @@ -399,7 +399,7 @@ pub fn git_testament(input: TokenStream) -> TokenStream {

(quote! {
#[allow(clippy::needless_update)]
static #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
const #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
commit: #commit,
modifications: &[#(#statuses),*],
branch_name: #branch_name,
Expand Down
24 changes: 24 additions & 0 deletions tests/const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use git_testament::{git_testament, git_testament_macros};

git_testament!(TESTAMENT);

git_testament_macros!(TESTAMENT);

const TESTAMENT_BRANCH_NAME_OR_DEFAULT: &str = {
match TESTAMENT.branch_name {
Some(branch_name) => branch_name,
None => "main",
}
};

const MACROS_BRANCH_NAME_OR_DEFAULT: &str = {
match TESTAMENT_branch!() {
Some(branch_name) => branch_name,
None => "main",
}
};

#[test]
fn it_works() {
assert_eq!(TESTAMENT_BRANCH_NAME_OR_DEFAULT, MACROS_BRANCH_NAME_OR_DEFAULT);
}

0 comments on commit 478accf

Please sign in to comment.