diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 524f449a..e9e570d7 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1,4 +1,6 @@ -[root] +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] name = "rust" -version = "0.1.0" +version = "0.2.0" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index c0fef86b..875b0d44 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,4 +1,5 @@ [package] name = "rust" -version = "0.1.0" -authors = ["Michael Gerhaeuser "] +version = "0.2.0" +authors = ["Michael Gerhaeuser ", "rrokkam "] +edition = "2018" diff --git a/rust/src/gildedrose/mod.rs b/rust/src/gildedrose.rs similarity index 69% rename from rust/src/gildedrose/mod.rs rename to rust/src/gildedrose.rs index f055f99d..9e387756 100644 --- a/rust/src/gildedrose/mod.rs +++ b/rust/src/gildedrose.rs @@ -1,38 +1,46 @@ -use std::string; -use std::vec; - +use std::fmt::{self, Display}; pub struct Item { - pub name: string::String, + pub name: String, pub sell_in: i32, - pub quality: 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 fn new(name: impl Into, sell_in: i32, quality: i32) -> Item { + Item { + name: name.into(), + sell_in, + quality, + } + } +} + +impl Display for Item { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}, {}, {}", self.name, self.sell_in, self.quality) } } pub struct GildedRose { - pub items: vec::Vec + pub items: Vec, } impl GildedRose { - pub fn new(items: vec::Vec) -> GildedRose { - GildedRose {items: items} + pub fn new(items: Vec) -> GildedRose { + GildedRose { 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.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 - { + if item.quality < 50 { item.quality = item.quality + 1; if item.name == "Backstage passes to a TAFKAL80ETC concert" { @@ -77,4 +85,15 @@ impl GildedRose { } #[cfg(test)] -mod test; +mod tests { + use super::{GildedRose, Item}; + + #[test] + pub fn foo() { + let items = vec![Item::new("foo", 0, 0)]; + let mut rose = GildedRose::new(items); + rose.update_quality(); + + assert_eq!("fixme", rose.items[0].name); + } +} diff --git a/rust/src/gildedrose/test.rs b/rust/src/gildedrose/test.rs deleted file mode 100644 index afc11c88..00000000 --- a/rust/src/gildedrose/test.rs +++ /dev/null @@ -1,10 +0,0 @@ -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 index b56d07e6..eb973caf 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,31 +1,30 @@ - mod gildedrose; -use gildedrose::{Item, GildedRose}; +use gildedrose::{GildedRose, Item}; 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), + Item::new("+5 Dexterity Vest", 10, 20), + Item::new("Aged Brie", 2, 0), + Item::new("Elixir of the Mongoose", 5, 7), + Item::new("Sulfuras, Hand of Ragnaros", 0, 80), + Item::new("Sulfuras, Hand of Ragnaros", -1, 80), + Item::new("Backstage passes to a TAFKAL80ETC concert", 15, 20), + Item::new("Backstage passes to a TAFKAL80ETC concert", 10, 49), + Item::new("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) + Item::new("Conjured Mana Cake", 3, 6), ]; let mut rose = GildedRose::new(items); println!("OMGHAI!"); - for i in (0..30) { + 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!("{}", item); } - println!(""); + println!(); rose.update_quality(); } }