Last active
June 7, 2020 14:21
-
-
Save BrianKopp/27bdf67d5846708e58bc1e8a9cdd30cd to your computer and use it in GitHub Desktop.
RustRusotoRocket_DataAccessTrait
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
// 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