Skip to main content

vortex_array/extension/uuid/
metadata.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt;
5use std::hash::Hash;
6use std::hash::Hasher;
7
8use uuid::Version;
9use vortex_error::VortexResult;
10use vortex_error::vortex_bail;
11
12/// Converts a `u8` discriminant back to a [`uuid::Version`].
13pub(crate) fn u8_to_version(b: u8) -> VortexResult<Version> {
14    match b {
15        0 => Ok(Version::Nil),
16        1 => Ok(Version::Mac),
17        2 => Ok(Version::Dce),
18        3 => Ok(Version::Md5),
19        4 => Ok(Version::Random),
20        5 => Ok(Version::Sha1),
21        6 => Ok(Version::SortMac),
22        7 => Ok(Version::SortRand),
23        8 => Ok(Version::Custom),
24        0xff => Ok(Version::Max),
25        _ => vortex_bail!("unknown UUID version discriminant: {b}"),
26    }
27}
28
29/// Metadata for the UUID extension type.
30///
31/// Optionally records which UUID version the column contains (e.g. v4 random, v7
32/// sort-random). When `None`, the column may contain any mix of versions.
33#[derive(Clone, Debug, Default)]
34pub struct UuidMetadata {
35    /// The UUID version, if known.
36    pub version: Option<Version>,
37}
38
39impl fmt::Display for UuidMetadata {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self.version {
42            None => write!(f, ""),
43            Some(v) => write!(f, "v{}", v as u8),
44        }
45    }
46}
47
48// `uuid::Version` derives `PartialEq` but not `Eq` or `Hash`, so we implement these
49// manually using the `#[repr(u8)]` discriminant.
50
51impl PartialEq for UuidMetadata {
52    fn eq(&self, other: &Self) -> bool {
53        self.version.map(|v| v as u8) == other.version.map(|v| v as u8)
54    }
55}
56
57impl Eq for UuidMetadata {}
58
59impl Hash for UuidMetadata {
60    fn hash<H: Hasher>(&self, state: &mut H) {
61        self.version.map(|v| v as u8).hash(state);
62    }
63}