Skip to content

Commit

Permalink
macro: Add support for pub testaments
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Silverstone <[email protected]>
  • Loading branch information
kinnison committed Nov 28, 2024
1 parent e9b3643 commit 76b9927
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-testament"
version = "0.2.5"
version = "0.2.6"
authors = ["Daniel Silverstone <[email protected]>"]
edition = "2021"

Expand All @@ -15,7 +15,7 @@ include = ["src", "tests", "test-template"]
members = ["git-testament-derive"]

[dependencies]
git-testament-derive = { version = "0.2.0", path = "git-testament-derive" }
git-testament-derive = { version = "0.2.1", path = "git-testament-derive" }

[dev-dependencies]
tempfile = "3"
Expand Down
2 changes: 1 addition & 1 deletion git-testament-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Daniel Silverstone <[email protected]>"]
edition = "2018"
name = "git-testament-derive"
version = "0.2.0"
version = "0.2.1"

description = "Record git working tree status when compiling your crate - inner procedural macro"
documentation = "https://docs.rs/git-testament/"
Expand Down
23 changes: 14 additions & 9 deletions git-testament-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::process::{Command, Stdio};
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::parse;
use syn::parse::{Parse, ParseStream};
use syn::{parse, Visibility};
use syn::{parse_macro_input, Ident, LitStr};

use log::warn;
Expand All @@ -23,14 +23,19 @@ const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]
struct TestamentOptions {
crate_: Ident,
name: Ident,
vis: Option<Visibility>,
}

impl Parse for TestamentOptions {
fn parse(input: ParseStream) -> parse::Result<Self> {
Ok(TestamentOptions {
crate_: input.parse()?,
name: input.parse()?,
})
let crate_ = input.parse()?;
let name = input.parse()?;
let vis = if input.is_empty() {
None
} else {
Some(input.parse()?)
};
Ok(TestamentOptions { crate_, name, vis })
}
}

Expand Down Expand Up @@ -310,7 +315,7 @@ impl GitInformation {

#[proc_macro]
pub fn git_testament(input: TokenStream) -> TokenStream {
let TestamentOptions { crate_, name } = parse_macro_input!(input);
let TestamentOptions { crate_, name, vis } = parse_macro_input!(input);

let InvocationInformation { pkgver, now } = InvocationInformation::acquire();
let gitinfo = match GitInformation::acquire() {
Expand All @@ -323,7 +328,7 @@ pub fn git_testament(input: TokenStream) -> TokenStream {
);
return (quote! {
#[allow(clippy::needless_update)]
const #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
#vis const #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
commit: #crate_::CommitKind::NoRepository(#pkgver, #now),
.. #crate_::EMPTY_TESTAMENT
};
Expand All @@ -345,7 +350,7 @@ pub fn git_testament(input: TokenStream) -> TokenStream {
if gitinfo.commitinfo.is_none() {
return (quote! {
#[allow(clippy::needless_update)]
const #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
#vis 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 +404,7 @@ pub fn git_testament(input: TokenStream) -> TokenStream {

(quote! {
#[allow(clippy::needless_update)]
const #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
#vis const #name: #crate_::GitTestament<'static> = #crate_::GitTestament {
commit: #commit,
modifications: &[#(#statuses),*],
branch_name: #branch_name,
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ use core::fmt::{self, Display, Formatter};
/// See [`GitTestament`] for the type of the defined `TESTAMENT`.
#[macro_export]
macro_rules! git_testament {
($vis:vis $name:ident) => {
$crate::__derive::git_testament! {
$crate $name $vis
}
};
($name:ident) => {
$crate::__derive::git_testament! {
$crate $name
Expand Down
6 changes: 6 additions & 0 deletions tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ git_testament!(TESTAMENT);

git_testament_macros!(version);

mod inner {
use git_testament::git_testament;
git_testament!(pub INNER);
}

#[test]
fn it_works() {
println!("Testament: {TESTAMENT}");
println!("Inner: {}", inner::INNER);
}

//testament macro is not guaranteed to be indentical to testament's Display in `no_std`
Expand Down

0 comments on commit 76b9927

Please sign in to comment.