Skip to content

Commit 8da299a

Browse files
committed
Merge common steps of the FileSystem implementation into HttpsFS::exec_command().
1 parent 5086f47 commit 8da299a

File tree

2 files changed

+59
-44
lines changed

2 files changed

+59
-44
lines changed

src/impls/https.rs

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ use tokio::net::{TcpListener, TcpStream};
6969
use tokio_rustls::server::TlsStream;
7070
use tokio_rustls::TlsAcceptor;
7171

72+
mod httpsfserror;
73+
use httpsfserror::HttpsFSError;
74+
7275
/// A file system exposed over https
7376
pub struct HttpsFS {
7477
addr: String,
@@ -320,6 +323,14 @@ impl HttpsFS {
320323
let cert = reqwest::Certificate::from_pem(&buf)?;
321324
Ok(cert)
322325
}
326+
327+
fn exec_command(&self, cmd: &Command) -> Result<CommandResponse, HttpsFSError> {
328+
let req = serde_json::to_string(&cmd)?;
329+
let result = self.client.post(&self.addr).body(req).send()?;
330+
let result = result.text()?;
331+
let result: CommandResponse = serde_json::from_str(&result)?;
332+
Ok(result)
333+
}
323334
}
324335

325336
impl HttpsFSBuilder {
@@ -913,10 +924,7 @@ impl FileSystem for HttpsFS {
913924
let req = Command::ReadDir(CommandReadDir {
914925
path: String::from(path),
915926
});
916-
let req = serde_json::to_string(&req)?;
917-
let result = self.client.post(&self.addr).body(req).send()?;
918-
let result = result.text()?;
919-
let result: CommandResponse = serde_json::from_str(&result)?;
927+
let result = self.exec_command(&req)?;
920928
let result = match result {
921929
CommandResponse::ReadDir(value) => value,
922930
_ => {
@@ -937,10 +945,7 @@ impl FileSystem for HttpsFS {
937945
let req = Command::CreateDir(CommandCreateDir {
938946
path: String::from(path),
939947
});
940-
let req = serde_json::to_string(&req)?;
941-
let result = self.client.post(&self.addr).body(req).send()?;
942-
let result = result.text()?;
943-
let result: CommandResponse = serde_json::from_str(&result)?;
948+
let result = self.exec_command(&req)?;
944949
let result = match result {
945950
CommandResponse::CreateDir(value) => value,
946951
_ => {
@@ -977,10 +982,7 @@ impl FileSystem for HttpsFS {
977982
let req = Command::CreateFile(CommandCreateFile {
978983
path: String::from(path),
979984
});
980-
let req = serde_json::to_string(&req)?;
981-
let result = self.client.post(&self.addr).body(req).send()?;
982-
let result = result.text()?;
983-
let result: CommandResponse = serde_json::from_str(&result)?;
985+
let result = self.exec_command(&req)?;
984986
let result = match result {
985987
CommandResponse::CreateFile(value) => value,
986988
_ => {
@@ -992,7 +994,7 @@ impl FileSystem for HttpsFS {
992994

993995
match result {
994996
CommandResponseCreateFile::Failed => Err(VfsError::Other {
995-
message: String::from("Result doesn't match the request!"),
997+
message: String::from("Faild to create file!"),
996998
}),
997999
CommandResponseCreateFile::Success => Ok(Box::new(WritableFile {
9981000
client: self.client.clone(),
@@ -1017,10 +1019,7 @@ impl FileSystem for HttpsFS {
10171019
let req = Command::Metadata(CommandMetadata {
10181020
path: String::from(path),
10191021
});
1020-
let req = serde_json::to_string(&req)?;
1021-
let result = self.client.post(&self.addr).body(req).send()?;
1022-
let result = result.text()?;
1023-
let result: CommandResponse = serde_json::from_str(&result)?;
1022+
let result = self.exec_command(&req)?;
10241023
match result {
10251024
CommandResponse::Metadata(value) => meta_res_convert_cmd_vfs(value),
10261025
_ => Err(VfsError::Other {
@@ -1037,25 +1036,7 @@ impl FileSystem for HttpsFS {
10371036
let req = Command::Exists(CommandExists {
10381037
path: String::from(path),
10391038
});
1040-
1041-
let req = serde_json::to_string(&req);
1042-
if let Err(e) = req {
1043-
println!("Error: {:?}", e);
1044-
return false;
1045-
}
1046-
let req = req.unwrap();
1047-
let result = self.client.post(&self.addr).body(req).send();
1048-
if let Err(e) = result {
1049-
println!("Error: {:?}", e);
1050-
return false;
1051-
}
1052-
let result = result.unwrap().text();
1053-
if let Err(e) = result {
1054-
println!("Error: {:?}", e);
1055-
return false;
1056-
}
1057-
let result: Result<CommandResponse, serde_json::Error> =
1058-
serde_json::from_str(&result.unwrap());
1039+
let result = self.exec_command(&req);
10591040
if let Err(e) = result {
10601041
println!("Error: {:?}", e);
10611042
return false;
@@ -1070,10 +1051,7 @@ impl FileSystem for HttpsFS {
10701051
let req = Command::RemoveFile(CommandRemoveFile {
10711052
path: String::from(path),
10721053
});
1073-
let req = serde_json::to_string(&req)?;
1074-
let result = self.client.post(&self.addr).body(req).send()?;
1075-
let result = result.text()?;
1076-
let result: CommandResponse = serde_json::from_str(&result)?;
1054+
let result = self.exec_command(&req)?;
10771055
let result = match result {
10781056
CommandResponse::RemoveFile(value) => value,
10791057
_ => {
@@ -1095,10 +1073,7 @@ impl FileSystem for HttpsFS {
10951073
let req = Command::RemoveDir(CommandRemoveDir {
10961074
path: String::from(path),
10971075
});
1098-
let req = serde_json::to_string(&req)?;
1099-
let result = self.client.post(&self.addr).body(req).send()?;
1100-
let result = result.text()?;
1101-
let result: CommandResponse = serde_json::from_str(&result)?;
1076+
let result = self.exec_command(&req)?;
11021077
let result = match result {
11031078
CommandResponse::RemoveDir(value) => value,
11041079
_ => {

src/impls/https/httpsfserror.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::VfsError;
2+
use thiserror::Error;
3+
4+
#[derive(Error, Debug)]
5+
pub enum HttpsFSError {
6+
#[error("Serialization/Deserialization error: {0}")]
7+
SerDe(serde_json::Error),
8+
9+
#[error("Network error: {0}")]
10+
Network(reqwest::Error),
11+
}
12+
13+
impl From<serde_json::Error> for HttpsFSError {
14+
fn from(error: serde_json::Error) -> Self {
15+
HttpsFSError::SerDe(error)
16+
}
17+
}
18+
19+
impl From<reqwest::Error> for HttpsFSError {
20+
fn from(error: reqwest::Error) -> Self {
21+
HttpsFSError::Network(error)
22+
}
23+
}
24+
25+
impl From<HttpsFSError> for VfsError {
26+
fn from(error: HttpsFSError) -> Self {
27+
let cause = Box::new(match error {
28+
HttpsFSError::SerDe(_) => VfsError::Other {
29+
message: format!("{}", error),
30+
},
31+
HttpsFSError::Network(_) => VfsError::Other {
32+
message: format!("{}", error),
33+
},
34+
});
35+
VfsError::WithContext {
36+
context: String::from("HttpsFS"),
37+
cause: cause,
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)