diff --git a/src/action.rs b/src/action.rs index e71822620..a1b71b0a7 100644 --- a/src/action.rs +++ b/src/action.rs @@ -1,3 +1,5 @@ +use std::convert::TryFrom; + #[derive(PartialEq, Debug)] pub enum Action { Drop, @@ -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 { - 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", @@ -49,9 +37,28 @@ impl Action { } } +impl TryFrom<&str> for Action { + type Error = String; + + fn try_from(s: &str) -> Result { + 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() { diff --git a/src/color.rs b/src/color.rs index e089a7229..a022bb673 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,3 +1,5 @@ +use std::convert::TryFrom; + #[derive(Debug, Copy, Clone, PartialEq)] pub enum Color { White, @@ -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 { +impl TryFrom<&str> for Color { + type Error = String; + + fn try_from(s: &str) -> Result { match s { "black" => Ok(Color::Black), "blue" => Ok(Color::Blue), @@ -30,6 +33,7 @@ impl Color { #[cfg(test)] mod tests { use super::Color; + use super::TryFrom; #[test] fn action_from_str_black() { diff --git a/src/config.rs b/src/config.rs index 61f21c59e..2909a8b8f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use crate::color::Color; +use std::convert::TryFrom; use std::{env, ffi::OsString}; #[derive(Debug, Clone)] diff --git a/src/line.rs b/src/line.rs index 54d899162..5d501d7d2 100644 --- a/src/line.rs +++ b/src/line.rs @@ -1,4 +1,5 @@ use crate::action::Action; +use std::convert::TryFrom; #[derive(PartialEq, Debug)] pub struct Line {