fix clippy

This commit is contained in:
2020-04-12 14:17:42 +08:00
parent 63919d5532
commit 2e70fc3d73
5 changed files with 18 additions and 18 deletions

View File

@@ -117,7 +117,7 @@ pub fn parse_config(config_json: &json::JsonValue) -> OSSBackupdConfig {
let mut items_objects: Vec<OSSBackupdConfigItem> = vec![];
if items.is_array() {
for i in 0..items.len() {
items_objects.push(parse_oss_backupd_config_item(&items[i], &root_oss_config_object, &encrypt_pubkey_file, &backup_count));
items_objects.push(parse_oss_backupd_config_item(&items[i], &root_oss_config_object, &encrypt_pubkey_file, backup_count));
}
}
@@ -142,16 +142,16 @@ pub fn get_config_json(custom_oss_backupd_config: Option<&str>, verbose: bool) -
fn remove_start_end_slash(s: &str) -> String {
let mut ss = s;
while ss.starts_with("/") {
while ss.starts_with('/') {
ss = &ss[1..]
}
while ss.ends_with("/") {
while ss.ends_with('/') {
ss = &ss[0..(ss.len() - 1)];
}
ss.to_owned()
}
fn parse_oss_backupd_config_item(item: &json::JsonValue, root_oss_config_object: &Option<OSSConfig>, root_encrypt_pubkey_file: &Option<String>, root_backup_count: &Option<u32>) -> OSSBackupdConfigItem {
fn parse_oss_backupd_config_item(item: &json::JsonValue, root_oss_config_object: &Option<OSSConfig>, root_encrypt_pubkey_file: &Option<String>, root_backup_count: Option<u32>) -> OSSBackupdConfigItem {
let target = get_string_value(item, "target");
let file_name = get_string_value(item, "file_name");
let mut backup_count = get_u32_value(item, "backup_count");
@@ -185,7 +185,7 @@ fn parse_oss_backupd_config_item(item: &json::JsonValue, root_oss_config_object:
encrypt_pubkey_file = root_encrypt_pubkey_file.clone();
}
if backup_count.is_none() && root_backup_count.is_some() {
backup_count = root_backup_count.clone();
backup_count = root_backup_count;
}
OSSBackupdConfigItem {
@@ -314,7 +314,7 @@ fn get_config_content(custom_oss_backupd_config: Option<&str>, verbose: bool) ->
fn get_user_home() -> XResult<String> {
match dirs::home_dir() {
None => Err(new_box_ioerror("Home dir not found!")),
Some(home_dir) => home_dir.to_str().map(|h| h.to_owned()).ok_or(new_box_ioerror("Home dir not found!")),
Some(home_dir) => home_dir.to_str().map(|h| h.to_owned()).ok_or_else(|| new_box_ioerror("Home dir not found!")),
}
}

View File

@@ -61,29 +61,29 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem,
print_message(MessageType::DEBUG, &format!("Process config item index: {}, config: {:?}", item_index, config_item));
}
let encrypt_pubkey_file = config_item.encrypt_pubkey_file.as_ref().ok_or("encrypt_pubkey_file".to_owned())?;
let encrypt_pubkey_file = config_item.encrypt_pubkey_file.as_ref().ok_or_else(|| "encrypt_pubkey_file".to_owned())?;
if options.verbose {
print_message(MessageType::DEBUG, &format!("Encrypt pubkey file: {}", encrypt_pubkey_file));
}
let target = config_item.target.as_ref().ok_or("target".to_owned())?;
let target = config_item.target.as_ref().ok_or_else(|| "target".to_owned())?;
if options.verbose {
print_message(MessageType::DEBUG, &format!("Target file: {}", iff!(target.is_empty(), "<empty>", target)));
}
let oss_config = config_item.oss_config.as_ref().ok_or("oss_config".to_owned())?;
let oss_config = config_item.oss_config.as_ref().ok_or_else(|| "oss_config".to_owned())?;
let endpoint = oss_config.endpoint.as_ref().ok_or("oss_config#endpoint".to_owned())?;
let endpoint = oss_config.endpoint.as_ref().ok_or_else(|| "oss_config#endpoint".to_owned())?;
if options.verbose {
print_message(MessageType::DEBUG, &format!("Endpoint: {}", endpoint));
}
let access_key_id = oss_config.access_key_id.as_ref().ok_or("oss_config#access_key_id".to_owned())?;
let access_key_id = oss_config.access_key_id.as_ref().ok_or_else(|| "oss_config#access_key_id".to_owned())?;
if options.verbose {
print_message(MessageType::DEBUG, &format!("Access key id: {}", access_key_id));
}
let access_key_secret = oss_config.access_key_secret.as_ref().ok_or("oss_config#access_key_secret".to_owned())?;
let access_key_secret = oss_config.access_key_secret.as_ref().ok_or_else(|| "oss_config#access_key_secret".to_owned())?;
let bucket = &oss_config.bucket.as_ref().ok_or("oss_config#bucket".to_owned())?;
let bucket = &oss_config.bucket.as_ref().ok_or_else(|| "oss_config#bucket".to_owned())?;
if options.verbose {
print_message(MessageType::DEBUG, &format!("Bucket: {}", bucket));
}
@@ -118,7 +118,7 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem,
let temp_pgp_file = &format!("temp_file_{}.gpg", secs);
let remove_temp_files = || {
for f in vec![temp_zip_file, temp_pgp_file] {
for f in &[temp_zip_file, temp_pgp_file] {
if Path::new(f).is_file() {
if options.verbose {
print_message(MessageType::DEBUG, &format!("Remove file: {}", f));
@@ -211,7 +211,7 @@ fn process_new_backup_file(backup_content_json: &str, new_item: &str, limit: usi
}
// stringify JSON array
fn stringity_json_array(vec: &Vec<String>) -> XResult<String> {
fn stringity_json_array(vec: &[String]) -> XResult<String> {
let mut json_arr = json::JsonValue::new_array();
for v in vec {
json_arr.push(json::JsonValue::from(v.as_str()))?;

View File

@@ -102,7 +102,7 @@ impl OSSClient {
// https://endpoint, or http://endpoint -> endpoint
fn remove_endpoint_http_or_s(endpoint: &str) -> String {
let mut endpoint = endpoint.to_owned();
for prefix in vec![HTTP_SS, HTTPS_SS] {
for prefix in &[HTTP_SS, HTTPS_SS] {
if endpoint.starts_with(prefix) {
endpoint = endpoint.chars().skip(prefix.chars().count()).collect::<String>()
}

View File

@@ -92,7 +92,7 @@ fn encrypt_read_write(file: &mut File, write: &mut dyn Write) -> XResult<()> {
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(Box::new(e)),
};
write.write(&buf[..len])?;
write.write_all(&buf[..len])?;
read += len as u64;
pb.set_position(read);
}

View File

@@ -68,6 +68,6 @@ pub fn zip_file(target: &str, zip_file: &str) -> XResult<()> {
pub fn get_file_name(path: &Path) -> String {
match path.file_name() {
None => "no_file_name".to_owned(),
Some(f) => f.to_os_string().into_string().unwrap_or("unknown_file_name".to_owned()),
Some(f) => f.to_os_string().into_string().unwrap_or_else(|_| "unknown_file_name".to_owned()),
}
}