use std::{
fmt::{Debug, Display},
hash::Hash,
};
use nautilus_core::correctness::{FAILED, check_valid_string_ascii};
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 ActorId(Ustr);
impl ActorId {
pub fn new_checked<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
let value = value.as_ref();
check_valid_string_ascii(value, stringify!(value))?;
Ok(Self(Ustr::from(value)))
}
pub fn new<T: AsRef<str>>(value: T) -> Self {
Self::new_checked(value).expect(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()
}
}
impl Debug for ActorId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\"{}\"", self.0)
}
}
impl Display for ActorId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::ActorId;
#[rstest]
fn test_string_reprs() {
let actor_id = ActorId::from("MyActor");
assert_eq!(actor_id.as_str(), "MyActor");
assert_eq!(format!("{actor_id}"), "MyActor");
}
}