Skip to content

Commit 88cdea0

Browse files
authored
Merge pull request #104 from MitMaro/tim/use-try-from
Use TryFrom trait now that it is stable
2 parents 371df63 + 08d9e70 commit 88cdea0

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/action.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::TryFrom;
2+
13
#[derive(PartialEq, Debug)]
24
pub enum Action {
35
Drop,
@@ -10,20 +12,6 @@ pub enum Action {
1012
}
1113

1214
impl Action {
13-
// TODO move into TryFrom once https://github.com/rust-lang/rust/issues/33417 is in stable
14-
pub fn try_from(s: &str) -> Result<Self, String> {
15-
match s {
16-
"drop" | "d" => Ok(Action::Drop),
17-
"edit" | "e" => Ok(Action::Edit),
18-
"exec" | "x" => Ok(Action::Exec),
19-
"fixup" | "f" => Ok(Action::Fixup),
20-
"pick" | "p" => Ok(Action::Pick),
21-
"reword" | "r" => Ok(Action::Reword),
22-
"squash" | "s" => Ok(Action::Squash),
23-
_ => Err(format!("Invalid action: {}", s)),
24-
}
25-
}
26-
2715
pub fn as_string(&self) -> String {
2816
String::from(match self {
2917
Action::Drop => "drop",
@@ -49,9 +37,28 @@ impl Action {
4937
}
5038
}
5139

40+
impl TryFrom<&str> for Action {
41+
type Error = String;
42+
43+
fn try_from(s: &str) -> Result<Self, String> {
44+
match s {
45+
"drop" | "d" => Ok(Action::Drop),
46+
"edit" | "e" => Ok(Action::Edit),
47+
"exec" | "x" => Ok(Action::Exec),
48+
"fixup" | "f" => Ok(Action::Fixup),
49+
"pick" | "p" => Ok(Action::Pick),
50+
"reword" | "r" => Ok(Action::Reword),
51+
"squash" | "s" => Ok(Action::Squash),
52+
_ => Err(format!("Invalid action: {}", s)),
53+
}
54+
}
55+
56+
}
57+
5258
#[cfg(test)]
5359
mod tests {
5460
use super::Action;
61+
use super::TryFrom;
5562

5663
#[test]
5764
fn action_to_str_drop() {

src/color.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::TryFrom;
2+
13
#[derive(Debug, Copy, Clone, PartialEq)]
24
pub enum Color {
35
White,
@@ -10,9 +12,10 @@ pub enum Color {
1012
Yellow,
1113
}
1214

13-
impl Color {
14-
// TODO move into TryFrom once https://github.com/rust-lang/rust/issues/33417 is in stable
15-
pub fn try_from(s: &str) -> Result<Self, String> {
15+
impl TryFrom<&str> for Color {
16+
type Error = String;
17+
18+
fn try_from(s: &str) -> Result<Self, String> {
1619
match s {
1720
"black" => Ok(Color::Black),
1821
"blue" => Ok(Color::Blue),
@@ -30,6 +33,7 @@ impl Color {
3033
#[cfg(test)]
3134
mod tests {
3235
use super::Color;
36+
use super::TryFrom;
3337

3438
#[test]
3539
fn action_from_str_black() {

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::color::Color;
2+
use std::convert::TryFrom;
23
use std::{env, ffi::OsString};
34

45
#[derive(Debug, Clone)]

src/line.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::action::Action;
2+
use std::convert::TryFrom;
23

34
#[derive(PartialEq, Debug)]
45
pub struct Line {

0 commit comments

Comments
 (0)