Skip to content

Use TryFrom trait now that it is stable #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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