Skip to content

Commit f32ea1c

Browse files
committed
use 'FromStr' instead of custom method
1 parent b9314f1 commit f32ea1c

2 files changed

Lines changed: 47 additions & 15 deletions

File tree

src/crypto/methods.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
use std::fmt;
2+
use std::str::FromStr;
3+
4+
pub enum BelongLib {
5+
Crypto,
6+
#[cfg(feature = "openssl")]
7+
Openssl,
8+
}
9+
110
macro_rules! define_methods {
211
[$($method:tt => ($key_len:expr, $iv_len:expr, $lib:tt),)*] => (
312
#[allow(non_camel_case_types)]
@@ -9,16 +18,6 @@ macro_rules! define_methods {
918
}
1019

1120
impl Method {
12-
pub fn from(method: &str) -> Option<Method> {
13-
let method = method.replace("-", "_");
14-
match method.as_str() {
15-
$(
16-
stringify!($method) => Some(Method::$method),
17-
)*
18-
_ => None,
19-
}
20-
}
21-
2221
pub fn info(self) -> (usize, usize) {
2322
match self {
2423
$(
@@ -43,6 +42,29 @@ macro_rules! define_methods {
4342
]
4443
}
4544
}
45+
46+
impl FromStr for Method {
47+
type Err = ();
48+
fn from_str(s: &str) -> Result<Self, Self::Err> {
49+
let s = s.replace("-", "_");
50+
match s.as_str() {
51+
$(
52+
stringify!($method) => Ok(Method::$method),
53+
)*
54+
_ => Err(()),
55+
}
56+
}
57+
}
58+
59+
impl fmt::Display for Method {
60+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61+
match *self {
62+
$(
63+
Method::$method => write!(f, "{}", stringify!($method).replace("_", "-").as_str()),
64+
)*
65+
}
66+
}
67+
}
4668
)
4769
}
4870

@@ -81,8 +103,3 @@ define_methods!(
81103
aes_256_cfb8 => (32, 16, Openssl),
82104
);
83105

84-
pub enum BelongLib {
85-
Crypto,
86-
#[cfg(feature = "openssl")]
87-
Openssl,
88-
}

src/my_daemonize.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::str::FromStr;
2+
13
#[derive(PartialEq, Eq)]
24
pub enum Cmd {
35
Stop,
@@ -6,6 +8,19 @@ pub enum Cmd {
68
Unknown,
79
}
810

11+
impl FromStr for Cmd {
12+
type Err = String;
13+
14+
fn from_str(s: &str) -> Result<Self, Self::Err> {
15+
match s {
16+
"stop" => Ok(Cmd::Stop),
17+
"start" => Ok(Cmd::Start),
18+
"restart" => Ok(Cmd::Restart),
19+
_ => Err(format!("invalid daemon command: {}", s)),
20+
}
21+
}
22+
}
23+
924
pub use self::_daemonize::init;
1025

1126
#[cfg(target_family = "unix")]

0 commit comments

Comments
 (0)