Skip to main content

quant_codec_core/
ids.rs

1use crate::QuantCodecError;
2use core::fmt;
3
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7macro_rules! string_id {
8    ($name:ident) => {
9        #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10        #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11        pub struct $name(String);
12
13        impl $name {
14            pub fn new(value: impl Into<String>) -> Result<Self, QuantCodecError> {
15                let value = value.into();
16                if value.is_empty() {
17                    return Err(QuantCodecError::EmptyIdentifier {
18                        type_name: stringify!($name),
19                    });
20                }
21                if value.trim() != value {
22                    return Err(QuantCodecError::IdentifierWhitespace {
23                        type_name: stringify!($name),
24                    });
25                }
26                Ok(Self(value))
27            }
28
29            pub fn as_str(&self) -> &str {
30                &self.0
31            }
32        }
33
34        impl fmt::Debug for $name {
35            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36                f.debug_tuple(stringify!($name)).field(&self.0).finish()
37            }
38        }
39
40        impl fmt::Display for $name {
41            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42                f.write_str(&self.0)
43            }
44        }
45    };
46}
47
48string_id!(CodecId);
49string_id!(ModelFingerprint);
50string_id!(TokenizerFingerprint);