1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
//! This module defines the [`GiftWrap`] enum which is the base generic type used in this crate. It
//! represents a real-life gift box which can hold any gift that can fit within the box. The enum
//! has two variants:
//!
//! * Gifts(T) - Which represents a gift box with whatever `Gifts` contained as `T`.
//! * Empty - Which represents an empty gift box.
//!
//! # Examples
//!
//! ```
//! use giftbox::giftbox::GiftBox;
//! use giftbox::gifttag::GiftTag;
//! use giftbox::giftwrap::GiftWrap;
//! use giftbox::patterns::Patterns;
//! let filled_box = GiftBox::fill(Some(["Toys", "Candy", "Money"]));
//! let tag = GiftTag::write(
//! "Bob".to_string(),
//! "Sally".to_string(),
//! "Happy Cake Day!".to_string()
//! );
//! let wrapped_box = filled_box.wrap(
//! Patterns::Polkadots,
//! true,
//! Some(tag)
//! );
//! assert_eq!(
//! wrapped_box,
//! {
//! GiftWrap {
//! contents:{
//! GiftBox::Gifts(["Toys", "Candy", "Money"])
//! },
//! pattern: Patterns::Polkadots,
//! has_bow: true,
//! tag: Some(
//! GiftTag {
//! recipient: "Bob".to_string(),
//! sender: "Sally".to_string(),
//! message: "Happy Cake Day!".to_string()
//! }
//! )
//! }
//! }
//! )
//! ```
use crate::giftbox::GiftBox::*;
use crate::gifttag::GiftTag;
use crate::giftwrap::GiftWrap;
use crate::patterns::Patterns;
use std::fmt::*;
/// A `GiftBox` type for Rust that could contain any type of gift that can be represented as a Rust
/// type.
///
/// For example, the `GiftBox` can contain:
///
/// **A `u8` gift represented as:**
/// ```
/// use giftbox::giftbox::GiftBox;
/// let gift_of_42 = GiftBox::Gifts(42);
/// ```
/// **A nice message in a `String` as:**
/// ```
/// use giftbox::giftbox::GiftBox;
/// let gift_of_string = GiftBox::Gifts(String::from("If anything is worth doing, do it with all
/// your heart. <3"));
/// ```
///
/// # `GiftBox` States
/// `GiftBox` has two possible states:
/// * The `GiftBox` contains `Gifts(T)` where the value of `T` is the gift.
/// * The `GiftBox` is `Empty`.
///
/// # Methods
///
/// ## fill()
/// Fill a `GiftBox` with the [`GiftBox::fill()`] method. Example:
/// ```
/// use giftbox::giftbox::GiftBox;
/// let filled_gift_box = GiftBox::fill(Some(["Toys", "Candy", "Money"]));
/// ```
/// This will create an instance of a filled gift box called `filled_gift_box`. The
/// `filled_gift_box` is the same as:
/// ```text
/// GiftBox::Gifts(["Toys", "Candy", "Money"])
/// ```
///
/// ## fill_keeping_some()
/// You can fill a box and keep some for yourself and makes the contents of `GiftBox` contain
/// keep the [`std::option::Option`] too, with the [`GiftBox::fill_keeping_some()`] method:
/// ```
/// use giftbox::giftbox::GiftBox;
/// let some_filled_box = GiftBox::fill_keeping_some(Some("Chocolate"));
/// assert_eq!(some_filled_box.open(), Some("Chocolate"));
/// ```
///
/// ## open()
/// You can open a `GiftBox` with [`GiftBox::open()`] to get the contents of the `GiftBox`. Example:
/// ```
/// use giftbox::giftbox::GiftBox;
/// let filled_gift_box = GiftBox::fill(Some(["Toys", "Candy", "Money"]));
/// let gifts = filled_gift_box.open();
/// assert_eq!(gifts, ["Toys", "Candy", "Money"]);
/// ```
///
/// ### Opening an `Empty` box causes a panic!
/// If you open an empty box, represented as ```GiftBox::Empty``` then the compiler will panic.
/// Example:
/// ```should_panic
/// use giftbox::giftbox::GiftBox;
/// let empty_box = GiftBox::Empty;
/// empty_box.open()
/// // ^^^This will cause a panic at compile time.^^^
/// ```
///
/// You can also create an empty box by filling a box with a `None` from [`std::option::Option`].
/// ```should_panic
/// use giftbox::giftbox::GiftBox;
/// let another_empty_box = GiftBox::fill(None);
/// another_empty_box.open()
/// // ^^^This will also cause a panic at compile time.^^^
/// ```
///
/// # About the Contents of a `GiftBox` and Memory Allocation
/// While a `GiftBox` could contain any type `T` within `GiftBox::Gifts(T)`, it should be remembered
/// that when creating a `GiftBox` Rust will associate another type (type `T` in this case) with the
/// `GiftBox` type to create a combined type. In this case you have to choose one specific `T` value
/// each time you declare a variable that stores a `GiftBox`.
///
/// The moment you create a `GiftBox`, you have to decide what it is a `GiftBox` of, so that the
/// memory for it can be laid out. At the moment that `GiftBox` is created it needs to decide what
/// it is a `GiftBox` "of" the moment it is put into memory. You cannot create an empty box, which
/// you can decide what you want it to be a box of later, as the compiler would not know how to lay
/// out the memory.
///
/// ## Declaring What Type `T` is in `GiftBox::Gifts(T)`
/// The compiler must always know what type `T` is for every variable. If the value is a concrete
/// instance of `GiftBox` then it must know the `T`. Even if an empty `GiftBox` is declared the
/// compiler has to know what type `T` could be in order to correctly assign memory. When declaring
/// a GiftBox that contains something, the compiler is able to automatically infer what type `T` is
/// so you do not have to declare exactly what type `T` is. In the following example, the compiler
/// knows how much memory to allocate for this GiftBox because the type of `T` can be inferred from
/// the integer `42`:
/// ```
/// use giftbox::giftbox::GiftBox;
/// let integer_gift = GiftBox::Gifts(42);
/// ```
/// However, if you are declaring an empty `GiftBox`, you must associate the empty type with the
/// `GiftBox` by adding the type annotation to the empty box. For example, you could create an empty
/// `GiftBox` with a pointer type, such as:
/// ```
/// use giftbox::giftbox::GiftBox;
/// let empty_box: GiftBox<()> = GiftBox::Empty;
/// ```
/// That tells the compiler "ah, so for the empty_box variable, the `T` associated with `GiftBox` in
/// that instance is the empty type". If you wanted to assign a gift value to that variable, you
/// would then only be allowed to put in an empty type instance `empty_box = GiftBox::Gifts(())`.
/// You could not change the type for `empty_box`. For example, the following would not compile:
/// ```compile_fail
/// use giftbox::giftbox::GiftBox;
/// let mut empty_box: GiftBox<()> = GiftBox::Empty;
/// empty_box = GiftBox::Gifts(true);
/// // ^^^This would not compile!^^^
///
/// let mut int_box = GiftBox::Gifts(42);
/// num_box = GiftBox::Gifts("Words");
/// // ^^^This would not compile as well!^^^
/// ```
///
/// The compiler must always know what `T` is for every variable. If the value is a concrete
/// instance of `GiftBox` then it must know the type for `T`.
///
/// # Generic Functions and `GiftBox`
/// If you would like to accept a GiftBox, but this particular function does not care what kind of
/// gift is in the gift box, then you can make the function itself generic. Example:
/// ```
/// use giftbox::giftbox::GiftBox;
/// fn open_gift_or_panic<T>(gift: GiftBox<T>) -> T {
/// return match gift {
/// GiftBox::Gifts(the_gift) => the_gift,
/// GiftBox::Empty => panic!("I thought I was going to get a gift! D: "),
/// }
/// }
/// ```
/// Here, the function works for any `T` because the code works regardless of what the type in the
/// gift is, it only needs to return the type which is whatever type was in the gift box. In the
/// case of the generic function, it passes the responsibility of figuring out what the `T` is on to
/// the caller of the function.
#[derive(Copy, Debug, PartialEq, PartialOrd)]
pub enum GiftBox<T> {
Gifts(T),
Empty,
}
impl<T> GiftBox<T> {
/// The `fill(t: Option<T>)` method accepts an [`std::option::Option`] and returns a `GiftBox`
/// with either `GiftBox::Gifts(t)` if `Some(t)` was provided or a `GiftBox::Empty` if
/// `None` is provided.
///
/// # Arguments
/// * `t: Option<T>` - `t` accepts an [`std::option::Option`]
///
/// # Returns
/// Returns a `GiftBox<T>`.
///
/// # Example
/// Filling a `GiftBox` with `Some(T)`:
/// ```
/// use giftbox::giftbox::GiftBox;
/// let filled_box = GiftBox::fill(Some(["Toys", "Candy", "Money"]));
/// assert_eq!(filled_box, GiftBox::Gifts(["Toys", "Candy", "Money"]));
/// ```
/// Filling a `GiftBox` with `None`:
/// ```
/// use giftbox::giftbox::GiftBox;
/// let empty_box: GiftBox<()> = GiftBox::fill(None);
/// assert_eq!(empty_box, GiftBox::Empty);
/// ```
pub fn fill(t: Option<T>) -> GiftBox<T> {
match t {
Some(t) => GiftBox::Gifts(t),
None => GiftBox::Empty,
}
}
/// The `fill_keeping_some(t: Option<T>)` method accepts an
/// [`std::option::Option`] and returns a `GiftBox` with either `GiftBox::Gifts(Some(t))` if
/// `Some(t)` was provided or a `GiftBox::Empty` if `None` is provided.
///
/// # Arguments
/// * `t: Option<T>` - `t` accepts an [`std::option::Option`]
///
/// # Returns
/// Returns a `GiftBox<Option<T>>`.
///
/// # Example
/// Filling a `GiftBox` with `Some(T)`:
/// ```
/// use giftbox::giftbox::GiftBox;
/// let filled_box = GiftBox::fill_keeping_some(Some(["Toys", "Candy", "Money"]));
/// assert_eq!(filled_box, GiftBox::Gifts(Some(["Toys", "Candy", "Money"])));
/// ```
/// Filling a `GiftBox` with `None`:
/// ```<T>
/// use giftbox::giftbox::GiftBox;
/// use std::option::Option;
/// let empty_box: GiftBox<Option<T>> = GiftBox::fill_keeping_some(None);
/// assert_eq!(empty_box, GiftBox::Empty);
/// ```
pub fn fill_keeping_some(t: Option<T>) -> GiftBox<Option<T>> {
match t {
Some(t) => GiftBox::Gifts(Some(t)),
None => GiftBox::Empty,
}
}
/// The `open()` method takes a GiftBox and returns the contents of that GiftBox.
///
/// # Arguments
/// * `self` only.
///
/// # Returns
/// Returns `T` where `T` is the contents of a `GiftBox:Gifts(<T>)`.
///
/// # Panics!
/// The `open()` method will panic if used on a GiftBox that is empty (`GiftBox::Empty`).
///
/// # Example
/// ```
/// use giftbox::giftbox::GiftBox;
/// let filled_box = GiftBox::fill(Some(vec![1, 3, 5, 7]));
/// assert_eq!(filled_box.open(), vec![1, 3, 5, 7]);
/// ```
pub fn open(self) -> T {
match self {
Gifts(gift) => gift,
Empty => panic!("Opened an empty gift box!"),
}
}
/// The `wrap` method takes a `GiftBox` and some `GiftWrap` parameters to return a `GiftBox`
/// contained within `GiftWrap`.
///
/// # Arguments
/// * `self`
/// * `pattern` - Accepts a [`Patterns`] enum type representing the type of pattern of the
/// `GiftWrap`.
/// * `has_bow` - Accepts a boolean representing whether or not the `GiftWrap` has a bow.
/// * `tag` - Accepts an Option containing a `Tag` type struct or `None` representing a
/// `GiftTag` that may or may not be included.
///
/// # Returns
/// Returns `GiftWrap<GiftBox<T>>`
///
/// # Example
/// ```
/// use giftbox::giftbox::GiftBox;
/// use giftbox::gifttag::GiftTag;
/// use giftbox::giftwrap::GiftWrap;
/// use giftbox::patterns::Patterns;
/// let filled_box = GiftBox::fill(Some(["Toys", "Candy", "Money"]));
/// let tag = GiftTag::write(
/// "Bob".to_string(),
/// "Sally".to_string(),
/// "Happy Cake Day!".to_string()
/// );
/// let wrapped_box = filled_box.wrap(
/// Patterns::Polkadots,
/// true,
/// Some(tag)
/// );
/// assert_eq!(
/// wrapped_box,
/// {
/// GiftWrap {
/// contents:{
/// GiftBox::Gifts(["Toys", "Candy", "Money"])
/// },
/// pattern: Patterns::Polkadots,
/// has_bow: true,
/// tag: Some(
/// GiftTag {
/// recipient: "Bob".to_string(),
/// sender: "Sally".to_string(),
/// message: "Happy Cake Day!".to_string()
/// }
/// )
/// }
/// }
/// )
/// ```
pub fn wrap(
self,
pattern: Patterns,
has_bow: bool,
tag: Option<GiftTag>,
) -> GiftWrap<GiftBox<T>> {
GiftWrap {
contents: self,
pattern,
has_bow,
tag,
}
}
}
/// This `Clone` allows for GiftBox<T> to utilize `#[derive(Copy)]`.
impl<T: Clone> Clone for GiftBox<T> {
fn clone(&self) -> Self {
match self {
Gifts(x) => Gifts(x.clone()),
Empty => Empty,
}
}
fn clone_from(&mut self, source: &Self) {
match (self, source) {
(Gifts(to), Gifts(from)) => to.clone_from(from),
(to, from) => *to = from.clone(),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[should_panic]
fn open_empty_box_panic() {
let empty_box: GiftBox<()> = GiftBox::Empty;
empty_box.open();
}
#[test]
fn filling_box() {
let filled_box = GiftBox::fill(Some(["Toys", "Candy", "Money"]));
assert_eq!(filled_box, GiftBox::Gifts(["Toys", "Candy", "Money"]));
}
#[test]
fn open_filled_box() {
let filled_box = GiftBox::fill(Some(vec![1, 3, 5, 7]));
assert_eq!(filled_box.open(), vec![1, 3, 5, 7]);
}
#[test]
fn filling_with_some() {
let some_filled_box = GiftBox::fill_keeping_some(Some("Chocolate"));
assert_eq!(some_filled_box.open(), Some("Chocolate"));
}
#[test]
fn filling_with_none() {
let empty_box: GiftBox<()> = GiftBox::fill(None);
assert_eq!(empty_box, GiftBox::Empty);
}
}