Created
June 28, 2015 16:07
-
-
Save leonardinius/edd0ebcea00517b64bcc to your computer and use it in GitHub Desktop.
Rust kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Part of http://www.meetup.com/Latvian-Developers-Network/events/221757301/
It appears the error during the KATA was cause by the following
😊 other than works as intended without any changes