use alloc::borrow::ToOwned;
use alloc::format;
use iceoryx2_bb_container::string::*;
use iceoryx2_bb_derive_macros::ZeroCopySend;
use iceoryx2_bb_elementary_traits::zero_copy_send::ZeroCopySend;
use serde::{Deserialize, Serialize, de::Visitor};
use crate::constants::MAX_SERVICE_NAME_LENGTH;
pub const INTERNAL_SERVICE_PREFIX: &str = "iox2://";
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ServiceNameError {
InvalidContent,
ExceedsMaximumLength,
}
impl core::fmt::Display for ServiceNameError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "ServiceNameError::{self:?}")
}
}
impl core::error::Error for ServiceNameError {}
impl From<StringModificationError> for ServiceNameError {
fn from(error: StringModificationError) -> Self {
match error {
StringModificationError::InsertWouldExceedCapacity => {
ServiceNameError::ExceedsMaximumLength
}
StringModificationError::InvalidCharacter => ServiceNameError::InvalidContent,
}
}
}
type ServiceNameString = StaticString<MAX_SERVICE_NAME_LENGTH>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, ZeroCopySend)]
#[repr(C)]
pub struct ServiceName {
value: ServiceNameString,
}
impl ServiceName {
pub fn new(name: &str) -> Result<Self, ServiceNameError> {
if Self::has_iox2_prefix(name) {
return Err(ServiceNameError::InvalidContent);
}
Self::__internal_new(name)
}
#[doc(hidden)]
pub fn __internal_new_prefixed(name: &str) -> Result<Self, ServiceNameError> {
Self::__internal_new(&(INTERNAL_SERVICE_PREFIX.to_owned() + name))
}
#[doc(hidden)]
pub fn __internal_new(name: &str) -> Result<Self, ServiceNameError> {
if name.is_empty() {
return Err(ServiceNameError::InvalidContent);
}
let value = ServiceNameString::try_from(name).map_err(ServiceNameError::from)?;
Ok(Self { value })
}
pub fn as_str(&self) -> &str {
self.value.as_str()
}
pub fn has_iox2_prefix(name: &str) -> bool {
name.starts_with(INTERNAL_SERVICE_PREFIX)
}
pub fn max_len() -> usize {
ServiceNameString::capacity()
}
}
impl core::fmt::Display for ServiceName {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.value)
}
}
impl TryInto<ServiceName> for &str {
type Error = ServiceNameError;
fn try_into(self) -> Result<ServiceName, Self::Error> {
ServiceName::__internal_new(self)
}
}
impl PartialEq<&str> for ServiceName {
fn eq(&self, other: &&str) -> bool {
*self.as_str() == **other
}
}
impl PartialEq<&str> for &ServiceName {
fn eq(&self, other: &&str) -> bool {
*self.as_str() == **other
}
}
impl core::ops::Deref for ServiceName {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
struct ServiceNameVisitor;
impl Visitor<'_> for ServiceNameVisitor {
type Value = ServiceName;
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("a string containing the service name")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match ServiceName::__internal_new(v) {
Ok(v) => Ok(v),
Err(v) => Err(E::custom(format!("invalid service name provided {v:?}."))),
}
}
}
impl<'de> Deserialize<'de> for ServiceName {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_str(ServiceNameVisitor)
}
}
impl Serialize for ServiceName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}