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
// Copyright © 2020-present, Michael Cummings
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// MIT License
//
// Copyright © 2020-present, Michael Cummings <mgcummings@yahoo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#[cfg(feature = "wasm-bindgen")]
pub use crate::uuid4gen::*;
pub use crate::{error::*, uuid4::*};
use std::collections::HashMap;

mod error;
#[cfg(test)]
mod tests;
mod uuid4;
#[cfg(feature = "wasm-bindgen")]
mod uuid4gen;

/// Core trait for the library.
pub trait Uuid {
    /// Provides direct access to the internal value of the uuid.
    ///
    /// This allows the other trait methods to access the value without knowing
    /// how or where it is actual kept.
    fn uuid0(&self) -> u128;
    /// Provides direct access to set the internal value of the uuid.
    ///
    /// This allows the other trait methods to write the value without knowing
    /// how or where it is actual kept.
    fn set_uuid0(&mut self, v: u128);
    /// Generate a custom base 64 encoded UUID v4 (random).
    fn as_base64(&self) -> String {
        let mut map = HashMap::with_capacity(64);
        for (k, v) in Self::BASE64.iter() {
            map.insert(*k, *v);
        }
        let mut binary = String::from("0000");
        binary += &*format!("{:0>128b}", self.uuid0());
        let mut result = String::new();
        for _ in 0..22 {
            let (bits, remaining) = binary.split_at(6);
            result.push(
                *map.get(bits)
                    .expect("Received unknown bit pattern. Check BASE64"),
            );
            binary = remaining.to_string();
        }
        result
    }
    /// Generate a hexadecimal encoded UUID v4 (random).
    fn as_hex_string(&self) -> String {
        format!("{:0>32x}", self.uuid0())
    }
    /// Generate a standard UUID v4 (random).
    ///
    /// The original PHP code for the function was found in answer at
    /// https://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid
    /// by Arie which is based on a function found at
    /// http://php.net/manual/en/function.com-create-guid.php
    /// by pavel.volyntsev(at)gmail.
    ///
    /// There have been many other changes since the above code especially with
    /// translation to Rust.
    fn as_uuid(&self) -> String {
        let mut hex = format!("{:0>32x}", self.uuid0());
        let mut result = String::new();
        let chunks = [8usize, 4, 4, 4, 12];
        for idx in chunks.iter() {
            let (chunk, remaining) = hex.split_at(*idx);
            result.push_str(chunk);
            result.push('-');
            hex = remaining.to_string();
        }
        result.truncate(36);
        result
    }
    /// An array use when decoding/encoding base64.
    const BASE64: [(&'static str, char); 64] = [
        ("000000", 'A'),
        ("000001", 'B'),
        ("000010", 'C'),
        ("000011", 'D'),
        ("000100", 'E'),
        ("000101", 'F'),
        ("000110", 'G'),
        ("000111", 'H'),
        ("001000", 'I'),
        ("001001", 'J'),
        ("001010", 'K'),
        ("001011", 'L'),
        ("001100", 'M'),
        ("001101", 'N'),
        ("001110", 'O'),
        ("001111", 'P'),
        ("010000", 'Q'),
        ("010001", 'R'),
        ("010010", 'S'),
        ("010011", 'T'),
        ("010100", 'U'),
        ("010101", 'V'),
        ("010110", 'W'),
        ("010111", 'X'),
        ("011000", 'Y'),
        ("011001", 'Z'),
        ("011010", 'a'),
        ("011011", 'b'),
        ("011100", 'c'),
        ("011101", 'd'),
        ("011110", 'e'),
        ("011111", 'f'),
        ("100000", 'g'),
        ("100001", 'h'),
        ("100010", 'i'),
        ("100011", 'j'),
        ("100100", 'k'),
        ("100101", 'l'),
        ("100110", 'm'),
        ("100111", 'n'),
        ("101000", 'o'),
        ("101001", 'p'),
        ("101010", 'q'),
        ("101011", 'r'),
        ("101100", 's'),
        ("101101", 't'),
        ("101110", 'u'),
        ("101111", 'v'),
        ("110000", 'w'),
        ("110001", 'x'),
        ("110010", 'y'),
        ("110011", 'z'),
        ("110100", '0'),
        ("110101", '1'),
        ("110110", '2'),
        ("110111", '3'),
        ("111000", '4'),
        ("111001", '5'),
        ("111010", '6'),
        ("111011", '7'),
        ("111100", '8'),
        ("111101", '9'),
        ("111110", '-'),
        ("111111", '_'),
    ];
}