GildedRose-Refactoring-Kata/rust/src/gildedrose.rs
rrokkam 2ebbc987fb Make Item::new take Into<String> as a name
This clears a lot of Rust-specific String boilerplate, so it's not
necessary to type String::from("foo") every time we want an item
with name "foo". It also makes the code look more similar to the C#
version of the code.

I am leaving the public struct members in because those are more
similar to the matching code in the other languages' implementations.
2020-07-19 13:51:27 -07:00

100 lines
2.8 KiB
Rust

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<String>, 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<Item>,
}
impl GildedRose {
pub fn new(items: Vec<Item>) -> 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);
}
}