Skip to content

Commit

Permalink
Refine UUID4
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed May 4, 2024
1 parent e7b41a2 commit a8a78ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions nautilus_core/core/src/python/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use pyo3::{
};

use super::to_pyvalue_err;
use crate::uuid::UUID4;
use crate::uuid::{UUID4, UUID4_LEN};

#[pymethods]
impl UUID4 {
Expand All @@ -42,7 +42,7 @@ impl UUID4 {
let bytes: &PyBytes = state.extract(py)?;
let slice = bytes.as_bytes();

if slice.len() != 37 {
if slice.len() != UUID4_LEN {
return Err(to_pyvalue_err(
"Invalid state for deserialzing, incorrect bytes length",
));
Expand Down Expand Up @@ -86,7 +86,7 @@ impl UUID4 {
}

fn __repr__(&self) -> String {
format!("{}('{}')", stringify!(UUID4), self)
format!("{:?}", self)
}

#[getter]
Expand Down
20 changes: 16 additions & 4 deletions nautilus_core/core/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use uuid::Uuid;

/// The maximum length of ASCII characters for a `UUID4` string value (includes null terminator).
const UUID4_LEN: usize = 37;
pub(crate) const UUID4_LEN: usize = 37;

/// Represents a pseudo-random UUID (universally unique identifier)
/// version 4 based on a 128-bit label as specified in RFC 4122.
#[repr(C)]
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.core")
Expand Down Expand Up @@ -87,6 +87,12 @@ impl Default for UUID4 {
}
}

impl Debug for UUID4 {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}('{}')", stringify!(UUID4), self)
}
}

impl Display for UUID4 {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_cstr().to_string_lossy())
Expand Down Expand Up @@ -158,11 +164,17 @@ mod tests {
assert_ne!(uuid1, uuid2);
}

#[rstest]
fn test_uuid4_debug() {
let uuid_string = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
let uuid = UUID4::from(uuid_string);
assert_eq!(format!("{:?}", uuid), format!("UUID4('{uuid_string}')"));
}

#[rstest]
fn test_uuid4_display() {
let uuid_string = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
let uuid = UUID4::from(uuid_string);
let result_string = format!("{uuid}");
assert_eq!(result_string, uuid_string);
assert_eq!(format!("{uuid}"), uuid_string);
}
}

0 comments on commit a8a78ac

Please sign in to comment.