use alloc::{
boxed::Box,
format,
string::{String, ToString},
vec::Vec,
};
use core::{
convert::{From, TryFrom, TryInto},
fmt,
hash::Hash,
str::FromStr,
};
pub use uhlc::{Timestamp, NTP64};
use zenoh_keyexpr::OwnedKeyExpr;
use zenoh_result::{bail, zerror};
pub type TimestampId = uhlc::ID;
pub mod whatami;
pub use whatami::*;
pub use zenoh_keyexpr::key_expr;
pub mod wire_expr;
pub use wire_expr::*;
mod cowstr;
pub use cowstr::CowStr;
mod encoding;
pub use encoding::{Encoding, KnownEncoding};
pub mod locator;
pub use locator::*;
pub mod endpoint;
pub use endpoint::*;
pub mod resolution;
pub use resolution::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Property {
pub key: u64,
pub value: Vec<u8>,
}
#[repr(u8)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub enum SampleKind {
#[default]
Put = 0,
Delete = 1,
}
impl fmt::Display for SampleKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SampleKind::Put => write!(f, "PUT"),
SampleKind::Delete => write!(f, "DELETE"),
}
}
}
impl TryFrom<u64> for SampleKind {
type Error = u64;
fn try_from(kind: u64) -> Result<Self, u64> {
match kind {
0 => Ok(SampleKind::Put),
1 => Ok(SampleKind::Delete),
_ => Err(kind),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ZenohId(uhlc::ID);
impl ZenohId {
pub const MAX_SIZE: usize = 16;
#[inline]
pub fn size(&self) -> usize {
self.0.size()
}
#[inline]
pub fn to_le_bytes(&self) -> [u8; uhlc::ID::MAX_SIZE] {
self.0.to_le_bytes()
}
pub fn rand() -> ZenohId {
ZenohId(uhlc::ID::rand())
}
pub fn into_keyexpr(self) -> OwnedKeyExpr {
self.into()
}
}
impl Default for ZenohId {
fn default() -> Self {
Self::rand()
}
}
#[derive(Debug, Clone, Copy)]
pub struct SizeError(usize);
#[cfg(feature = "std")]
impl std::error::Error for SizeError {}
#[cfg(not(feature = "std"))]
impl zenoh_result::IError for SizeError {}
impl From<uhlc::SizeError> for SizeError {
fn from(val: uhlc::SizeError) -> Self {
Self(val.0)
}
}
impl fmt::Display for SizeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Maximum ID size ({} bytes) exceeded: {}",
uhlc::ID::MAX_SIZE,
self.0
)
}
}
macro_rules! derive_tryfrom {
($T: ty) => {
impl TryFrom<$T> for ZenohId {
type Error = zenoh_result::Error;
fn try_from(val: $T) -> Result<Self, Self::Error> {
match val.try_into() {
Ok(ok) => Ok(Self(ok)),
Err(err) => Err(Box::<SizeError>::new(err.into())),
}
}
}
};
}
derive_tryfrom!([u8; 1]);
derive_tryfrom!(&[u8; 1]);
derive_tryfrom!([u8; 2]);
derive_tryfrom!(&[u8; 2]);
derive_tryfrom!([u8; 3]);
derive_tryfrom!(&[u8; 3]);
derive_tryfrom!([u8; 4]);
derive_tryfrom!(&[u8; 4]);
derive_tryfrom!([u8; 5]);
derive_tryfrom!(&[u8; 5]);
derive_tryfrom!([u8; 6]);
derive_tryfrom!(&[u8; 6]);
derive_tryfrom!([u8; 7]);
derive_tryfrom!(&[u8; 7]);
derive_tryfrom!([u8; 8]);
derive_tryfrom!(&[u8; 8]);
derive_tryfrom!([u8; 9]);
derive_tryfrom!(&[u8; 9]);
derive_tryfrom!([u8; 10]);
derive_tryfrom!(&[u8; 10]);
derive_tryfrom!([u8; 11]);
derive_tryfrom!(&[u8; 11]);
derive_tryfrom!([u8; 12]);
derive_tryfrom!(&[u8; 12]);
derive_tryfrom!([u8; 13]);
derive_tryfrom!(&[u8; 13]);
derive_tryfrom!([u8; 14]);
derive_tryfrom!(&[u8; 14]);
derive_tryfrom!([u8; 15]);
derive_tryfrom!(&[u8; 15]);
derive_tryfrom!([u8; 16]);
derive_tryfrom!(&[u8; 16]);
derive_tryfrom!(&[u8]);
impl FromStr for ZenohId {
type Err = zenoh_result::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.contains(|c: char| c.is_ascii_uppercase()) {
bail!(
"Invalid id: {} - uppercase hexadecimal is not accepted, use lowercase",
s
);
}
let u: uhlc::ID = s
.parse()
.map_err(|e: uhlc::ParseIDError| zerror!("Invalid id: {} - {}", s, e.cause))?;
Ok(ZenohId(u))
}
}
impl fmt::Debug for ZenohId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl fmt::Display for ZenohId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl From<&ZenohId> for uhlc::ID {
fn from(zid: &ZenohId) -> Self {
zid.0
}
}
impl From<ZenohId> for OwnedKeyExpr {
fn from(zid: ZenohId) -> Self {
unsafe { OwnedKeyExpr::from_string_unchecked(zid.to_string()) }
}
}
impl From<&ZenohId> for OwnedKeyExpr {
fn from(zid: &ZenohId) -> Self {
(*zid).into()
}
}
impl serde::Serialize for ZenohId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.to_string().as_str())
}
}
impl<'de> serde::Deserialize<'de> for ZenohId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct ZenohIdVisitor;
impl<'de> serde::de::Visitor<'de> for ZenohIdVisitor {
type Value = ZenohId;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str(&format!("An hex string of 1-{} bytes", ZenohId::MAX_SIZE))
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
v.parse().map_err(serde::de::Error::custom)
}
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str(v)
}
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str(&v)
}
}
deserializer.deserialize_str(ZenohIdVisitor)
}
}
#[repr(u8)]
#[derive(Debug, Default, Copy, Clone, Eq, Hash, PartialEq)]
pub enum Priority {
Control = 0,
RealTime = 1,
InteractiveHigh = 2,
InteractiveLow = 3,
DataHigh = 4,
#[default]
Data = 5,
DataLow = 6,
Background = 7,
}
impl Priority {
pub const MIN: Self = Self::Background;
pub const MAX: Self = Self::Control;
pub const NUM: usize = 1 + Self::MIN as usize - Self::MAX as usize;
}
impl TryFrom<u8> for Priority {
type Error = zenoh_result::Error;
fn try_from(v: u8) -> Result<Self, Self::Error> {
match v {
0 => Ok(Priority::Control),
1 => Ok(Priority::RealTime),
2 => Ok(Priority::InteractiveHigh),
3 => Ok(Priority::InteractiveLow),
4 => Ok(Priority::DataHigh),
5 => Ok(Priority::Data),
6 => Ok(Priority::DataLow),
7 => Ok(Priority::Background),
unknown => bail!(
"{} is not a valid priority value. Admitted values are: [{}-{}].",
unknown,
Self::MAX as u8,
Self::MIN as u8
),
}
}
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum Reliability {
#[default]
BestEffort,
Reliable,
}
impl Reliability {
#[cfg(feature = "test")]
pub fn rand() -> Self {
use rand::Rng;
let mut rng = rand::thread_rng();
if rng.gen_bool(0.5) {
Reliability::Reliable
} else {
Reliability::BestEffort
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct Channel {
pub priority: Priority,
pub reliability: Reliability,
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum CongestionControl {
#[default]
Drop = 0,
Block = 1,
}
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum SubMode {
#[default]
Push = 0,
Pull = 1,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SubInfo {
pub reliability: Reliability,
pub mode: SubMode,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct QueryableInfo {
pub complete: u64, pub distance: u64, }
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum ConsolidationMode {
None,
Monotonic,
Latest,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum QueryTarget {
#[default]
BestMatching,
All,
AllComplete,
#[cfg(feature = "complete_n")]
Complete(u64),
}