use std::fmt::{self, Display}; pub struct Item { pub name: String, pub sell_in: i32, pub quality: i32, } impl Item { 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, } impl GildedRose { 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.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 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); } }