Echo Writes Code

build.rs

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
use git2::{ Repository };

use std::fmt::{ self };
use std::process::{ ExitCode };

enum Error {
	FromGit2Crate(git2::Error),
}

impl From<git2::Error> for Error {
	fn from(e: git2::Error) -> Error {
		Error::FromGit2Crate(e)
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match *self {
			Error::FromGit2Crate(ref e) => write!(f, "Error from the git2 crate: {}", e),
		}
	}
}

type Result<T> = ::std::result::Result<T, Error>;

fn main() -> ExitCode {
	let result = configure_build();

	match result {
		Ok(_) => ExitCode::SUCCESS,
		Err(e) => {
			eprintln!("{}", e);
			ExitCode::FAILURE
		}
	}
}

fn configure_build() -> Result<()> {
	set_env_git_hash()?;

	Ok(())
}

fn set_env_git_hash() -> Result<()> {
	let repo = Repository::open(".")?;
	let hash = repo.refname_to_id("HEAD")?;
	println!("cargo::rustc-env=GIT_HASH={}", hash);

	Ok(())
}