From acf27a2d7b822438fb5a596cb9ca5db714590bf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20F=C3=BCrderer?= Date: Fri, 3 May 2019 13:56:08 +0200 Subject: [PATCH] Improve return type for insert function --- server/texture-sync-server/src/model/mod.rs | 22 ++++++++++++++----- .../src/persistency/mod.rs | 6 +++-- .../texture-sync-server/src/protocol/error.rs | 9 -------- .../protocol/implementation/listen_forever.rs | 7 ------ 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/server/texture-sync-server/src/model/mod.rs b/server/texture-sync-server/src/model/mod.rs index e06b579..f4b46ac 100644 --- a/server/texture-sync-server/src/model/mod.rs +++ b/server/texture-sync-server/src/model/mod.rs @@ -22,12 +22,24 @@ pub struct Texture { } pub enum ReplaceTextureStatus { - // Done. + /// Done. Ok, - // Call Again With Texture Binary + /// Call Again With Texture Binary NeedTextureData(Sha256), - - // Name or id already in use - Conflict, +} + +pub type ProtocolResult = Result; +pub enum ProtocolError { + BadRequest(String), + FileNotFound(String), + Conflict(String), + InternalServerError(std::io::Error), + NotImplemented, +} + +impl From for ProtocolError { + fn from(err: io::Error) -> Self { + ProtocolError::InternalServerError(err) + } } diff --git a/server/texture-sync-server/src/persistency/mod.rs b/server/texture-sync-server/src/persistency/mod.rs index 992e696..78bdb36 100644 --- a/server/texture-sync-server/src/persistency/mod.rs +++ b/server/texture-sync-server/src/persistency/mod.rs @@ -75,7 +75,7 @@ impl DataStore { &mut self, tex: Texture, data: Option>>, - ) -> io::Result { + ) -> ProtocolResult { use io::Write; // Check for collisions @@ -86,7 +86,9 @@ impl DataStore { .is_some() { // Name or id already in use - Ok(ReplaceTextureStatus::Conflict) + Err(ProtocolError::Conflict( + "Name or id is already in use.".to_string(), + )) } else { // Insert it if self.has_hash(&tex.texture_hash)? { diff --git a/server/texture-sync-server/src/protocol/error.rs b/server/texture-sync-server/src/protocol/error.rs index 32e1ac3..caa75da 100644 --- a/server/texture-sync-server/src/protocol/error.rs +++ b/server/texture-sync-server/src/protocol/error.rs @@ -1,10 +1 @@ use crate::model::*; - -pub type ProtocolResult = Result; -pub enum ProtocolError { - BadRequest(String), - FileNotFound(String), - Conflict(String), - InternalServerError(std::io::Error), - NotImplemented, -} diff --git a/server/texture-sync-server/src/protocol/implementation/listen_forever.rs b/server/texture-sync-server/src/protocol/implementation/listen_forever.rs index 7880f01..7177cb1 100644 --- a/server/texture-sync-server/src/protocol/implementation/listen_forever.rs +++ b/server/texture-sync-server/src/protocol/implementation/listen_forever.rs @@ -109,10 +109,6 @@ where panic!("Contract Violation: handler must not return NeedTextureData \ when data is given."); } - Ok(ReplaceTextureStatus::Conflict) => { - connection - .send(&Package::Error(409, "Conflict".to_string()))?; - } Err(err) => { connection.send(&Package::from(err))?; } @@ -129,9 +125,6 @@ where } } } - Ok(ReplaceTextureStatus::Conflict) => { - connection.send(&Package::Error(409, "Conflict".to_string()))?; - } Err(err) => { connection.send(&Package::from(err))?; }