fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![allow(unused_assignments)] 2#![warn(clippy::unnecessary_to_owned)] 3 4#[allow(dead_code)] 5#[derive(Clone, Copy)] 6enum FileType { 7 Account, 8 PrivateKey, 9 Certificate, 10} 11 12fn main() { 13 let path = std::path::Path::new("x"); 14 15 let _ = check_files(&[(FileType::Account, path)]); 16 let _ = check_files_vec(vec![(FileType::Account, path)]); 17 18 // negative tests 19 let _ = check_files_ref(&[(FileType::Account, path)]); 20 let _ = check_files_mut(&[(FileType::Account, path)]); 21 let _ = check_files_ref_mut(&[(FileType::Account, path)]); 22 let _ = check_files_self_and_arg(&[(FileType::Account, path)]); 23 let _ = check_files_mut_path_buf(&[(FileType::Account, std::path::PathBuf::new())]); 24} 25 26// `check_files` and its variants are based on: 27// https://github.com/breard-r/acmed/blob/1f0dcc32aadbc5e52de6d23b9703554c0f925113/acmed/src/storage.rs#L262 28fn check_files(files: &[(FileType, &std::path::Path)]) -> bool { 29 for (t, path) in files { 30 let other = match get_file_path(t) { 31 Ok(p) => p, 32 Err(_) => { 33 return false; 34 }, 35 }; 36 if !path.is_file() || !other.is_file() { 37 return false; 38 } 39 } 40 true 41} 42 43fn check_files_vec(files: Vec<(FileType, &std::path::Path)>) -> bool { 44 for (t, path) in files.iter() { 45 let other = match get_file_path(t) { 46 Ok(p) => p, 47 Err(_) => { 48 return false; 49 }, 50 }; 51 if !path.is_file() || !other.is_file() { 52 return false; 53 } 54 } 55 true 56} 57 58fn check_files_ref(files: &[(FileType, &std::path::Path)]) -> bool { 59 for (ref t, path) in files.iter().copied() { 60 let other = match get_file_path(t) { 61 Ok(p) => p, 62 Err(_) => { 63 return false; 64 }, 65 }; 66 if !path.is_file() || !other.is_file() { 67 return false; 68 } 69 } 70 true 71} 72 73#[allow(unused_assignments)] 74fn check_files_mut(files: &[(FileType, &std::path::Path)]) -> bool { 75 for (mut t, path) in files.iter().copied() { 76 t = FileType::PrivateKey; 77 let other = match get_file_path(&t) { 78 Ok(p) => p, 79 Err(_) => { 80 return false; 81 }, 82 }; 83 if !path.is_file() || !other.is_file() { 84 return false; 85 } 86 } 87 true 88} 89 90fn check_files_ref_mut(files: &[(FileType, &std::path::Path)]) -> bool { 91 for (ref mut t, path) in files.iter().copied() { 92 *t = FileType::PrivateKey; 93 let other = match get_file_path(t) { 94 Ok(p) => p, 95 Err(_) => { 96 return false; 97 }, 98 }; 99 if !path.is_file() || !other.is_file() { 100 return false; 101 } 102 } 103 true 104} 105 106fn check_files_self_and_arg(files: &[(FileType, &std::path::Path)]) -> bool { 107 for (t, path) in files.iter().copied() { 108 let other = match get_file_path(&t) { 109 Ok(p) => p, 110 Err(_) => { 111 return false; 112 }, 113 }; 114 if !path.join(path).is_file() || !other.is_file() { 115 return false; 116 } 117 } 118 true 119} 120 121#[allow(unused_assignments)] 122fn check_files_mut_path_buf(files: &[(FileType, std::path::PathBuf)]) -> bool { 123 for (mut t, path) in files.iter().cloned() { 124 t = FileType::PrivateKey; 125 let other = match get_file_path(&t) { 126 Ok(p) => p, 127 Err(_) => { 128 return false; 129 }, 130 }; 131 if !path.is_file() || !other.is_file() { 132 return false; 133 } 134 } 135 true 136} 137 138fn get_file_path(_file_type: &FileType) -> Result<std::path::PathBuf, std::io::Error> { 139 Ok(std::path::PathBuf::new()) 140}