diff options
| author | Andreas Grois <andi@grois.info> | 2023-04-02 21:59:22 +0200 |
|---|---|---|
| committer | Andreas Grois <andi@grois.info> | 2023-04-02 21:59:22 +0200 |
| commit | cebdd3be32d50be379663e92d4428e6bba19ba51 (patch) | |
| tree | 8c5b57f2e2ed3c089e0546fdb95ef8c2082d1952 /examples/text-adventure/data.rs | |
| parent | 418f514fc46f45ae2901753e3398adb33664bed9 (diff) | |
Run cargo fmt
I think readability was better before that...
Diffstat (limited to 'examples/text-adventure/data.rs')
| -rw-r--r-- | examples/text-adventure/data.rs | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/examples/text-adventure/data.rs b/examples/text-adventure/data.rs index 3e9c49e..bf78977 100644 --- a/examples/text-adventure/data.rs +++ b/examples/text-adventure/data.rs @@ -1,13 +1,13 @@ #[derive(Clone, Copy)] -pub enum Speaker{ +pub enum Speaker { Partner, DeliLady, Cashier, } -impl Speaker{ - pub fn text_description(self)->&'static str{ - match self{ +impl Speaker { + pub fn text_description(self) -> &'static str { + match self { Speaker::Partner => "Your partner", Speaker::DeliLady => "The lady behind the deli counter", Speaker::Cashier => "The cashier", @@ -16,7 +16,7 @@ impl Speaker{ } #[derive(Clone, Copy)] -pub enum Mood{ +pub enum Mood { Friendly, Confused, Happy, @@ -25,9 +25,9 @@ pub enum Mood{ Apologetic, } -impl Mood{ - pub fn text_description(self)->&'static str{ - match self{ +impl Mood { + pub fn text_description(self) -> &'static str { + match self { Mood::Friendly => "a friendly expression", Mood::Confused => "a confused expression", Mood::Happy => "a happy expression", @@ -39,7 +39,7 @@ impl Mood{ } #[derive(Clone, Copy, PartialEq, Eq)] -pub enum Location{ +pub enum Location { Entrance, Deli, Checkout, @@ -47,9 +47,9 @@ pub enum Location{ Shelves, } -impl Location{ +impl Location { //Used if we want to present the location in text. If we were writing a visual novel, we would offer another function that returns the respective assets. - pub fn get_text_description(self)->&'static str{ + pub fn get_text_description(self) -> &'static str { match self { Location::Entrance => "You are at the entrance area of the super market. Behind you is the parking lot, in front the inviting automated doors of the entrance. Your partner is here with you.", Location::Deli => "This is the area with the deli counter. There is a lady wearing a hair protector and plastic gloves standing behind the presentation tray.", @@ -60,8 +60,8 @@ impl Location{ } } -#[derive(Copy,Clone,PartialEq,Eq)] -pub enum Item{ +#[derive(Copy, Clone, PartialEq, Eq)] +pub enum Item { //refrigerators Milk, Yoghurt, @@ -81,7 +81,7 @@ pub enum Item{ } impl Item { - fn price(self) -> usize { + fn price(self) -> usize { match self { Item::SausageRoll | Item::FishSandwich | Item::Shots => 300, Item::Pickles | Item::Pulp => 250, @@ -110,7 +110,7 @@ impl Item { } } -impl Location{ +impl Location { pub fn items(self) -> Vec<Item> { match self { Location::Entrance => Vec::default(), @@ -126,34 +126,34 @@ impl Location{ //I left it as Clone intentionally, to illustrate how one can work around the limitation of it not being Copy. #[derive(Clone, Default)] pub struct Inventory { - pub items : Vec<Item>, + pub items: Vec<Item>, } -impl Inventory{ - pub fn has_item_from_room(&self, room : Location) -> bool { +impl Inventory { + pub fn has_item_from_room(&self, room: Location) -> bool { let items_from_room = room.items(); self.items.iter().any(|i| items_from_room.contains(i)) } - pub fn try_add(self, item : Item) -> Result<Self, Self>{ + pub fn try_add(self, item: Item) -> Result<Self, Self> { if self.items.len() < 3 { let mut items = self.items; items.push(item); //am I the only one that hates that push doesn't return the updated vec? - Ok(Inventory{items}) + Ok(Inventory { items }) } else { Err(self) } } - pub fn try_remove(mut self, item : Item) -> Result<Self, Self>{ + pub fn try_remove(mut self, item: Item) -> Result<Self, Self> { let idx = self.items.iter().position(|i| *i == item); match idx { Some(idx) => { self.items.swap_remove(idx); Ok(self) - }, + } None => Err(self), } } - pub fn get_money() -> &'static str{ + pub fn get_money() -> &'static str { "€10.00" //It doesn't change. Can as well be a constant. } pub fn total_price(&self) -> usize { @@ -162,4 +162,4 @@ impl Inventory{ pub fn can_afford(&self) -> bool { self.total_price() <= 1000_usize } -}
\ No newline at end of file +} |
