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
use std::fmt;
use std::mem;

/// A semver bump level
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum BumpLevel {
    Major,
    Minor,
    Patch,
    None,
}

impl BumpLevel {
    /// Return the name of this bump level
    pub fn name(&self) -> &'static str {
        match *self {
            BumpLevel::Major => "Major",
            BumpLevel::Minor => "Minor",
            BumpLevel::Patch => "Patch",
            BumpLevel::None => "None",
        }
    }
}

/// A specific commit type
#[derive(PartialEq, Eq, Copy, Clone)]
pub enum CommitType {
    Breaking,
    Feature,
    Bugfix,
    Other,
    Meta,
}

/// Iterator over the different commit types
pub struct CommitTypeIterator {
    current: Option<CommitType>
}

impl CommitType {
    /// Return the first commit type (Breaking)
    pub fn first_variant() -> CommitType { CommitType::Breaking }

    /// Return the last commit variant (Meta)
    pub fn last_variant() -> CommitType { CommitType::Meta }

    /// Given a commit type, return the next commit type
    pub fn next_variant(&self) -> Option<CommitType> {
        match *self {
            CommitType::Breaking => Some(CommitType::Feature),
            CommitType::Feature => Some(CommitType::Bugfix),
            CommitType::Bugfix => Some(CommitType::Other),
            CommitType::Other => Some(CommitType::Meta),
            CommitType::Meta => None,
        }
    }

    /// Given a commit type, return the previous commit type
    pub fn prev_variant(&self) -> Option<CommitType> {
        match *self {
            CommitType::Breaking => None,
            CommitType::Feature => Some(CommitType::Breaking),
            CommitType::Bugfix => Some(CommitType::Feature),
            CommitType::Other => Some(CommitType::Bugfix),
            CommitType::Meta => Some(CommitType::Other),
        }
    }

    /// Return an iterator over all commit types
    pub fn iter_variants() -> CommitTypeIterator {
        CommitTypeIterator { current: Some(CommitType::first_variant()) }
    }

    /// Return the emoji for this commit type
    pub fn emoji(&self) -> &'static str {
        match *self {
            CommitType::Breaking => "💥",
            CommitType::Feature => "🎉",
            CommitType::Bugfix => "🐛",
            CommitType::Other => "🔥",
            CommitType::Meta => "🌹",
        }
    }

    /// Return the bump level for this commit type
    pub fn bump_level(&self) -> BumpLevel {
        match *self {
            CommitType::Breaking => BumpLevel::Major,
            CommitType::Feature => BumpLevel::Minor,
            CommitType::Bugfix => BumpLevel::Patch,
            CommitType::Other => BumpLevel::Patch,
            CommitType::Meta => BumpLevel::None,
        }
    }

    /// Return the description for this commit type
    pub fn description(&self) -> &'static str {
        match *self {
            CommitType::Breaking => "Breaking change",
            CommitType::Feature => "New functionality",
            CommitType::Bugfix => "Bugfix",
            CommitType::Other => "Cleanup / Performance",
            CommitType::Meta => "Meta",
        }
    }
}

impl fmt::Debug for CommitType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "CommitType {{ {} }}", self.emoji())
    }
}

impl Iterator for CommitTypeIterator {
    type Item = CommitType;

    fn next(&mut self) -> Option<CommitType> {
        match self.current {
            Some(commit_type) => mem::replace(&mut self.current, commit_type.next_variant()),
            None => None,
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match self.current {
            Some(CommitType::Breaking) => (5, Some(5)),
            Some(CommitType::Feature) => (4, Some(4)),
            Some(CommitType::Bugfix) => (3, Some(3)),
            Some(CommitType::Other) => (2, Some(2)),
            Some(CommitType::Meta) => (1, Some(1)),
            None => (0, Some(0)),
        }
    }
}

impl ExactSizeIterator for CommitTypeIterator {
    fn len(&self) -> usize {
        5
    }
}

#[cfg(test)]
mod tests {
    use super::{CommitType, BumpLevel};

    #[test]
    fn it_gives_the_first_type() {
        assert_eq!(CommitType::first_variant(), CommitType::Breaking);
    }

    #[test]
    fn it_gives_the_last_type() {
        assert_eq!(CommitType::last_variant(), CommitType::Meta);
    }

    #[test]
    fn it_gives_the_next_type() {
        assert_eq!(CommitType::Breaking.next_variant(), Some(CommitType::Feature));
        assert_eq!(CommitType::Feature.next_variant(), Some(CommitType::Bugfix));
        assert_eq!(CommitType::Bugfix.next_variant(), Some(CommitType::Other));
        assert_eq!(CommitType::Other.next_variant(), Some(CommitType::Meta));
        assert_eq!(CommitType::Meta.next_variant(), None);
    }

    #[test]
    fn it_gives_the_previous_type() {
        assert_eq!(CommitType::Breaking.prev_variant(), None);
        assert_eq!(CommitType::Feature.prev_variant(), Some(CommitType::Breaking));
        assert_eq!(CommitType::Bugfix.prev_variant(), Some(CommitType::Feature));
        assert_eq!(CommitType::Other.prev_variant(), Some(CommitType::Bugfix));
        assert_eq!(CommitType::Meta.prev_variant(), Some(CommitType::Other));
    }

    #[test]
    fn it_gives_an_variant_iterator() {
        let mut iter = CommitType::iter_variants();

        assert_eq!(iter.next(), Some(CommitType::Breaking));
        assert_eq!(iter.next(), Some(CommitType::Feature));
        assert_eq!(iter.next(), Some(CommitType::Bugfix));
        assert_eq!(iter.next(), Some(CommitType::Other));
        assert_eq!(iter.next(), Some(CommitType::Meta));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn it_gives_an_emoji() {
        assert_eq!(CommitType::Breaking.emoji(), "💥");
        assert_eq!(CommitType::Feature.emoji(), "🎉");
        assert_eq!(CommitType::Bugfix.emoji(), "🐛");
        assert_eq!(CommitType::Other.emoji(), "🔥");
        assert_eq!(CommitType::Meta.emoji(), "🌹");
    }

    #[test]
    fn it_gives_a_bump_level() {
        assert_eq!(CommitType::Breaking.bump_level(), BumpLevel::Major);
        assert_eq!(CommitType::Feature.bump_level(), BumpLevel::Minor);
        assert_eq!(CommitType::Bugfix.bump_level(), BumpLevel::Patch);
        assert_eq!(CommitType::Other.bump_level(), BumpLevel::Patch);
        assert_eq!(CommitType::Meta.bump_level(), BumpLevel::None);
    }

    #[test]
    fn it_gives_a_bump_level_name() {
        assert_eq!(CommitType::Breaking.bump_level().name(), "Major");
        assert_eq!(CommitType::Feature.bump_level().name(), "Minor");
        assert_eq!(CommitType::Bugfix.bump_level().name(), "Patch");
        assert_eq!(CommitType::Meta.bump_level().name(), "None");
    }

    #[test]
    fn it_gives_a_description() {
        assert_eq!(CommitType::Breaking.description(), "Breaking change");
        assert_eq!(CommitType::Feature.description(), "New functionality");
        assert_eq!(CommitType::Bugfix.description(), "Bugfix");
        assert_eq!(CommitType::Other.description(), "Cleanup / Performance");
        assert_eq!(CommitType::Meta.description(), "Meta");
    }
}