diff --git a/Cargo.toml b/Cargo.toml index 9a86c84..712dc52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "git-testament" -version = "0.2.5" +version = "0.2.6" authors = ["Daniel Silverstone "] edition = "2021" @@ -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" diff --git a/git-testament-derive/Cargo.toml b/git-testament-derive/Cargo.toml index 01dc3d9..3976a91 100644 --- a/git-testament-derive/Cargo.toml +++ b/git-testament-derive/Cargo.toml @@ -2,7 +2,7 @@ authors = ["Daniel Silverstone "] 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/" diff --git a/git-testament-derive/src/lib.rs b/git-testament-derive/src/lib.rs index 264fc64..c393578 100644 --- a/git-testament-derive/src/lib.rs +++ b/git-testament-derive/src/lib.rs @@ -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; @@ -23,14 +23,19 @@ const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day] struct TestamentOptions { crate_: Ident, name: Ident, + vis: Option, } impl Parse for TestamentOptions { fn parse(input: ParseStream) -> parse::Result { - 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 }) } } @@ -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() { @@ -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 }; @@ -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 @@ -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, diff --git a/src/lib.rs b/src/lib.rs index 335794f..6047bcf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 diff --git a/tests/simple.rs b/tests/simple.rs index 317294f..4ee0349 100644 --- a/tests/simple.rs +++ b/tests/simple.rs @@ -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`