From a699803ce717f16779a44d4e9b55ae76c26a3dde Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 12:33:57 -0700 Subject: [PATCH 01/10] cargo fmt Standard Rust formatting practice --- rust/src/gildedrose/mod.rs | 18 +++++++++++------- rust/src/gildedrose/test.rs | 2 +- rust/src/main.rs | 23 +++++++++++++++++------ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/rust/src/gildedrose/mod.rs b/rust/src/gildedrose/mod.rs index f055f99d..17314625 100644 --- a/rust/src/gildedrose/mod.rs +++ b/rust/src/gildedrose/mod.rs @@ -4,35 +4,39 @@ use std::vec; pub struct Item { pub name: string::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} + Item { + name: name, + sell_in: sell_in, + quality: quality, + } } } pub struct GildedRose { - pub items: vec::Vec + pub items: vec::Vec, } impl GildedRose { pub fn new(items: vec::Vec) -> GildedRose { - GildedRose {items: items} + 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.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" { diff --git a/rust/src/gildedrose/test.rs b/rust/src/gildedrose/test.rs index afc11c88..1883cc40 100644 --- a/rust/src/gildedrose/test.rs +++ b/rust/src/gildedrose/test.rs @@ -1,4 +1,4 @@ -use super::{Item, GildedRose}; +use super::{GildedRose, Item}; #[test] pub fn foo() { diff --git a/rust/src/main.rs b/rust/src/main.rs index b56d07e6..22da85fb 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,7 +1,6 @@ - mod gildedrose; -use gildedrose::{Item, GildedRose}; +use gildedrose::{GildedRose, Item}; fn main() { let items = vec![ @@ -10,11 +9,23 @@ fn main() { 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( + 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) + Item::new(String::from("Conjured Mana Cake"), 3, 6), ]; let mut rose = GildedRose::new(items); From 2a53bf5db52745a61951ae0a951a94f7aa54c00d Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 12:35:37 -0700 Subject: [PATCH 02/10] Delete imports that are already in the prelude `Vec` and `String` are in the prelude, so qualifying them with `vec::Vec` and `string::String` is unnecessary. --- rust/Cargo.lock | 4 +++- rust/src/gildedrose/mod.rs | 9 +++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 524f449a..54e4db43 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" diff --git a/rust/src/gildedrose/mod.rs b/rust/src/gildedrose/mod.rs index 17314625..0c1bc16c 100644 --- a/rust/src/gildedrose/mod.rs +++ b/rust/src/gildedrose/mod.rs @@ -1,8 +1,5 @@ -use std::string; -use std::vec; - pub struct Item { - pub name: string::String, + pub name: String, pub sell_in: i32, pub quality: i32, } @@ -18,11 +15,11 @@ impl Item { } pub struct GildedRose { - pub items: vec::Vec, + pub items: Vec, } impl GildedRose { - pub fn new(items: vec::Vec) -> GildedRose { + pub fn new(items: Vec) -> GildedRose { GildedRose { items: items } } From 87f3e536a405529d4fd493a7a2ec28c132f08c6b Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 12:38:24 -0700 Subject: [PATCH 03/10] Risky: Move tests into mod.rs Usual Rust practice is to put tests in the same file they are testing. I don't see any magic in the texttests that would require tests and code to be in separate files, but I am not entirely sure, so I added the risky tag. --- rust/src/gildedrose/mod.rs | 13 ++++++++++++- rust/src/gildedrose/test.rs | 10 ---------- 2 files changed, 12 insertions(+), 11 deletions(-) delete mode 100644 rust/src/gildedrose/test.rs diff --git a/rust/src/gildedrose/mod.rs b/rust/src/gildedrose/mod.rs index 0c1bc16c..43565e4b 100644 --- a/rust/src/gildedrose/mod.rs +++ b/rust/src/gildedrose/mod.rs @@ -78,4 +78,15 @@ impl GildedRose { } #[cfg(test)] -mod test; +mod tests { + use super::{GildedRose, Item}; + + #[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/gildedrose/test.rs b/rust/src/gildedrose/test.rs deleted file mode 100644 index 1883cc40..00000000 --- a/rust/src/gildedrose/test.rs +++ /dev/null @@ -1,10 +0,0 @@ -use super::{GildedRose, Item}; - -#[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); -} From 972e00541a2eec223bc979a6b138ed2dda68f643 Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 12:39:57 -0700 Subject: [PATCH 04/10] Move gildedrose/mod.rs -> gildedrose.rs Avoiding putting simple one-file modules in their own folder. --- rust/src/{gildedrose/mod.rs => gildedrose.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rust/src/{gildedrose/mod.rs => gildedrose.rs} (100%) diff --git a/rust/src/gildedrose/mod.rs b/rust/src/gildedrose.rs similarity index 100% rename from rust/src/gildedrose/mod.rs rename to rust/src/gildedrose.rs From b9372efe6f2ac02026d59a3cc12f147d82f7bb59 Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 12:45:26 -0700 Subject: [PATCH 05/10] Use 2018-edition field init shorthand --- rust/src/gildedrose.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rust/src/gildedrose.rs b/rust/src/gildedrose.rs index 43565e4b..acb7d6e2 100644 --- a/rust/src/gildedrose.rs +++ b/rust/src/gildedrose.rs @@ -7,9 +7,9 @@ pub struct Item { impl Item { pub fn new(name: String, sell_in: i32, quality: i32) -> Item { Item { - name: name, - sell_in: sell_in, - quality: quality, + name, + sell_in, + quality, } } } @@ -20,7 +20,7 @@ pub struct GildedRose { impl GildedRose { pub fn new(items: Vec) -> GildedRose { - GildedRose { items: items } + GildedRose { items } } pub fn update_quality(&mut self) { From f21ed2ae131798db805775ef3f24a744a7614080 Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 12:53:02 -0700 Subject: [PATCH 06/10] Make main.rs clippy-clean --- rust/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index 22da85fb..6d0f0193 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -30,13 +30,13 @@ fn main() { 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!(""); + println!(); rose.update_quality(); } } From c88bdfd53e2b7361f957799c9e0042da9fa6d745 Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 13:00:57 -0700 Subject: [PATCH 07/10] Implement Display for Item This is a more idiomatic way of printing the contents of an object than reaching into its innards. It is also more 1-1 with the original C# code, which overrides toString for Item. --- rust/src/gildedrose.rs | 7 +++++++ rust/src/main.rs | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/rust/src/gildedrose.rs b/rust/src/gildedrose.rs index acb7d6e2..876da88c 100644 --- a/rust/src/gildedrose.rs +++ b/rust/src/gildedrose.rs @@ -1,3 +1,4 @@ +use std::fmt::{self, Display}; pub struct Item { pub name: String, pub sell_in: i32, @@ -14,6 +15,12 @@ impl Item { } } +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, } diff --git a/rust/src/main.rs b/rust/src/main.rs index 6d0f0193..ceda22dd 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -34,7 +34,7 @@ fn main() { println!("-------- day {} --------", i); println!("name, sellIn, quality"); for item in &rose.items { - println!("{}, {}, {}", item.name, item.sell_in, item.quality); + println!("{}", item); } println!(); rose.update_quality(); From 2ebbc987fb5df0c772b2734c37a13b62c209a3f0 Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 13:18:50 -0700 Subject: [PATCH 08/10] Make Item::new take Into 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. --- rust/src/gildedrose.rs | 6 +++--- rust/src/main.rs | 30 +++++++++--------------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/rust/src/gildedrose.rs b/rust/src/gildedrose.rs index 876da88c..9e387756 100644 --- a/rust/src/gildedrose.rs +++ b/rust/src/gildedrose.rs @@ -6,9 +6,9 @@ pub struct Item { } impl Item { - pub fn new(name: String, sell_in: i32, quality: i32) -> Item { + pub fn new(name: impl Into, sell_in: i32, quality: i32) -> Item { Item { - name, + name: name.into(), sell_in, quality, } @@ -90,7 +90,7 @@ mod tests { #[test] pub fn foo() { - let items = vec![Item::new(String::from("foo"), 0, 0)]; + let items = vec![Item::new("foo", 0, 0)]; let mut rose = GildedRose::new(items); rose.update_quality(); diff --git a/rust/src/main.rs b/rust/src/main.rs index ceda22dd..eb973caf 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -4,28 +4,16 @@ 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); From d897d295b7e034679f8eeedef44950b958370da2 Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 13:33:33 -0700 Subject: [PATCH 09/10] Update Cargo.toml; bump version number --- rust/Cargo.lock | 2 +- rust/Cargo.toml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 54e4db43..e9e570d7 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -2,5 +2,5 @@ # 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..00040bd8 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,4 +1,4 @@ [package] name = "rust" -version = "0.1.0" -authors = ["Michael Gerhaeuser "] +version = "0.2.0" +authors = ["Michael Gerhaeuser ", "rrokkam "] From 45e53e823b7aa04a2903efda39ced96436fcdab0 Mon Sep 17 00:00:00 2001 From: rrokkam Date: Sun, 19 Jul 2020 13:41:08 -0700 Subject: [PATCH 10/10] Bump to 2018 edition --- rust/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 00040bd8..875b0d44 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -2,3 +2,4 @@ name = "rust" version = "0.2.0" authors = ["Michael Gerhaeuser ", "rrokkam "] +edition = "2018"