Skip to main content

hermit/
id.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9use core::fmt;
10use core::str::FromStr;
11
12use serde::Deserialize;
13use serde::Serialize;
14use uuid::Uuid;
15
16/// A recording ID.
17#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
18pub struct Id(Uuid);
19
20impl Id {
21    /// Generates a new, random ID.
22    pub fn unique() -> Self {
23        Self(Uuid::new_v4())
24    }
25}
26
27impl FromStr for Id {
28    type Err = <Uuid as FromStr>::Err;
29
30    fn from_str(s: &str) -> Result<Self, Self::Err> {
31        Uuid::from_str(s).map(Self)
32    }
33}
34
35impl fmt::Display for Id {
36    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37        fmt::Display::fmt(&self.0.as_simple(), f)
38    }
39}
40
41impl fmt::Debug for Id {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        fmt::Display::fmt(&self.0.as_simple(), f)
44    }
45}