version_consts_git/lib.rs
1use std::fmt;
2
3use proc_macro_hack::proc_macro_hack;
4
5/// Determine properties of the git working tree at the crate root, if any
6///
7/// # Example
8/// ```
9/// use version_consts_git::version;
10/// fn main() {
11/// match version!() {
12/// None => eprintln!("not built from git"),
13/// Some(x) => {
14/// print!("{}", x.commit);
15/// if x.dirty {
16/// println!(" (dirty)");
17/// } else {
18/// println!();
19/// }
20/// }
21/// }
22/// }
23/// ```
24
25#[proc_macro_hack]
26pub use version_consts_git_impl::version;
27
28/// A concrete git commit
29#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
30pub struct Commit(pub [u8; 20]);
31
32impl std::ops::Deref for Commit {
33 type Target = [u8];
34 fn deref(&self) -> &[u8] {
35 &self.0
36 }
37}
38
39impl fmt::Display for Commit {
40 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41 for x in &self.0 {
42 write!(f, "{:02x}", x)?;
43 }
44 Ok(())
45 }
46}
47
48#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
49pub struct Version {
50 /// The current git commit
51 pub commit: Commit,
52 /// Whether there were uncommited changes
53 pub dirty: bool,
54}