nautilus_model/identifiers/
actor_id.rs1use std::{
19 fmt::{Debug, Display},
20 hash::Hash,
21};
22
23use nautilus_core::correctness::{
24 CorrectnessResult, CorrectnessResultExt, FAILED, check_valid_string_ascii,
25};
26use ustr::Ustr;
27
28#[repr(C)]
30#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
31#[cfg_attr(
32 feature = "python",
33 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
34)]
35#[cfg_attr(
36 feature = "python",
37 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
38)]
39pub struct ActorId(Ustr);
40
41impl ActorId {
42 pub fn new_checked<T: AsRef<str>>(value: T) -> CorrectnessResult<Self> {
52 let value = value.as_ref();
53 check_valid_string_ascii(value, stringify!(value))?;
54 Ok(Self(Ustr::from(value)))
55 }
56
57 pub fn new<T: AsRef<str>>(value: T) -> Self {
63 Self::new_checked(value).expect_display(FAILED)
64 }
65
66 #[cfg_attr(not(feature = "python"), allow(dead_code))]
68 pub(crate) fn set_inner(&mut self, value: &str) {
69 self.0 = Ustr::from(value);
70 }
71
72 #[must_use]
74 pub fn inner(&self) -> Ustr {
75 self.0
76 }
77
78 #[must_use]
80 pub fn as_str(&self) -> &str {
81 self.0.as_str()
82 }
83}
84
85impl Debug for ActorId {
86 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87 write!(f, "\"{}\"", self.0)
88 }
89}
90
91impl Display for ActorId {
92 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93 write!(f, "{}", self.0)
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 use rstest::rstest;
100
101 use super::ActorId;
102
103 #[rstest]
104 fn test_string_reprs() {
105 let actor_id = ActorId::from("MyActor");
106 assert_eq!(actor_id.as_str(), "MyActor");
107 assert_eq!(format!("{actor_id}"), "MyActor");
108 }
109
110 #[rstest]
111 #[should_panic(expected = "Condition failed: invalid string for 'value', was empty")]
112 fn test_new_with_empty_string_panics_with_display_format() {
113 let _ = ActorId::new("");
114 }
115}