-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from xsnippet/crud-storage
The initial implementation of the storage layer
- Loading branch information
Showing
10 changed files
with
922 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::error; | ||
use std::fmt; | ||
|
||
/// All errors that can be returned by the storage layer wrapped into a single | ||
/// enum type for convinience. | ||
#[derive(Debug)] | ||
pub enum StorageError { | ||
/// Snippet with this id already exists in the database | ||
Duplicate { id: String }, | ||
/// Snippet with this id can't be found | ||
NotFound { id: String }, | ||
/// All other errors that can't be handled by the storage layer | ||
InternalError(Box<dyn error::Error>), | ||
} | ||
|
||
impl fmt::Display for StorageError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
StorageError::Duplicate { id } => write!(f, "Snippet with id `{}` already exists", id), | ||
StorageError::NotFound { id } => write!(f, "Snippet with id `{}` is not found", id), | ||
StorageError::InternalError(e) => write!(f, "Internal error: `{}`", e), | ||
} | ||
} | ||
} | ||
|
||
// this is not strictly required, but allows for wrapping a StorageError | ||
// instance into a Box<dyn Error> if needed. Error's only requirement is for | ||
// a type to implement Debug + Display | ||
impl error::Error for StorageError {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_display() { | ||
assert_eq!( | ||
"Snippet with id `spam` already exists", | ||
format!( | ||
"{}", | ||
StorageError::Duplicate { | ||
id: "spam".to_string() | ||
} | ||
) | ||
); | ||
|
||
assert_eq!( | ||
"Snippet with id `eggs` is not found", | ||
format!( | ||
"{}", | ||
StorageError::NotFound { | ||
id: "eggs".to_string() | ||
} | ||
) | ||
); | ||
|
||
let error = "foo".parse::<f32>().err().unwrap(); | ||
assert_eq!( | ||
"Internal error: `invalid float literal`", | ||
format!("{}", StorageError::InternalError(Box::new(error))) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,27 @@ | ||
pub mod sql; | ||
mod errors; | ||
mod models; | ||
mod sql; | ||
|
||
pub use errors::StorageError; | ||
pub use models::{Changeset, Snippet}; | ||
pub use sql::SqlStorage; | ||
|
||
/// CRUD interface for storing/loading snippets from a persistent storage. | ||
/// | ||
/// Types implementing this trait are required to be both Send and Sync, so | ||
/// that their instances can be safely shared between multiple threads. | ||
pub trait Storage: Send + Sync { | ||
/// Save the state of the given snippet to the persistent storage. | ||
fn create(&self, snippet: &Snippet) -> Result<Snippet, StorageError>; | ||
|
||
/// Returns the snippet uniquely identified by a given id (a slug or a | ||
/// legacy numeric id) | ||
fn get(&self, id: &str) -> Result<Snippet, StorageError>; | ||
|
||
/// Update the state of the given snippet in the persistent storage | ||
fn update(&self, snippet: &Snippet) -> Result<Snippet, StorageError>; | ||
|
||
/// Delete the snippet uniquely identified by a given id (a slug or a legacy | ||
/// numeric id) | ||
fn delete(&self, id: &str) -> Result<(), StorageError>; | ||
} |
Oops, something went wrong.