Improve return type for insert function

This commit is contained in:
Lukas Fürderer 2019-05-03 13:56:08 +02:00
parent 96c2864a07
commit acf27a2d7b
4 changed files with 21 additions and 23 deletions

View File

@ -22,12 +22,24 @@ pub struct Texture {
} }
pub enum ReplaceTextureStatus { pub enum ReplaceTextureStatus {
// Done. /// Done.
Ok, Ok,
// Call Again With Texture Binary /// Call Again With Texture Binary
NeedTextureData(Sha256), NeedTextureData(Sha256),
}
// Name or id already in use
Conflict, pub type ProtocolResult<T> = Result<T, ProtocolError>;
pub enum ProtocolError {
BadRequest(String),
FileNotFound(String),
Conflict(String),
InternalServerError(std::io::Error),
NotImplemented,
}
impl From<io::Error> for ProtocolError {
fn from(err: io::Error) -> Self {
ProtocolError::InternalServerError(err)
}
} }

View File

@ -75,7 +75,7 @@ impl DataStore {
&mut self, &mut self,
tex: Texture, tex: Texture,
data: Option<Arc<Vec<u8>>>, data: Option<Arc<Vec<u8>>>,
) -> io::Result<ReplaceTextureStatus> { ) -> ProtocolResult<ReplaceTextureStatus> {
use io::Write; use io::Write;
// Check for collisions // Check for collisions
@ -86,7 +86,9 @@ impl DataStore {
.is_some() .is_some()
{ {
// Name or id already in use // Name or id already in use
Ok(ReplaceTextureStatus::Conflict) Err(ProtocolError::Conflict(
"Name or id is already in use.".to_string(),
))
} else { } else {
// Insert it // Insert it
if self.has_hash(&tex.texture_hash)? { if self.has_hash(&tex.texture_hash)? {

View File

@ -1,10 +1 @@
use crate::model::*; use crate::model::*;
pub type ProtocolResult<T> = Result<T, ProtocolError>;
pub enum ProtocolError {
BadRequest(String),
FileNotFound(String),
Conflict(String),
InternalServerError(std::io::Error),
NotImplemented,
}

View File

@ -109,10 +109,6 @@ where
panic!("Contract Violation: handler must not return NeedTextureData \ panic!("Contract Violation: handler must not return NeedTextureData \
when data is given."); when data is given.");
} }
Ok(ReplaceTextureStatus::Conflict) => {
connection
.send(&Package::Error(409, "Conflict".to_string()))?;
}
Err(err) => { Err(err) => {
connection.send(&Package::from(err))?; connection.send(&Package::from(err))?;
} }
@ -129,9 +125,6 @@ where
} }
} }
} }
Ok(ReplaceTextureStatus::Conflict) => {
connection.send(&Package::Error(409, "Conflict".to_string()))?;
}
Err(err) => { Err(err) => {
connection.send(&Package::from(err))?; connection.send(&Package::from(err))?;
} }