use crate::script::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ScriptUuid(pub(crate) u128);
impl ScriptUuid {
pub(crate) fn canonical(&self) -> String {
crate::value::uuid::format_canonical(self.0).to_string()
}
pub fn as_u128(&self) -> u128 {
self.0
}
}
pub(crate) fn script_uuid_random() -> ScriptUuid {
script_uuid_v4(rand::random())
}
pub(crate) fn script_uuid_v4(bits: u128) -> ScriptUuid {
let version_4 = (bits & !(0xF << 76)) | (0x4 << 76);
ScriptUuid((version_4 & !(0x3 << 62)) | (0x2 << 62))
}
pub(crate) fn script_uuid_from_string(value: &str) -> Result<ScriptUuid, Box<rhai::EvalAltResult>> {
match ElementProperty::uuid_from_string(value) {
Ok(ElementProperty::Uuid(bits)) => Ok(ScriptUuid(bits)),
Ok(other) => Err(script_error(Box::new(
crate::error::TraverserError::cast_exception_target(
"uuid",
crate::graph::schema::value_type_name(&other),
Some(crate::value::literal::rhai_literal(&other)),
None,
),
))),
Err(err) => Err(script_error(err)),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScriptUuidTokens;
#[export_module]
#[allow(non_snake_case, non_upper_case_globals)]
pub(crate) mod UuidModule {
use super::{ScriptUuid, script_uuid_from_string};
#[rhai_fn(name = "fromString", name = "from_string", return_raw)]
pub fn from_string(value: &str) -> Result<ScriptUuid, Box<rhai::EvalAltResult>> {
script_uuid_from_string(value)
}
}