Files
radicle-git/radicle-macros/src/lib.rs
T
Fintan Halpenny ce7dc3890a meta: use long crate names
To help with clarity and potential publishing of the crates, the full
`radicle-` prefix is used for the crate names.

Note that it wasn't chosen got `git-ref-format` and `git-trailers`
since they are truly general libraries (bar the feature flag for link
specific reference names).

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
2022-09-15 17:58:14 +01:00

73 lines
2.0 KiB
Rust

// Copyright © 2019-2020 The Radicle Foundation <hello@radicle.foundation>
//
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.
#[macro_use]
extern crate proc_macro_error;
use std::convert::TryFrom;
use proc_macro::TokenStream;
use proc_macro_error::abort;
use quote::quote;
use syn::{parse_macro_input, LitStr};
use radicle_git_ext::reference::name::{RefLike, RefspecPattern};
/// Create `RefLike` from a string literal.
///
/// The string is validated at compile time, and an unsafe conversion is
/// emitted.
///
/// ```rust
/// use radicle_macros::reflike;
///
/// assert_eq!("lolek/bolek", reflike!("lolek/bolek").as_str())
/// ```
#[proc_macro_error]
#[proc_macro]
pub fn reflike(input: TokenStream) -> TokenStream {
let lit = parse_macro_input!(input as LitStr);
match RefLike::try_from(lit.value()) {
Ok(safe) => {
let safe: &str = &*safe;
let expand = quote! { unsafe { ::std::mem::transmute::<_, ::radicle_git_ext::RefLike>(#safe.to_owned()) }};
TokenStream::from(expand)
},
Err(e) => {
abort!(lit.span(), "invalid RefLike literal: {}", e);
},
}
}
/// Create a `RefspecPattern` from a string literal.
///
/// The string is validated at compile time, and an unsafe conversion is
/// emitted.
///
/// ```rust
/// use radicle_macros::refspec_pattern;
///
/// assert_eq!("refs/heads/*", refspec_pattern!("refs/heads/*").as_str())
/// ```
#[proc_macro_error]
#[proc_macro]
pub fn refspec_pattern(input: TokenStream) -> TokenStream {
let lit = parse_macro_input!(input as LitStr);
match RefspecPattern::try_from(lit.value()) {
Ok(safe) => {
let safe: &str = &*safe;
let expand = quote! { unsafe { ::std::mem::transmute::<_, ::radicle_git_ext::RefspecPattern>(#safe.to_owned()) }};
TokenStream::from(expand)
},
Err(e) => {
abort!(lit.span(), "invalid RefspecPattern literal: {}", e);
},
}
}