dilemma_tactix_lib/models/
choice.rs

1// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2// * Copyright (c) 2023-2024
3// *
4// * This project is dual-licensed under the MIT and Apache licenses.
5// *
6// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
7// ** APACHE 2.0 LICENSE
8// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
9// *
10// * Licensed under the Apache License, Version 2.0 (the "License");
11// * you may not use this file except in compliance with the License.
12// * You may obtain a copy of the License at
13// *
14// * http://www.apache.org/licenses/LICENSE-2.0
15// *
16// * Unless required by applicable law or agreed to in writing, software
17// * distributed under the License is distributed on an "AS IS" BASIS,
18// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// * See the License for the specific language governing permissions and
20// * limitations under the License.
21// *
22// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
23// * * MIT LICENSE
24// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
25// *
26// * Permission is hereby granted, free of charge, to any person obtaining a copy
27// * of this software and associated documentation files (the "Software"), to deal
28// * in the Software without restriction, including without limitation the rights
29// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30// * copies of the Software, and to permit persons to whom the Software is
31// * furnished to do so, subject to the following conditions:
32// *
33// * The above copyright notice and this permission notice shall be included in all
34// * copies or substantial portions of the Software.
35// *
36// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42// * SOFTWARE.
43// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
44
45// Import the fmt module for formatting
46use std::fmt;
47
48// Define an enum called Choice with two variants: Atlantis and Olympus
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub enum Choice {
51    Atlantis,
52    Olympus,
53}
54
55// Implement methods for the Choice enum
56impl Choice {
57    // Define a method to return the Atlantis variant of Choice
58    // Example: let my_choice = Choice::atlantis(); // my_choice is now
59    // Choice::Atlantis
60    #[must_use]
61    pub const fn atlantis() -> Self {
62        Self::Atlantis
63    }
64
65    // Define a method to return the Olympus variant of Choice
66    // Example: let my_choice = Choice::olympus(); // my_choice is now
67    // Choice::Olympus
68    #[must_use]
69    pub const fn olympus() -> Self {
70        Self::Olympus
71    }
72}
73
74// Implement the Display trait for the Choice enum
75// This allows a Choice to be formatted as a string using the "{}" format
76// specifier
77impl fmt::Display for Choice {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        // Write the string representation of the Choice to the provided formatter
80        // Example: println!("{}", Choice::Atlantis); // Prints: Atlantis
81        write!(f, "{self:?}")
82    }
83}
84
85#[cfg(test)]
86mod tests {
87
88    use super::*;
89
90    #[test]
91    fn test_choice_to_string() {
92        assert_eq!(Choice::Atlantis.to_string(), "Atlantis");
93
94        assert_eq!(Choice::Olympus.to_string(), "Olympus");
95    }
96
97    #[test]
98    fn test_choice_atlantis() {
99        assert_eq!(Choice::atlantis(), Choice::Atlantis);
100    }
101
102    #[test]
103    fn test_choice_olympus() {
104        assert_eq!(Choice::olympus(), Choice::Olympus);
105    }
106
107    #[test]
108    fn test_choice_display() {
109        assert_eq!(format!("{}", Choice::Atlantis), "Atlantis");
110
111        assert_eq!(format!("{}", Choice::Olympus), "Olympus");
112    }
113}