havok_types/
signature.rs

1//! Havok C++ Class signature hex number.
2//!
3//! # Example
4//! - `0x13a39ba7`
5use core::str::FromStr;
6use parse_display::Display;
7
8/// Havok C++ Class signature hex number.
9///
10/// - XML: hex string(At least 8 digits)
11/// - hkx binary data: Endianness bytes
12///
13/// # Examples
14/// ```
15/// use havok_types::signature::Signature;
16///
17/// assert_eq!(Signature::new(0x13a39ba7).to_string(), "0x13a39ba7");
18/// assert_eq!(Signature::new(0x00000001).to_string(), "0x1");
19/// assert_eq!(Signature::new(0x000000001).to_string(), "0x1");
20/// assert_eq!(Signature::new(0x076ad60a).to_string(), "0x76ad60a");
21///
22/// assert_eq!("0x13a39ba7".parse(), Ok(Signature::new(0x13a39ba7)));
23/// ```
24///
25/// # Note
26/// The [`Copy`] is derive for [`usize`] wrapper type.
27#[cfg_attr(feature = "json_schema", derive(schemars::JsonSchema))]
28#[cfg_attr(
29    feature = "serde",
30    derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
31)]
32#[repr(transparent)]
33#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Display)]
34#[display("{0:#x}")]
35pub struct Signature(u32);
36
37impl Signature {
38    /// Creates a new `Signature`
39    #[inline]
40    pub const fn new(sig: u32) -> Self {
41        Self(sig)
42    }
43
44    /// Get inner value.
45    #[inline]
46    pub const fn get(self) -> u32 {
47        self.0
48    }
49}
50
51impl FromStr for Signature {
52    type Err = &'static str;
53
54    #[inline]
55    fn from_str(s: &str) -> Result<Self, Self::Err> {
56        Ok(Self::new(
57            crate::parse_int::ParseNumber::parse(s)
58                .map_err(|_err| "Invalid integer for Signature")?,
59        ))
60    }
61}
62
63impl From<u32> for Signature {
64    #[inline]
65    fn from(value: u32) -> Self {
66        Self(value)
67    }
68}
69
70impl From<Signature> for u32 {
71    #[inline]
72    fn from(value: Signature) -> Self {
73        value.0
74    }
75}