|
32 | 32 | //! perceived as a liability.
|
33 | 33 | //! - Write a HttpsFS version, which can be compiled to WebAssembly
|
34 | 34 | //! - Consider to provide an non-blocking version of HttpsFS
|
35 |
| -//! - Change result of FileSystem::exists() from bool to VfsResult<bool> |
36 | 35 | //! - Do version check after connecting to a HttpsFSServer
|
37 | 36 | //! - Do not expose reqwest::Certificate and rustls::Certificate via the API
|
38 | 37 | //! - Look for some unwrap(), which can be removed.
|
@@ -185,7 +184,7 @@ struct CommandRemoveDir {
|
185 | 184 |
|
186 | 185 | #[derive(Debug, Deserialize, Serialize)]
|
187 | 186 | enum CommandResponse {
|
188 |
| - Exists(bool), |
| 187 | + Exists(Result<bool, String>), |
189 | 188 | Metadata(Result<CmdMetadata, String>),
|
190 | 189 | CreateFile(CommandResponseCreateFile),
|
191 | 190 | RemoveFile(Result<(), String>),
|
@@ -654,7 +653,13 @@ impl<T: FileSystem> HttpsFSServer<T> {
|
654 | 653 |
|
655 | 654 | fn handle_command(command: &Command, file_system: &dyn FileSystem) -> CommandResponse {
|
656 | 655 | match command {
|
657 |
| - Command::Exists(param) => CommandResponse::Exists(file_system.exists(¶m.path)), |
| 656 | + Command::Exists(param) => CommandResponse::Exists({ |
| 657 | + let result = file_system.exists(¶m.path); |
| 658 | + match result { |
| 659 | + Ok(val) => Ok(val), |
| 660 | + Err(e) => Err(format!("{:?}", e)), |
| 661 | + } |
| 662 | + }), |
658 | 663 | Command::Metadata(param) => CommandResponse::Metadata(meta_res_convert_vfs_cmd(
|
659 | 664 | file_system.metadata(¶m.path),
|
660 | 665 | )),
|
@@ -691,7 +696,12 @@ impl<T: FileSystem> HttpsFSServer<T> {
|
691 | 696 | }
|
692 | 697 |
|
693 | 698 | fn write(cmd: &CommandWrite, file_system: &dyn FileSystem) -> Result<usize, String> {
|
694 |
| - if !file_system.exists(&cmd.path) { |
| 699 | + let exist = file_system.exists(&cmd.path); |
| 700 | + if let Err(e) = exist { |
| 701 | + return Err(format!("{:?}", e)); |
| 702 | + } |
| 703 | + let exist = exist.unwrap(); |
| 704 | + if !exist { |
695 | 705 | println!("WARN: Tried to write to non existing file.");
|
696 | 706 | return Err(String::from("File does not exists!"));
|
697 | 707 | }
|
@@ -1112,7 +1122,7 @@ impl FileSystem for HttpsFS {
|
1112 | 1122 | }
|
1113 | 1123 |
|
1114 | 1124 | fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead>> {
|
1115 |
| - if !self.exists(path) { |
| 1125 | + if !self.exists(path)? { |
1116 | 1126 | return Err(VfsError::FileNotFound {
|
1117 | 1127 | path: path.to_string(),
|
1118 | 1128 | })?;
|
@@ -1176,22 +1186,28 @@ impl FileSystem for HttpsFS {
|
1176 | 1186 | }
|
1177 | 1187 | }
|
1178 | 1188 |
|
1179 |
| - fn exists(&self, path: &str) -> bool { |
| 1189 | + fn exists(&self, path: &str) -> VfsResult<bool> { |
1180 | 1190 | // TODO: Add more logging
|
1181 | 1191 | // TODO: try to change return type to VfsResult<bool>
|
1182 | 1192 | // At the moment 'false' does not mean, that the file either does not exist
|
1183 | 1193 | // or that an error has occurred. An developer does not expect this.
|
1184 | 1194 | let req = Command::Exists(CommandExists {
|
1185 | 1195 | path: String::from(path),
|
1186 | 1196 | });
|
1187 |
| - let result = self.exec_command(&req); |
1188 |
| - if let Err(e) = result { |
1189 |
| - println!("Error: {}", e); |
1190 |
| - return false; |
1191 |
| - } |
1192 |
| - match result.unwrap() { |
| 1197 | + let result = self.exec_command(&req)?; |
| 1198 | + let result = match result { |
1193 | 1199 | CommandResponse::Exists(value) => value,
|
1194 |
| - _ => false, |
| 1200 | + _ => { |
| 1201 | + return Err(VfsError::Other { |
| 1202 | + message: String::from("Result doesn't match the request!"), |
| 1203 | + }); |
| 1204 | + } |
| 1205 | + }; |
| 1206 | + match result { |
| 1207 | + Err(e) => Err(VfsError::Other { |
| 1208 | + message: format!("{:?}", e), |
| 1209 | + }), |
| 1210 | + Ok(val) => Ok(val), |
1195 | 1211 | }
|
1196 | 1212 | }
|
1197 | 1213 |
|
|
0 commit comments