Skip to content

Instantly share code, notes, and snippets.

@BrianKopp
Last active June 7, 2020 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianKopp/27bdf67d5846708e58bc1e8a9cdd30cd to your computer and use it in GitHub Desktop.
Save BrianKopp/27bdf67d5846708e58bc1e8a9cdd30cd to your computer and use it in GitHub Desktop.
RustRusotoRocket_DataAccessTrait
// to block and await
use futures::executor::block_on;
// Would allow us to swap out implementations for testing or to change persistence layer
trait UserCrud {
fn get_user(&self, id: String) -> Result<User, &'static str>;
fn delete_user(&self, id: String) -> Result<bool, &'static str>;
fn save_user(&self, user: User) -> Result<bool, &'static str>;
}
// Has everything we need to know about our dynamo implementation
struct DynamoUserCrud {
table_name: String,
client: DynamoDbClient,
}
// Actually implement the trait for our dynamo struct
impl UserCrud for DynamoUserCrud {
fn get_user(&self, id: String) -> Result<User, &'static str> {
block_on(dynamo_get_user(&self.client, self.table_name.to_string(), id))
}
fn delete_user(&self, id: String) -> Result<bool, &'static str> {
block_on(dynamo_delete_user(&self.client, self.table_name.to_string(), id))
}
fn save_user(&self, user: User) -> Result<bool, &'static str> {
block_on(dynamo_save_user(&self.client, self.table_name.to_string(), user))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment