Skip to content

Commit

Permalink
Merge pull request #104 from MitMaro/tim/use-try-from
Browse files Browse the repository at this point in the history
Use TryFrom trait now that it is stable
  • Loading branch information
MitMaro committed Apr 11, 2019
2 parents 371df63 + 08d9e70 commit 88cdea0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
35 changes: 21 additions & 14 deletions src/action.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

#[derive(PartialEq, Debug)]
pub enum Action {
Drop,
Expand All @@ -10,20 +12,6 @@ pub enum Action {
}

impl Action {
// TODO move into TryFrom once https://github.com/rust-lang/rust/issues/33417 is in stable
pub fn try_from(s: &str) -> Result<Self, String> {
match s {
"drop" | "d" => Ok(Action::Drop),
"edit" | "e" => Ok(Action::Edit),
"exec" | "x" => Ok(Action::Exec),
"fixup" | "f" => Ok(Action::Fixup),
"pick" | "p" => Ok(Action::Pick),
"reword" | "r" => Ok(Action::Reword),
"squash" | "s" => Ok(Action::Squash),
_ => Err(format!("Invalid action: {}", s)),
}
}

pub fn as_string(&self) -> String {
String::from(match self {
Action::Drop => "drop",
Expand All @@ -49,9 +37,28 @@ impl Action {
}
}

impl TryFrom<&str> for Action {
type Error = String;

fn try_from(s: &str) -> Result<Self, String> {
match s {
"drop" | "d" => Ok(Action::Drop),
"edit" | "e" => Ok(Action::Edit),
"exec" | "x" => Ok(Action::Exec),
"fixup" | "f" => Ok(Action::Fixup),
"pick" | "p" => Ok(Action::Pick),
"reword" | "r" => Ok(Action::Reword),
"squash" | "s" => Ok(Action::Squash),
_ => Err(format!("Invalid action: {}", s)),
}
}

}

#[cfg(test)]
mod tests {
use super::Action;
use super::TryFrom;

#[test]
fn action_to_str_drop() {
Expand Down
10 changes: 7 additions & 3 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Color {
White,
Expand All @@ -10,9 +12,10 @@ pub enum Color {
Yellow,
}

impl Color {
// TODO move into TryFrom once https://github.com/rust-lang/rust/issues/33417 is in stable
pub fn try_from(s: &str) -> Result<Self, String> {
impl TryFrom<&str> for Color {
type Error = String;

fn try_from(s: &str) -> Result<Self, String> {
match s {
"black" => Ok(Color::Black),
"blue" => Ok(Color::Blue),
Expand All @@ -30,6 +33,7 @@ impl Color {
#[cfg(test)]
mod tests {
use super::Color;
use super::TryFrom;

#[test]
fn action_from_str_black() {
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::color::Color;
use std::convert::TryFrom;
use std::{env, ffi::OsString};

#[derive(Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions src/line.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::action::Action;
use std::convert::TryFrom;

#[derive(PartialEq, Debug)]
pub struct Line {
Expand Down

0 comments on commit 88cdea0

Please sign in to comment.