use std::{
fmt::{Debug, Display},
hash::Hash,
};
use nautilus_core::correctness::{
CorrectnessResult, CorrectnessResultExt, FAILED, check_valid_string_utf8,
};
use ustr::Ustr;
#[repr(C)]
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
)]
pub struct PositionId(Ustr);
impl PositionId {
pub fn new_checked<T: AsRef<str>>(value: T) -> CorrectnessResult<Self> {
let value = value.as_ref();
check_valid_string_utf8(value, stringify!(value))?;
Ok(Self(Ustr::from(value)))
}
pub fn new<T: AsRef<str>>(value: T) -> Self {
Self::new_checked(value).expect_display(FAILED)
}
#[cfg_attr(not(feature = "python"), allow(dead_code))]
pub(crate) fn set_inner(&mut self, value: &str) {
self.0 = Ustr::from(value);
}
#[must_use]
pub fn inner(&self) -> Ustr {
self.0
}
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
#[must_use]
pub fn is_virtual(&self) -> bool {
self.0.starts_with("P-")
}
}
impl Debug for PositionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\"{}\"", self.0)
}
}
impl Display for PositionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::PositionId;
use crate::identifiers::stubs::*;
#[rstest]
fn test_string_reprs(position_id_test: PositionId) {
assert_eq!(position_id_test.as_str(), "P-123456789");
assert_eq!(format!("{position_id_test}"), "P-123456789");
}
#[rstest]
#[should_panic(expected = "Condition failed: invalid string for 'value', was empty")]
fn test_new_with_empty_string_panics_with_display_format() {
let _ = PositionId::new("");
}
#[rstest]
fn test_deserialize_json_with_unicode_escapes() {
let id: PositionId = serde_json::from_str(r#""P-\u9f99\u867e-1""#).unwrap();
assert_eq!(id.as_str(), "P-\u{9f99}\u{867e}-1");
}
#[rstest]
fn test_serialization_roundtrip_non_ascii() {
let id = PositionId::new("P-\u{9f99}\u{867e}-1");
let json = serde_json::to_string(&id).unwrap();
assert_eq!(json, "\"P-\u{9f99}\u{867e}-1\"");
let deserialized: PositionId = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, id);
}
#[rstest]
fn test_deserialize_rejects_empty_string() {
let result: Result<PositionId, _> = serde_json::from_str(r#""""#);
assert!(result.is_err());
}
}