Skip to content

Instantly share code, notes, and snippets.

@leonardinius
Created June 28, 2015 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonardinius/edd0ebcea00517b64bcc to your computer and use it in GitHub Desktop.
Save leonardinius/edd0ebcea00517b64bcc to your computer and use it in GitHub Desktop.
Rust kata
// KATA
#![feature(convert)]
use std::result::Result as StdResult;
use std::io;
use std::process::Command;
use std::path::{Path,PathBuf};
use std::error::Error;
use std::ffi::OsStr;
use std::fmt;
#[derive(Debug)]
enum GitCommandError {
Unknown,
Status(i32), // process exit status <> 0
ExecCommand(io::Error), // cause
}
impl fmt::Display for GitCommandError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
}
}
impl Error for GitCommandError {
fn description(&self) -> &str {
match *self {
GitCommandError::Unknown => "Unknown error",
GitCommandError::Status(_) => "Status error",
GitCommandError::ExecCommand(_) => "Exec command failed",
}
}
fn cause(&self) -> Option<&Error> {
match *self {
GitCommandError::ExecCommand( ref ioe ) => Some(ioe),
_ => None,
}
}
}
impl From<io::Error> for GitCommandError {
fn from(cause : io::Error) -> Self {
GitCommandError::ExecCommand(cause)
}
}
type Result<T> = StdResult<T, GitCommandError>;
fn git_exec<S: AsRef<OsStr>>(dir: &Path, args: &[S]) -> Result<String> {
let output = try!(
Command::new(&PathBuf::from("git"))
.current_dir(dir)
.args(args)
.output()
);
if !output.status.success() {
Err( GitCommandError::Status(output.status.code().unwrap()) )
} else {
let out = String::from_utf8_lossy(&output.stdout);
Ok(String::from(&*out))
}
}
fn main() {
println!("git -> {:?}", git_exec(&PathBuf::from("."), vec!["log"].as_slice()));
}
@leonardinius
Copy link
Author

Part of http://www.meetup.com/Latvian-Developers-Network/events/221757301/

It appears the error during the KATA was cause by the following

λ ~/personal/rust-kata/ master* git log
fatal: bad default revision 'HEAD'
λ ~/personal/rust-kata/ master*

😊 other than works as intended without any changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment