use crate::fail;
use crate::layer::OpenedSpan;
use tracing::Subscriber;
use tracing_subscriber::{registry::LookupSpan, Registry};
use uuid::Uuid;
#[must_use]
pub fn id() -> Uuid {
tracing::dispatcher::get_default(|dispatch| {
let subscriber = dispatch
.downcast_ref::<Registry>()
.unwrap_or_else(fail::subscriber_not_found);
let current = subscriber.current_span();
let id = current.id().expect(fail::NO_CURRENT_SPAN);
subscriber
.span(id)
.expect(fail::SPAN_NOT_IN_CONTEXT)
.extensions()
.get::<OpenedSpan>()
.expect(fail::NO_FOREST_LAYER)
.uuid()
})
}
pub(crate) const fn try_parse(input: &[u8]) -> Option<Uuid> {
match (input.len(), input) {
(32, s) => parse_simple(s),
(36, s)
| (38, [b'{', s @ .., b'}'])
| (45, [b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..]) => {
parse_hyphenated(s)
}
_ => None,
}
}
#[inline]
const fn parse_simple(s: &[u8]) -> Option<Uuid> {
if s.len() != 32 {
return None;
}
let mut buf: [u8; 16] = [0; 16];
let mut i = 0;
while i < 16 {
let h1 = HEX_TABLE[s[i * 2] as usize];
let h2 = HEX_TABLE[s[i * 2 + 1] as usize];
if h1 | h2 == 0xff {
return None;
}
buf[i] = SHL4_TABLE[h1 as usize] | h2;
i += 1;
}
Some(Uuid::from_bytes(buf))
}
#[inline]
const fn parse_hyphenated(s: &[u8]) -> Option<Uuid> {
if s.len() != 36 {
return None;
}
match [s[8], s[13], s[18], s[23]] {
[b'-', b'-', b'-', b'-'] => {}
_ => return None,
}
let positions: [u8; 8] = [0, 4, 9, 14, 19, 24, 28, 32];
let mut buf: [u8; 16] = [0; 16];
let mut j = 0;
while j < 8 {
let i = positions[j];
let h1 = HEX_TABLE[s[i as usize] as usize];
let h2 = HEX_TABLE[s[(i + 1) as usize] as usize];
let h3 = HEX_TABLE[s[(i + 2) as usize] as usize];
let h4 = HEX_TABLE[s[(i + 3) as usize] as usize];
if h1 | h2 | h3 | h4 == 0xff {
return None;
}
buf[j * 2] = SHL4_TABLE[h1 as usize] | h2;
buf[j * 2 + 1] = SHL4_TABLE[h3 as usize] | h4;
j += 1;
}
Some(Uuid::from_bytes(buf))
}
const HEX_TABLE: &[u8; 256] = &{
let mut buf = [0; 256];
let mut i: u8 = 0;
loop {
buf[i as usize] = match i {
b'0'..=b'9' => i - b'0',
b'a'..=b'f' => i - b'a' + 10,
b'A'..=b'F' => i - b'A' + 10,
_ => 0xff,
};
if i == 255 {
break buf;
}
i += 1;
}
};
const SHL4_TABLE: &[u8; 256] = &{
let mut buf = [0; 256];
let mut i: u8 = 0;
loop {
buf[i as usize] = i.wrapping_shl(4);
if i == 255 {
break buf;
}
i += 1;
}
};