-
Notifications
You must be signed in to change notification settings - Fork 2
/
arbitrary.rs
49 lines (43 loc) · 1.6 KB
/
arbitrary.rs
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
use arbitrary::{Arbitrary, Result, Unstructured};
use arrayvec::ArrayString;
use crate::{DynamicType, StaticType, TypeSafeId};
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
impl<'a, T: StaticType> Arbitrary<'a> for TypeSafeId<T> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
let millis = u.arbitrary::<u64>()? & 0xFFFF_FFFF_FFFF; // 48 bits
let data: [u8; 10] = u.arbitrary()?;
Ok(Self::from_type_and_uuid(
T::default(),
uuid::Builder::from_unix_timestamp_millis(millis, &data).into_uuid(),
))
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
impl<'a> Arbitrary<'a> for DynamicType {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
let size = u.arbitrary_len::<u8>()?;
let mut str = ArrayString::<63>::new();
while !str.is_full() && str.len() < size {
match u.peek_bytes(1) {
Some([b @ b'a'..=b'z']) => {
str.push(*b as char);
u.bytes(1)?;
}
_ => break,
}
}
Ok(DynamicType(str))
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
impl<'a> Arbitrary<'a> for TypeSafeId<DynamicType> {
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
let tag: DynamicType = u.arbitrary()?;
let millis = u.arbitrary::<u64>()? & 0xFFFF_FFFF_FFFF; // 48 bits
let data: [u8; 10] = u.arbitrary()?;
Ok(Self::from_type_and_uuid(
tag,
uuid::Builder::from_unix_timestamp_millis(millis, &data).into_uuid(),
))
}
}