kpdb/types/
group_uuid.rs

1// Copyright (c) 2016-2017 Martijn Rijkeboer <mrr@sru-systems.com>
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 std::fmt::Display;
10use uuid::Uuid;
11
12/// The identifier for a group.
13#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
14pub struct GroupUuid(pub Uuid);
15
16impl GroupUuid {
17    /// Create a new random group identifier.
18    pub fn new_random() -> GroupUuid {
19        GroupUuid(Uuid::new_v4())
20    }
21
22    /// Create a nil/zero group identifier.
23    pub fn nil() -> GroupUuid {
24        GroupUuid(Uuid::nil())
25    }
26}
27
28impl Default for GroupUuid {
29    fn default() -> GroupUuid {
30        GroupUuid::nil()
31    }
32}
33
34impl Display for GroupUuid {
35    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
36        f.write_str(self.0.to_string().as_str())
37    }
38}
39
40#[cfg(test)]
41mod tests {
42
43    use super::*;
44    use uuid::Uuid;
45
46    #[test]
47    fn test_new_random_returns_random_group_uuids() {
48        let a = GroupUuid::new_random();
49        let b = GroupUuid::new_random();
50        assert!(a != b);
51    }
52
53    #[test]
54    fn test_nil_returns_nil_uuid() {
55        let expected = Uuid::nil();
56        let actual = GroupUuid::nil().0;
57        assert_eq!(actual, expected);
58    }
59
60    #[test]
61    fn test_default_returns_nil_group_uuid() {
62        let expected = GroupUuid::nil();
63        let actual = GroupUuid::default();
64        assert_eq!(actual, expected);
65    }
66}