Skip to content

Instantly share code, notes, and snippets.

@BrianKopp
BrianKopp / main.rs
Last active June 13, 2020 22:08
Programming Challenge - Rust - Roman Numerals Checkpoint 3
fn make_roman_numeral(my_int: i32) -> String {
let descending = get_roman_numerals_descending();
let mut roman_numeral = String::new();
let mut remainder = my_int;
// find the largest roman numeral smaller than remainder,
// append the roman character, and deduct from the remainder,
// if roman numeral is not smaller, try the next valid subtractor.
// e.g. if remainder is smaller than 10, X, then check if remainder
// is smaller than 9, IX, else go down to next numeral, V.
@BrianKopp
BrianKopp / main.rs
Created June 13, 2020 22:00
Programming Challenge - Rust - Roman Numerals Checkpoint 2
// associate a roman numeral with its value
struct RomanNumeral {
txt: String,
value: i32,
}
fn is_factor_of_ten(i: i32) -> bool {
if i == 1 {
return true;
}
@BrianKopp
BrianKopp / main.rs
Created June 13, 2020 20:39
Programming Challenge - Rust - Roman Numerals Checkpoint 1
use std::env;
fn main() {
// get the command line argument
let args: Vec<String> = env::args().collect();
// first command line arg is the program
if args.len() < 2 {
panic!("must supply a command line argument of the number to convert");
}
@BrianKopp
BrianKopp / main.rs
Created June 7, 2020 14:32
RustRusotoRocket_RocketInit
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
fn main() {
rocket::ignite().mount("/", routes![health_check]).launch();
}
#[get("/health")]
fn health_check() -> &'static str {
@BrianKopp
BrianKopp / dynamo.rs
Last active June 7, 2020 14:31
RustRusotoRocket_DynamoUserCrudNew
use rusoto_core::Region;
use rusoto_dynamodb::DynamoDbClient;
impl DynamoUserCrud {
fn new(local: bool) -> DynamoDbClient {
let region = match local {
false => Region::UsEast1,
true => Region::Custom {
name: "us-east-1".to_owned(),
endpoint: "http://localhost:4566".to_owned()
@BrianKopp
BrianKopp / data.rs
Last active June 7, 2020 14:21
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>;
}
@BrianKopp
BrianKopp / dynamo.rs
Created June 7, 2020 14:15
RustRusotoRocket_DynamoSaveDelete
async fn dynamo_save_user(client: &DynamoDbClient, table_name: String, user: User) -> Result<bool, &'static str> {
let input = PutItemInput{
table_name: table_name,
item: user.to_map(),
..Default::default()
};
match client.put_item(input).await {
Err(error) => {
println!("Error saving dynamo item {:?}", error);
Err("error saving dynamo item")
@BrianKopp
BrianKopp / dynamo.rs
Created June 7, 2020 14:11
RustRusotoRocket_DynamoGetItem
extern crate rusoto_dynamodb;
use rusoto_dynamodb::*;
async fn dynamo_get_user(client: &DynamoDbClient, table_name: String, id: String) -> Result<User, &'static str> {
let mut get_key = HashMap::new();
get_key.insert(
"id".to_string(),
AttributeValue {
s: Some(id.to_string()),
..Default::default()
@BrianKopp
BrianKopp / User.rs
Last active June 7, 2020 14:04
RustRusotoRocket_UserMapToFrom
extern crate rusoto_dynamodb;
use rusoto_dynamodb::AttributeValue;
use std::default::Default;
use std::result::Result;
impl User {
fn from_map(map: HashMap<String, AttributeValue>) -> Result<User, &'static str> {
return Ok(User {
id: match map.get("id") {
@BrianKopp
BrianKopp / Cargo.toml
Created June 7, 2020 13:51
RustRusotoRocket_CargoAddRusoto
[dependencies]
rusoto_core = "0.44.0"
rusoto_dynamodb = "0.44.0"