Skip to content

Commit

Permalink
optimize code style
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Jul 2, 2017
1 parent 5be0fd3 commit 1cc6371
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 38 additions & 48 deletions src/bin/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,58 +168,48 @@ fn main() {
None => Config::new(),
};

let mut has_provided_server_config = false;

if matches.value_of("SERVER_ADDR").is_some() && matches.value_of("PASSWORD").is_some() &&
matches.value_of("ENCRYPT_METHOD").is_some()
{
let (svr_addr, password, method) = matches
.value_of("SERVER_ADDR")
.and_then(|svr_addr| {
matches.value_of("PASSWORD").map(|pwd| (svr_addr, pwd))
})
.and_then(|(svr_addr, pwd)| {
matches.value_of("ENCRYPT_METHOD").map(
|m| (svr_addr, pwd, m),
)
})
.unwrap();
let has_provided_server_config = match (
matches.value_of("SERVER_ADDR"),
matches.value_of("PASSWORD"),
matches.value_of("ENCRYPT_METHOD"),
) {
(Some(svr_addr), Some(password), Some(method)) => {
let method = match method.parse() {
Ok(m) => m,
Err(err) => {
panic!("Does not support {:?} method: {:?}", method, err);
}
};

let method = match method.parse() {
Ok(m) => m,
Err(err) => {
panic!("Does not support {:?} method: {:?}", method, err);
}
};

let sc = ServerConfig::new(
svr_addr.parse::<ServerAddr>().expect("Invalid server addr"),
password.to_owned(),
method,
None,
);

config.server.push(sc);
has_provided_server_config = true;
} else if matches.value_of("SERVER_ADDR").is_none() && matches.value_of("PASSWORD").is_none() &&
matches.value_of("ENCRYPT_METHOD").is_none()
{
// Does not provide server config
} else {
panic!("`server-addr`, `method` and `password` should be provided together");
}
let sc = ServerConfig::new(
svr_addr.parse::<ServerAddr>().expect("Invalid server addr"),
password.to_owned(),
method,
None,
);

let has_provided_local_config = if matches.value_of("LOCAL_ADDR").is_some() {
let local_addr = matches.value_of("LOCAL_ADDR").unwrap();
config.server.push(sc);
true
}
(None, None, None) => {
// Does not provide server config
false
}
_ => {
panic!("`server-addr`, `method` and `password` should be provided together");
}
};

let local_addr: SocketAddr = local_addr.parse().expect(
"`local-addr` is not a valid IP address",
);
let has_provided_local_config = match matches.value_of("LOCAL_ADDR") {
Some(local_addr) => {
let local_addr: SocketAddr = local_addr.parse().expect(
"`local-addr` is not a valid IP address",
);

config.local = Some(local_addr);
true
} else {
false
config.local = Some(local_addr);
true
}
None => false,
};

if !has_provided_config && !(has_provided_server_config && has_provided_local_config) {
Expand Down
69 changes: 30 additions & 39 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,46 +175,37 @@ fn main() {
None => Config::new(),
};

let mut has_provided_server_config = false;

if matches.value_of("SERVER_ADDR").is_some() && matches.value_of("PASSWORD").is_some() &&
matches.value_of("ENCRYPT_METHOD").is_some()
{
let (svr_addr, password, method) = matches
.value_of("SERVER_ADDR")
.and_then(|svr_addr| {
matches.value_of("PASSWORD").map(|pwd| (svr_addr, pwd))
})
.and_then(|(svr_addr, pwd)| {
matches.value_of("ENCRYPT_METHOD").map(
|m| (svr_addr, pwd, m),
)
})
.unwrap();
let has_provided_server_config = match (
matches.value_of("SERVER_ADDR"),
matches.value_of("PASSWORD"),
matches.value_of("ENCRYPT_METHOD"),
) {
(Some(svr_addr), Some(password), Some(method)) => {
let method = match method.parse() {
Ok(m) => m,
Err(err) => {
panic!("Does not support {:?} method: {:?}", method, err);
}
};

let method = match method.parse() {
Ok(m) => m,
Err(err) => {
panic!("Does not support {:?} method: {:?}", method, err);
}
};

let sc = ServerConfig::new(
svr_addr.parse::<ServerAddr>().expect("Invalid server addr"),
password.to_owned(),
method,
None,
);

config.server.push(sc);
has_provided_server_config = true;
} else if matches.value_of("SERVER_ADDR").is_none() && matches.value_of("PASSWORD").is_none() &&
matches.value_of("ENCRYPT_METHOD").is_none()
{
// Does not provide server config
} else {
panic!("`server-addr`, `method` and `password` should be provided together");
}
let sc = ServerConfig::new(
svr_addr.parse::<ServerAddr>().expect("Invalid server addr"),
password.to_owned(),
method,
None,
);

config.server.push(sc);
true
}
(None, None, None) => {
// Does not provide server config
false
}
_ => {
panic!("`server-addr`, `method` and `password` should be provided together");
}
};

if !has_provided_config && !has_provided_server_config {
println!("You have to specify a configuration file or pass arguments from argument list");
Expand Down

0 comments on commit 1cc6371

Please sign in to comment.