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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright © 2020 Alexandra Frydl
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use crate::prelude::*;
use ::uuid::{Builder, Uuid as Inner, Variant, Version};

/// A universally-unique identifier.
#[derive(
  Clone, Copy, Default, Deserialize, Eq, From, FromStr, Hash, Ord, PartialEq, PartialOrd, Serialize,
)]
#[serde(transparent)]
pub struct Uuid(Inner);

impl Uuid {
  /// Returns a "nil" UUID.
  pub const fn nil() -> Self {
    Self(Inner::nil())
  }

  /// Returns `true` if the UUID is the "nil" value.
  pub fn is_nil(&self) -> bool {
    self.0.is_nil()
  }

  /// Returns a new, random UUID.
  pub fn new() -> Self {
    random()
  }

  /// Returns a slice containing the bytes of the UUID.
  pub fn as_bytes(&self) -> &[u8] {
    self.0.as_bytes()
  }

  /// Converts the UUID to a `u128`.
  pub fn to_u128(&self) -> u128 {
    self.0.as_u128()
  }
}

// Implement random generation.

impl Random for Uuid {
  fn random_with(rng: &mut random::Rng) -> Self {
    let mut bytes = [0u8; 16];

    rng.fill_bytes(&mut bytes);

    let uuid =
      Builder::from_bytes(bytes).set_variant(Variant::RFC4122).set_version(Version::Random).build();

    Self(uuid)
  }
}

// Implement formatting.

macro_rules! impl_fmt {
  ($ty:ident) => {
    impl fmt::$ty for Uuid {
      fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::$ty::fmt(&self.0, f)
      }
    }
  };
}

impl_fmt!(Debug);
impl_fmt!(Display);
impl_fmt!(LowerHex);
impl_fmt!(UpperHex);

// Implement conversion to and from postgres.

cfg_if! {
  if #[cfg(feature = "postgres")] {
    use postgres_types as pg;

    impl<'a> pg::FromSql<'a> for Uuid {
      fn from_sql(ty: &pg::Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>>{
        Ok(Self(pg::FromSql::from_sql(ty, raw)?))
      }

      fn accepts(ty: &pg::Type) -> bool {
        <Inner as pg::FromSql>::accepts(ty)
      }
    }

    impl pg::ToSql for Uuid {
      fn to_sql(&self, ty: &pg::Type, out: &mut bytes::BytesMut) -> Result<pg::IsNull, Box<dyn Error + Sync + Send>>
      where
        Self: Sized,
      {
        self.0.to_sql(ty, out)
      }

      fn accepts(ty: &pg::Type) -> bool
      where
        Self: Sized,
      {
        <Inner as pg::ToSql>::accepts(ty)
      }

      fn to_sql_checked(&self, ty: &pg::Type, out: &mut bytes::BytesMut) -> Result<pg::IsNull, Box<dyn Error + Sync + Send>> {
        self.0.to_sql_checked(ty, out)
      }
    }
  }
}