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
//! This module defines the [`GiftWrap`] struct. This can be used by [`crate::giftbox::GiftBox`] to
//! wrap itself. It is meant to represent wrapping paper used to wrap a gift box. The struct has
//! four fields:
//! * **contents** - which is a generic data type that can hold any Rust definable type.
//! * **pattern** - the [`crate::patterns::Patterns`] enum which represents the type of wrapping
//! paper.
//! * **has_box** - a boolean which represents whether or not the `GiftWrap` has a bow.
//! * **tag** - an `Option<GiftTag>`, where the `GiftTag` is a struct representing a gift tag the
//! `GiftWrap` that contains the recipient, sender, and a message.
//!
//! # Examples
//! Wrap and unwrap a [`crate::giftbox::GiftBox`] with a tag:
//! ```
//! use giftbox::giftbox::GiftBox;
//! use giftbox::gifttag::GiftTag;
//! 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)
//! );
//! let unwrapped_box = wrapped_box.unwrap();
//! assert_eq!(unwrapped_box, filled_box);
//! ```
//!
//! todo!() Turn GiftWrap into a trait.

use crate::gifttag::GiftTag;
use crate::patterns::Patterns;
use std::fmt::*;

/// A `GiftWrap` type for Rust which represents gift wrap that can be wrapped around any other
/// type that can be represented as a Rust type.
///
/// `GiftWrap` is a Rust struct which has the following parameters:
/// * `contents` which can be any type `T`.
/// * `pattern` which represents the pattern of the `GiftWrap` defined in the `Patterns` enum.
/// * `has_bow` which is a boolean which represents whether or not the `GiftWrap` has a bow.
/// * `tag` which is an `Option<GiftTag>`, where the `GiftTag` is a struct representing a gift tag
/// on the `GiftWrap` that contains the recipient, sender, and a message.
///
/// # Methods
/// ## unwrap()
/// Unwrap `GiftWrap` to get its contents with the [`GiftWrap::unwrap()`] method. Example:
/// ```
/// use giftbox::giftbox::GiftBox;
/// use giftbox::giftwrap::GiftWrap;
/// use giftbox::patterns::Patterns;
/// let wrapped_string_gift = GiftWrap {
///     contents: GiftBox::Gifts("String of words".to_string()),
///     pattern: Patterns::Sparkles,
///     has_bow: true,
///     tag: None
/// };
/// let unwrapped_string_gift = wrapped_string_gift.unwrap();
/// assert_eq!(unwrapped_string_gift, GiftBox::Gifts("String of words".to_string()));
/// ```
///
/// ## read_tag()
/// "Read" the `GiftTag` of `GiftWrap`. More specifically, get a String of the `GiftTags` contents
/// with the [`GiftWrap::read_tag()`] Example:
/// ```
/// use giftbox::giftbox::GiftBox;
/// use giftbox::gifttag::GiftTag;
/// 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.read_tag(),
///     "To: Bob,\nFrom: Sally,\nMessage: Happy Cake Day!"
/// );
/// ```
#[derive(Debug, PartialEq)]
pub struct GiftWrap<T> {
    pub contents: T,
    pub pattern: Patterns,
    pub has_bow: bool,
    pub tag: Option<GiftTag>,
}

impl<T> GiftWrap<T> {
    /// The `unwrap()` method takes the `GiftWrap` and unwraps it to reveal its contents.
    ///
    /// # Arguments
    /// * `self` only.
    ///
    /// # Returns
    /// Returns `T` where `T` is the contents of `Giftwrap.contents`.
    ///
    /// # Example
    /// ```
    /// use giftbox::giftbox::GiftBox;
    /// use giftbox::gifttag::GiftTag;
    /// 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)
    /// );
    /// let unwrapped_box = wrapped_box.unwrap();
    /// assert_eq!(unwrapped_box, filled_box);
    /// ```
    pub fn unwrap(self) -> T {
        self.contents
    }

    /// The `read_tag()` method takes a `GiftWrap` and returns the contents of a `GiftTag` as a
    /// String. If there is no `GiftTag` (`self.tag` is `None`) then a default String is returned.
    ///
    /// # Arguments
    /// * `self` only.
    ///
    /// # Returns
    /// Returns a String returned from [`GiftTag::read()`] if there is `Some(tag)`. Otherwise, if
    /// there is `None` it returns a default String.
    ///
    /// # Example
    /// ```
    /// use giftbox::giftbox::GiftBox;
    /// use giftbox::gifttag::GiftTag;
    /// 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.read_tag(),
    ///     "To: Bob,\nFrom: Sally,\nMessage: Happy Cake Day!"
    /// );
    /// ```
    pub fn read_tag(self) -> String {
        match self.tag {
            Some(tag) => tag.read(),
            None => "There was no tag to read.".to_string(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::giftbox::GiftBox;

    #[test]
    fn wrap_gift_box_with_tag() {
        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(),
                }),
            }
        })
    }

    #[test]
    fn wrap_and_unwrap_gift_box() {
        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));
        let unwrapped_box = wrapped_box.unwrap();
        assert_eq!(unwrapped_box, filled_box);
    }

    #[test]
    fn wrap_and_open_gift_box_with_tag() {
        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.unwrap().open(), ["Toys", "Candy", "Money"]);
    }

    #[test]
    fn write_and_read_tag() {
        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.read_tag(),
            "To: Bob,\nFrom: Sally,\nMessage: Happy Cake Day!"
        );
    }

    #[test]
    fn attempt_to_read_none_tag() {
        let wrapped_nothing = GiftWrap {
            contents: (),
            pattern: Patterns::KraftPaper,
            has_bow: false,
            tag: None,
        };
        assert_eq!(wrapped_nothing.read_tag(), "There was no tag to read.");
    }
}