From 52d3d5607abe62204cd9a057a9f8158dccd1de01 Mon Sep 17 00:00:00 2001 From: Michael Gerhaeuser Date: Sat, 3 Oct 2015 21:11:56 +0200 Subject: [PATCH] Add rust version --- rust/.gitignore | 1 + rust/Cargo.lock | 4 ++ rust/Cargo.toml | 4 ++ rust/src/gildedrose/mod.rs | 80 +++++++++++++++++++++++++++++++++++++ rust/src/gildedrose/test.rs | 10 +++++ rust/src/main.rs | 31 ++++++++++++++ 6 files changed, 130 insertions(+) create mode 100644 rust/.gitignore create mode 100644 rust/Cargo.lock create mode 100644 rust/Cargo.toml create mode 100644 rust/src/gildedrose/mod.rs create mode 100644 rust/src/gildedrose/test.rs create mode 100644 rust/src/main.rs diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 00000000..2f7896d1 --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/rust/Cargo.lock b/rust/Cargo.lock new file mode 100644 index 00000000..524f449a --- /dev/null +++ b/rust/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "rust" +version = "0.1.0" + diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 00000000..c0fef86b --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "rust" +version = "0.1.0" +authors = ["Michael Gerhaeuser "] diff --git a/rust/src/gildedrose/mod.rs b/rust/src/gildedrose/mod.rs new file mode 100644 index 00000000..f055f99d --- /dev/null +++ b/rust/src/gildedrose/mod.rs @@ -0,0 +1,80 @@ +use std::string; +use std::vec; + +pub struct Item { + pub name: string::String, + pub sell_in: i32, + pub quality: i32 +} + +impl Item { + pub fn new(name: String, sell_in: i32, quality: i32) -> Item { + Item {name: name, sell_in: sell_in, quality: quality} + } +} + +pub struct GildedRose { + pub items: vec::Vec +} + +impl GildedRose { + pub fn new(items: vec::Vec) -> GildedRose { + GildedRose {items: items} + } + + pub fn update_quality(&mut self) { + for item in &mut self.items { + if item.name != "Aged Brie" && item.name != "Backstage passes to a TAFKAL80ETC concert" { + if item.quality > 0 { + if item.name != "Sulfuras, Hand of Ragnaros" { + item.quality = item.quality - 1; + } + } + } else { + if item.quality < 50 + { + item.quality = item.quality + 1; + + if item.name == "Backstage passes to a TAFKAL80ETC concert" { + if item.sell_in < 11 { + if item.quality < 50 { + item.quality = item.quality + 1; + } + } + + if item.sell_in < 6 { + if item.quality < 50 { + item.quality = item.quality + 1; + } + } + } + } + } + + if item.name != "Sulfuras, Hand of Ragnaros" { + item.sell_in = item.sell_in - 1; + } + + if item.sell_in < 0 { + if item.name != "Aged Brie" { + if item.name != "Backstage passes to a TAFKAL80ETC concert" { + if item.quality > 0 { + if item.name != "Sulfuras, Hand of Ragnaros" { + item.quality = item.quality - 1; + } + } + } else { + item.quality = item.quality - item.quality; + } + } else { + if item.quality < 50 { + item.quality = item.quality + 1; + } + } + } + } + } +} + +#[cfg(test)] +mod test; diff --git a/rust/src/gildedrose/test.rs b/rust/src/gildedrose/test.rs new file mode 100644 index 00000000..afc11c88 --- /dev/null +++ b/rust/src/gildedrose/test.rs @@ -0,0 +1,10 @@ +use super::{Item, GildedRose}; + +#[test] +pub fn foo() { + let items = vec![Item::new(String::from("foo"), 0, 0)]; + let mut rose = GildedRose::new(items); + rose.update_quality(); + + assert_eq!("fixme", rose.items[0].name); +} diff --git a/rust/src/main.rs b/rust/src/main.rs new file mode 100644 index 00000000..b56d07e6 --- /dev/null +++ b/rust/src/main.rs @@ -0,0 +1,31 @@ + +mod gildedrose; + +use gildedrose::{Item, GildedRose}; + +fn main() { + let items = vec![ + Item::new(String::from("+5 Dexterity Vest"), 10, 20), + Item::new(String::from("Aged Brie"), 2, 0), + Item::new(String::from("Elixir of the Mongoose"), 5, 7), + Item::new(String::from("Sulfuras, Hand of Ragnaros"), 0, 80), + Item::new(String::from("Sulfuras, Hand of Ragnaros"), -1, 80), + Item::new(String::from("Backstage passes to a TAFKAL80ETC concert"), 15, 20), + Item::new(String::from("Backstage passes to a TAFKAL80ETC concert"), 10, 49), + Item::new(String::from("Backstage passes to a TAFKAL80ETC concert"), 5, 49), + // this conjured item does not work properly yet + Item::new(String::from("Conjured Mana Cake"), 3, 6) + ]; + let mut rose = GildedRose::new(items); + + println!("OMGHAI!"); + for i in (0..30) { + println!("-------- day {} --------", i); + println!("name, sellIn, quality"); + for item in &rose.items { + println!("{}, {}, {}", item.name, item.sell_in, item.quality); + } + println!(""); + rose.update_quality(); + } +}