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
extern crate proc_macro;
use proc_macro::TokenStream;
use std::convert::AsRef;
use std::process::Command;
const HASH_BYTES: usize = 20;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Hash([u8; HASH_BYTES]);
impl Hash {
pub fn most_significant_u64(&self) -> u64 {
u64::from(self.0[0]) << 56
| u64::from(self.0[1]) << 48
| u64::from(self.0[2]) << 40
| u64::from(self.0[3]) << 32
| u64::from(self.0[4]) << 24
| u64::from(self.0[5]) << 16
| u64::from(self.0[6]) << 8
| u64::from(self.0[7])
}
}
impl From<Hash> for [u8; HASH_BYTES] {
fn from(hash: Hash) -> Self {
hash.0
}
}
impl AsRef<[u8; HASH_BYTES]> for Hash {
fn as_ref(&self) -> &[u8; HASH_BYTES] {
&self.0
}
}
impl std::fmt::Display for Hash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for byte in self.0.iter() {
write!(f, "{:02x}", *byte)?;
}
Ok(())
}
}
#[proc_macro]
pub fn try_revision_u64(_item: TokenStream) -> TokenStream {
match get_git_revision() {
Ok(revision) => {
let code = format!("Some::<u64>({})", revision.most_significant_u64());
code.parse().unwrap()
}
Err(_e) => "None::<u64>".parse().unwrap(),
}
}
#[proc_macro]
pub fn revision_u64(_item: TokenStream) -> TokenStream {
let revision = get_git_revision().expect("couldn't get git revision");
format!("{}u64", revision.most_significant_u64())
.parse()
.unwrap()
}
#[proc_macro]
pub fn try_revision_string(_item: TokenStream) -> TokenStream {
match get_git_revision() {
Ok(hash) => format!("Some::<&'static str>(\"{}\")", hash)
.parse()
.unwrap(),
Err(_) => "None::<&'static str>".parse().unwrap(),
}
}
#[proc_macro]
pub fn revision_string(_item: TokenStream) -> TokenStream {
let revision = get_git_revision().expect("couldn't get git revision");
format!("\"{}\"", revision).parse().unwrap()
}
fn get_git_revision() -> Result<Hash, Error> {
let output = Command::new("git")
.args(&["rev-parse", "HEAD"])
.output()
.map_err(|_| Error::Command)?;
if output.status.success() {
let mut stdout = output.stdout;
if stdout.len() < HASH_BYTES * 2 {
return Err(Error::Parse);
}
stdout.truncate(HASH_BYTES * 2);
let stdout = String::from_utf8(stdout).map_err(|_| Error::Parse)?;
let mut hash_bytes = [0u8; HASH_BYTES];
for i in 0..HASH_BYTES {
let two_digits: &str = &stdout[i * 2..i * 2 + 2];
hash_bytes[i] = u8::from_str_radix(two_digits, 16).map_err(|_| Error::Parse)?;
}
Ok(Hash(hash_bytes))
} else {
Err(Error::StatusCode)
}
}
#[derive(Debug)]
enum Error {
Command,
StatusCode,
Parse,
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Command => write!(f, "Failed to execute git"),
Error::StatusCode => write!(f, "git returned a non-success exit code"),
Error::Parse => write!(f, "the output of git could not be parsed"),
}
}
}