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
use std::collections::HashMap;
use rand::{self, RngCore};
use serde_json::Value;
use uuid::Uuid;
use crate::context::Context;
use crate::error::Result;
pub(crate) fn uuidv4(_args: &HashMap<String, Value>, _context: &mut Context) -> Result<Value> {
let mut rng = rand::thread_rng();
let mut bytes = [0; 16];
rng.fill_bytes(&mut bytes);
let uuid = Uuid::from_random_bytes(bytes);
Ok(Value::String(uuid.to_hyphenated().to_string()))
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use uuid::{Uuid, Version};
use crate::context::Context;
use super::uuidv4;
#[test]
fn result_is_valid_uuid_v4() {
let mut ctx = Context::default();
let args = HashMap::new();
let uuid_value = uuidv4(&args, &mut ctx).unwrap();
let uuid_str = uuid_value.as_str().unwrap();
let uuid = Uuid::parse_str(&uuid_str).unwrap();
assert_eq!(uuid.get_version(), Some(Version::Random));
}
}