kpdb/types/
group_uuid.rs

1// Copyright (c) 2016-2017 Martijn Rijkeboer <[email protected]>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use uuid::Uuid;
10
11/// The identifier for a group.
12#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
13pub struct GroupUuid(pub Uuid);
14
15impl GroupUuid {
16    /// Create a new random group identifier.
17    pub fn new_random() -> GroupUuid {
18        GroupUuid(Uuid::new_v4())
19    }
20
21    /// Create a nil/zero group identifier.
22    pub fn nil() -> GroupUuid {
23        GroupUuid(Uuid::nil())
24    }
25}
26
27impl Default for GroupUuid {
28    fn default() -> GroupUuid {
29        GroupUuid::nil()
30    }
31}
32
33#[cfg(test)]
34mod tests {
35
36    use super::*;
37    use uuid::Uuid;
38
39    #[test]
40    fn test_new_random_returns_random_group_uuids() {
41        let a = GroupUuid::new_random();
42        let b = GroupUuid::new_random();
43        assert!(a != b);
44    }
45
46    #[test]
47    fn test_nil_returns_nil_uuid() {
48        let expected = Uuid::nil();
49        let actual = GroupUuid::nil().0;
50        assert_eq!(actual, expected);
51    }
52
53    #[test]
54    fn test_default_returns_nil_group_uuid() {
55        let expected = GroupUuid::nil();
56        let actual = GroupUuid::default();
57        assert_eq!(actual, expected);
58    }
59}