1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright © 2021 The Radicle Link Contributors
//
// 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::TryInto;

use proc_macro::TokenStream;
use proc_macro_error::abort;
use quote::quote;
use syn::{parse_macro_input, LitStr};

use git_ref_format_core::{refspec::PatternStr, Component, Error, Qualified, RefStr};

/// Create a [`git_ref_format_core::RefString`] from a string literal.
///
/// The string is validated at compile time, and an unsafe conversion is
/// emitted.
#[proc_macro_error]
#[proc_macro]
pub fn refname(input: TokenStream) -> TokenStream {
    let lit = parse_macro_input!(input as LitStr);
    let val = lit.value();

    let parsed: Result<&RefStr, Error> = val.as_str().try_into();
    match parsed {
        Ok(safe) => {
            let safe: &str = safe.as_str();
            let expand = quote! {
                unsafe {
                    use ::std::mem::transmute;
                    use ::radicle_git_ext::ref_format::RefString;

                    transmute::<_, RefString>(#safe.to_owned())
                }
            };
            TokenStream::from(expand)
        }

        Err(e) => {
            abort!(lit.span(), "invalid refname literal: {}", e);
        }
    }
}

/// Create a [`git_ref_format_core::Qualified`] from a string literal.
///
/// The string is validated at compile time, and an unsafe conversion is
/// emitted.
#[proc_macro_error]
#[proc_macro]
pub fn qualified(input: TokenStream) -> TokenStream {
    let lit = parse_macro_input!(input as LitStr);
    let val = lit.value();

    let parsed: Result<&RefStr, Error> = val.as_str().try_into();
    match parsed {
        Ok(name) => {
            let qualified: Option<Qualified> = Qualified::from_refstr(name);
            match qualified {
                Some(safe) => {
                    let safe: &str = safe.as_str();
                    let expand = quote! {
                        unsafe {
                            use ::std::{borrow::Cow, mem::transmute};
                            use ::radicle_git_ext::ref_format::{Component, RefStr, RefString, Qualified};

                            let inner: RefString = transmute(#safe.to_owned());
                            let cow: Cow<'static, RefStr> = Cow::Owned(inner);
                            transmute::<_, Qualified>(cow)
                        }
                    };

                    TokenStream::from(expand)
                }

                None => {
                    abort!(
                        lit.span(),
                        "refname is not of the form 'refs/<category>/<name>'"
                    );
                }
            }
        }

        Err(e) => {
            abort!(lit.span(), "invalid refname literal: {}", e);
        }
    }
}

/// Create a [`git_ref_format_core::Component`] from a string literal.
///
/// The string is validated at compile time, and an unsafe conversion is
/// emitted.
#[proc_macro_error]
#[proc_macro]
pub fn component(input: TokenStream) -> TokenStream {
    let lit = parse_macro_input!(input as LitStr);
    let val = lit.value();

    let name: Result<&RefStr, Error> = val.as_str().try_into();
    match name {
        Ok(name) => {
            let comp: Option<Component> = name.into();
            match comp {
                Some(safe) => {
                    let safe: &str = safe.as_ref().as_str();
                    let expand = quote! {
                        unsafe {
                            use ::std::{borrow::Cow, mem::transmute};
                            use ::radicle_git_ext::ref_format::{Component, RefStr, RefString};

                            let inner: RefString = transmute(#safe.to_owned());
                            let cow: Cow<'static, RefStr> = Cow::Owned(inner);
                            transmute::<_, Component>(cow)
                        }
                    };

                    TokenStream::from(expand)
                }

                None => {
                    abort!(lit.span(), "component contains a '/'");
                }
            }
        }

        Err(e) => {
            abort!(lit.span(), "invalid refname literal: {}", e);
        }
    }
}

/// Create a [`git_ref_format_core::refspec::PatternString`] from a string
/// literal.
///
/// The string is validated at compile time, and an unsafe conversion is
/// emitted.
#[proc_macro_error]
#[proc_macro]
pub fn pattern(input: TokenStream) -> TokenStream {
    let lit = parse_macro_input!(input as LitStr);
    let val = lit.value();

    let parsed: Result<&PatternStr, Error> = val.as_str().try_into();
    match parsed {
        Ok(safe) => {
            let safe: &str = safe.as_str();
            let expand = quote! {
                unsafe {
                    use ::std::mem::transmute;
                    use ::radicle_git_ext::ref_format::refspec::PatternString;

                    transmute::<_, PatternString>(#safe.to_owned())
                }
            };
            TokenStream::from(expand)
        }

        Err(e) => {
            abort!(lit.span(), "invalid refspec pattern literal: {}", e);
        }
    }
}

/// Create a [`git_ref_format_core::refspec::QualifiedPattern`] from a string
/// literal.
///
/// The string is validated at compile time, and an unsafe conversion is
/// emitted.
#[proc_macro_error]
#[proc_macro]
pub fn qualified_pattern(input: TokenStream) -> TokenStream {
    let lit = parse_macro_input!(input as LitStr);
    let val = lit.value();

    let parsed: Result<&PatternStr, Error> = val.as_str().try_into();
    match parsed {
        Ok(safe) => {
            let safe: &str = safe.as_str();
            let expand = quote! {
                unsafe {
                    use ::std::mem::transmute;
                    use ::radicle_git_ext::ref_format::refspec::QualifiedPattern;

                    transmute::<_, QualifiedPattern>(#safe.to_owned())
                }
            };
            TokenStream::from(expand)
        }

        Err(e) => {
            abort!(lit.span(), "invalid refspec pattern literal: {}", e);
        }
    }
}