surrealdb_core/sql/
uuid.rsuse crate::sql::{escape::quote_str, strand::Strand};
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
use std::ops::Deref;
use std::str;
use std::str::FromStr;
use super::Datetime;
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Uuid";
#[revisioned(revision = 1)]
#[derive(
Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize, Hash,
)]
#[serde(rename = "$surrealdb::private::sql::Uuid")]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub struct Uuid(pub uuid::Uuid);
impl From<uuid::Uuid> for Uuid {
fn from(v: uuid::Uuid) -> Self {
Uuid(v)
}
}
impl From<Uuid> for uuid::Uuid {
fn from(s: Uuid) -> Self {
s.0
}
}
impl FromStr for Uuid {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from(s)
}
}
impl TryFrom<String> for Uuid {
type Error = ();
fn try_from(v: String) -> Result<Self, Self::Error> {
Self::try_from(v.as_str())
}
}
impl TryFrom<Strand> for Uuid {
type Error = ();
fn try_from(v: Strand) -> Result<Self, Self::Error> {
Self::try_from(v.as_str())
}
}
impl TryFrom<&str> for Uuid {
type Error = ();
fn try_from(v: &str) -> Result<Self, Self::Error> {
match uuid::Uuid::try_parse(v) {
Ok(v) => Ok(Self(v)),
Err(_) => Err(()),
}
}
}
impl Deref for Uuid {
type Target = uuid::Uuid;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Uuid {
pub fn new() -> Self {
Self(uuid::Uuid::now_v7())
}
pub fn new_v4() -> Self {
Self(uuid::Uuid::new_v4())
}
pub fn new_v7() -> Self {
Self(uuid::Uuid::now_v7())
}
pub fn new_v7_from_datetime(timestamp: Datetime) -> Self {
let ts = uuid::Timestamp::from_unix(
uuid::NoContext,
timestamp.0.timestamp() as u64,
timestamp.0.timestamp_subsec_nanos(),
);
Self(uuid::Uuid::new_v7(ts))
}
pub fn to_raw(&self) -> String {
self.0.to_string()
}
}
impl Display for Uuid {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "u")?;
Display::fmt("e_str(&self.0.to_string()), f)
}
}