typedb_driver/common/id.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20use std::fmt;
21
22use uuid::Uuid;
23
24#[derive(Clone, Eq, Hash, PartialEq)]
25pub struct ID(Vec<u8>);
26
27impl ID {
28 const PREFIX: &'static str = "0x";
29
30 pub(crate) fn generate() -> Self {
31 Uuid::new_v4().as_bytes().to_vec().into()
32 }
33}
34
35impl From<ID> for Vec<u8> {
36 fn from(id: ID) -> Self {
37 id.0
38 }
39}
40
41impl From<Vec<u8>> for ID {
42 fn from(vec: Vec<u8>) -> Self {
43 Self(vec)
44 }
45}
46
47impl fmt::Debug for ID {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 write!(f, "ID[{self}]")
50 }
51}
52
53impl fmt::Display for ID {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 write!(f, "{}", Self::PREFIX)?;
56 self.0.iter().try_for_each(|byte| write!(f, "{byte:02x}"))
57 }
58}