use std::fmt;
use std::str::FromStr;
use thiserror::Error;
use zenoh_keyexpr::OwnedKeyExpr;
use crate::chunk::{is_ident, is_lower_hex, is_plain_chunk};
pub const GRAMMAR: &str = "zk2";
pub const CONTROL: &str = "@zk";
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum KeyError {
#[error("{what} {value:?} is not a plain chunk ([a-z0-9]([a-z0-9._-]*[a-z0-9])?)")]
NotPlain { what: &'static str, value: String },
#[error("interface {0:?} is not <name>.v<major> with name segments [a-z][a-z0-9_]*")]
Interface(String),
#[error("{what} {value:?} must be {len} lowercase hex digits")]
Hex {
what: &'static str,
value: String,
len: usize,
},
#[error("unknown kind token {0:?} (stream, @stream, state, @state, events, @op)")]
Kind(String),
#[error("{0:?} is not a zk2 key: {1}")]
Shape(String, &'static str),
#[error("{0:?} is not a valid Zenoh key expression: {1}")]
KeyExpr(String, String),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Name(String);
impl Name {
pub fn new(what: &'static str, s: &str) -> Result<Self, KeyError> {
if is_plain_chunk(s) {
Ok(Self(s.to_owned()))
} else {
Err(KeyError::NotPlain {
what,
value: s.to_owned(),
})
}
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Addr {
pub system: Name,
pub service: Name,
}
impl Addr {
pub fn new(system: &str, service: &str) -> Result<Self, KeyError> {
Ok(Self {
system: Name::new("system", system)?,
service: Name::new("service", service)?,
})
}
}
impl FromStr for Addr {
type Err = KeyError;
fn from_str(s: &str) -> Result<Self, KeyError> {
let (sys, svc) = s.split_once('/').ok_or(KeyError::Shape(
s.to_owned(),
"an address is <system>/<service>",
))?;
Self::new(sys, svc)
}
}
impl fmt::Display for Addr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.system, self.service)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IfaceId {
name: String,
major: u32,
}
impl IfaceId {
pub fn new(name: &str, major: u32) -> Result<Self, KeyError> {
let segments_ok = !name.is_empty() && name.split('.').all(is_ident);
let ends_like_major = name
.rsplit_once('.')
.is_some_and(|(_, last)| is_major_segment(last));
if segments_ok && !ends_like_major && !is_major_segment(name) {
Ok(Self {
name: name.to_owned(),
major,
})
} else {
Err(KeyError::Interface(name.to_owned()))
}
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn major(&self) -> u32 {
self.major
}
}
fn is_major_segment(s: &str) -> bool {
s.strip_prefix('v')
.is_some_and(|d| !d.is_empty() && d.bytes().all(|c| c.is_ascii_digit()))
}
impl FromStr for IfaceId {
type Err = KeyError;
fn from_str(s: &str) -> Result<Self, KeyError> {
let err = || KeyError::Interface(s.to_owned());
let (name, major) = s.rsplit_once(".v").ok_or_else(err)?;
if major.is_empty() || !major.bytes().all(|c| c.is_ascii_digit()) {
return Err(err());
}
if major.len() > 1 && major.starts_with('0') {
return Err(err());
}
let major: u32 = major.parse().map_err(|_| err())?;
Self::new(name, major).map_err(|_| err())
}
}
macro_rules! serde_via_str {
($($t:ty),*) => {$(
impl serde::Serialize for $t {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(self)
}
}
impl<'de> serde::Deserialize<'de> for $t {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let s = String::deserialize(d)?;
s.parse().map_err(serde::de::Error::custom)
}
}
)*};
}
serde_via_str!(Addr, IfaceId);
impl fmt::Display for IfaceId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.v{}", self.name, self.major)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum KindToken {
Stream,
ExplicitStream,
State,
ExplicitState,
Events,
Op,
}
impl KindToken {
pub const ALL: [KindToken; 6] = [
Self::Stream,
Self::ExplicitStream,
Self::State,
Self::ExplicitState,
Self::Events,
Self::Op,
];
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Stream => "stream",
Self::ExplicitStream => "@stream",
Self::State => "state",
Self::ExplicitState => "@state",
Self::Events => "events",
Self::Op => "@op",
}
}
#[must_use]
pub fn is_verbatim(self) -> bool {
self.as_str().starts_with('@')
}
}
impl FromStr for KindToken {
type Err = KeyError;
fn from_str(s: &str) -> Result<Self, KeyError> {
Self::ALL
.into_iter()
.find(|k| k.as_str() == s)
.ok_or_else(|| KeyError::Kind(s.to_owned()))
}
}
impl fmt::Display for KindToken {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
pub const AMBIENT: &str = "zk2/**";
pub const VERBATIM: [&str; 4] = ["@stream", "@state", "@op", CONTROL];
#[must_use]
pub fn kind_selector(
system: Option<&Name>,
service: Option<&Name>,
iface: Option<&IfaceId>,
kind: KindToken,
) -> String {
let iface = iface.map(ToString::to_string);
[
GRAMMAR,
system.map_or("*", Name::as_str),
service.map_or("*", Name::as_str),
iface.as_deref().unwrap_or("*"),
kind.as_str(),
"**",
]
.join("/")
}
#[must_use]
pub fn template_selector(
addr: &Addr,
iface: &IfaceId,
kind: KindToken,
template: &crate::template::Template,
) -> String {
use crate::template::Segment;
let iface = iface.to_string();
let mut chunks: Vec<&str> = vec![
GRAMMAR,
addr.system.as_str(),
addr.service.as_str(),
&iface,
kind.as_str(),
];
chunks.extend(template.segments().iter().map(|s| match s {
Segment::Literal(l) => l.as_str(),
Segment::Param(_) => "*",
Segment::Rest(_) => "**",
}));
chunks.join("/")
}
pub fn blind_spots(selector: &str) -> Result<Vec<&'static str>, KeyError> {
let selector = OwnedKeyExpr::autocanonize(selector.to_owned())
.map_err(|e| KeyError::KeyExpr(selector.to_owned(), e.to_string()))?;
let probes = |chunk: &str| -> Vec<String> {
if chunk == CONTROL {
vec![
format!("**/{GRAMMAR}/*/*/{CONTROL}/**"),
format!("**/{GRAMMAR}/{CONTROL}/**"),
]
} else {
vec![format!("**/{GRAMMAR}/*/*/*/{chunk}/**")]
}
};
Ok(VERBATIM
.into_iter()
.filter(|chunk| {
!probes(chunk).iter().any(|p| {
zenoh_keyexpr::keyexpr::new(p.as_str())
.is_ok_and(|probe| selector.intersects(probe))
})
})
.collect())
}
macro_rules! hex_id {
($(#[$m:meta])* $t:ident, $what:literal, $len:literal) => {
$(#[$m])*
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $t(String);
impl $t {
pub fn new(s: &str) -> Result<Self, KeyError> {
if is_lower_hex(s, $len) {
Ok(Self(s.to_owned()))
} else {
Err(KeyError::Hex { what: $what, value: s.to_owned(), len: $len })
}
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl FromStr for $t {
type Err = KeyError;
fn from_str(s: &str) -> Result<Self, KeyError> {
Self::new(s)
}
}
impl fmt::Display for $t {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
};
}
hex_id!(
InstanceId,
"instance id",
16
);
hex_id!(
Fp16,
"fingerprint prefix",
16
);
hex_id!(
Sha256Hex,
"sha256",
64
);
impl InstanceId {
#[must_use]
pub fn from_u64(v: u64) -> Self {
Self(format!("{v:016x}"))
}
}
impl Sha256Hex {
#[must_use]
pub fn fp16(&self) -> Fp16 {
Fp16(self.0[..16].to_owned())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Key(OwnedKeyExpr);
impl Key {
fn from_chunks(chunks: &[&str]) -> Result<Self, KeyError> {
let s = chunks.join("/");
OwnedKeyExpr::try_from(s.clone())
.map(Self)
.map_err(|e| KeyError::KeyExpr(s, e.to_string()))
}
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
#[must_use]
pub fn into_keyexpr(self) -> OwnedKeyExpr {
self.0
}
}
impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
pub fn data_key(
addr: &Addr,
iface: &IfaceId,
kind: KindToken,
resource: &[&str],
) -> Result<Key, KeyError> {
if resource.is_empty() {
return Err(KeyError::Shape(
String::new(),
"a data key needs a resource path",
));
}
for c in resource {
if !is_plain_chunk(c) {
return Err(KeyError::NotPlain {
what: "resource chunk",
value: (*c).to_owned(),
});
}
}
let iface = iface.to_string();
let mut chunks = vec![
GRAMMAR,
addr.system.as_str(),
addr.service.as_str(),
&iface,
kind.as_str(),
];
chunks.extend_from_slice(resource);
Key::from_chunks(&chunks)
}
pub fn instance_key(addr: &Addr, instance: &InstanceId) -> Result<Key, KeyError> {
Key::from_chunks(&[
GRAMMAR,
addr.system.as_str(),
addr.service.as_str(),
CONTROL,
"instance",
instance.as_str(),
])
}
pub fn alive_key(
addr: &Addr,
iface: &IfaceId,
instance: &InstanceId,
fp: &Fp16,
) -> Result<Key, KeyError> {
let iface = iface.to_string();
Key::from_chunks(&[
GRAMMAR,
addr.system.as_str(),
addr.service.as_str(),
CONTROL,
"alive",
&iface,
instance.as_str(),
fp.as_str(),
])
}
pub fn member_key(
addr: &Addr,
iface: &IfaceId,
member: &str,
epoch: &InstanceId,
) -> Result<Key, KeyError> {
if !is_plain_chunk(member) {
return Err(KeyError::NotPlain {
what: "member",
value: member.to_owned(),
});
}
let iface = iface.to_string();
Key::from_chunks(&[
GRAMMAR,
addr.system.as_str(),
addr.service.as_str(),
CONTROL,
"member",
&iface,
member,
epoch.as_str(),
])
}
pub fn contract_key(iface: &IfaceId, fingerprint: &Sha256Hex) -> Result<Key, KeyError> {
let iface = iface.to_string();
Key::from_chunks(&[GRAMMAR, CONTROL, "contract", &iface, fingerprint.as_str()])
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ZkKey {
Data {
addr: Addr,
iface: IfaceId,
kind: KindToken,
resource: Vec<String>,
},
Instance {
addr: Addr,
instance: InstanceId,
},
Alive {
addr: Addr,
iface: IfaceId,
instance: InstanceId,
fp: Fp16,
},
Member {
addr: Addr,
iface: IfaceId,
member: String,
epoch: InstanceId,
},
Contract {
iface: IfaceId,
fingerprint: Sha256Hex,
},
}
impl ZkKey {
pub fn to_key(&self) -> Result<Key, KeyError> {
match self {
Self::Data {
addr,
iface,
kind,
resource,
} => {
let r: Vec<&str> = resource.iter().map(String::as_str).collect();
data_key(addr, iface, *kind, &r)
}
Self::Instance { addr, instance } => instance_key(addr, instance),
Self::Alive {
addr,
iface,
instance,
fp,
} => alive_key(addr, iface, instance, fp),
Self::Member {
addr,
iface,
member,
epoch,
} => member_key(addr, iface, member, epoch),
Self::Contract { iface, fingerprint } => contract_key(iface, fingerprint),
}
}
}
pub fn parse(key: &str) -> Result<ZkKey, KeyError> {
let shape = |why| KeyError::Shape(key.to_owned(), why);
let c: Vec<&str> = key.split('/').collect();
if c.first() != Some(&GRAMMAR) {
return Err(shape("it does not start with zk2/"));
}
if c.get(1) == Some(&CONTROL) {
return match c.as_slice() {
[_, _, "contract", iface, sha] => Ok(ZkKey::Contract {
iface: iface.parse()?,
fingerprint: sha.parse()?,
}),
_ => Err(shape("zk2/@zk/ holds only contract/<iface>/<sha256>")),
};
}
if c.len() < 6 {
return Err(shape("too few chunks"));
}
let addr = Addr::new(c[1], c[2])?;
if c[3] == CONTROL {
return match &c[4..] {
["instance", inst] => Ok(ZkKey::Instance {
addr,
instance: inst.parse()?,
}),
["alive", iface, inst, fp] => Ok(ZkKey::Alive {
addr,
iface: iface.parse()?,
instance: inst.parse()?,
fp: fp.parse()?,
}),
["member", iface, member, epoch] => {
if !is_plain_chunk(member) {
return Err(KeyError::NotPlain {
what: "member",
value: (*member).to_owned(),
});
}
Ok(ZkKey::Member {
addr,
iface: iface.parse()?,
member: (*member).to_owned(),
epoch: epoch.parse()?,
})
}
_ => Err(shape("unknown @zk control key")),
};
}
let iface: IfaceId = c[3].parse()?;
let kind: KindToken = c[4].parse()?;
let resource: Vec<String> = c[5..].iter().map(|s| (*s).to_owned()).collect();
for r in &resource {
if !is_plain_chunk(r) {
return Err(KeyError::NotPlain {
what: "resource chunk",
value: r.clone(),
});
}
}
if kind == KindToken::Events
&& !resource
.last()
.is_some_and(|l| crate::chunk::is_occurrence(l))
{
return Err(shape("an events key ends in an occurrence chunk"));
}
Ok(ZkKey::Data {
addr,
iface,
kind,
resource,
})
}
#[cfg(test)]
mod tests {
use super::*;
fn addr() -> Addr {
Addr::new("vehicle-01", "navigation").unwrap()
}
#[test]
fn a_template_selector_matches_its_members() {
use crate::template::Template;
let iface: IfaceId = "modem.v3".parse().unwrap();
let t = Template::parse("sdu/endpoint/{endpoint}").unwrap();
let sel = template_selector(&addr(), &iface, KindToken::State, &t);
assert_eq!(
sel,
"zk2/vehicle-01/navigation/modem.v3/state/sdu/endpoint/*"
);
let one = Template::parse("radio").unwrap();
assert_eq!(
template_selector(&addr(), &iface, KindToken::ExplicitState, &one),
"zk2/vehicle-01/navigation/modem.v3/@state/radio"
);
let rest = Template::parse("logs/{path...}").unwrap();
assert!(template_selector(&addr(), &iface, KindToken::State, &rest).ends_with("/logs/**"));
let key = data_key(
&addr(),
&iface,
KindToken::State,
&["sdu", "endpoint", "e1"],
)
.unwrap()
.to_string();
let sel = zenoh_keyexpr::keyexpr::new(sel.as_str()).unwrap();
assert!(sel.includes(zenoh_keyexpr::keyexpr::new(key.as_str()).unwrap()));
}
#[test]
fn iface_chunks() {
let n: IfaceId = "nav.v2".parse().unwrap();
assert_eq!((n.name(), n.major()), ("nav", 2));
let t: IfaceId = "tc.netem.v1".parse().unwrap();
assert_eq!((t.name(), t.major()), ("tc.netem", 1));
for bad in [
"nav",
"nav.v",
"nav.vx",
"Nav.v2",
"nav.v02",
".v2",
"nav..x.v1",
"a.v2.v3",
"v2.v3",
] {
assert!(bad.parse::<IfaceId>().is_err(), "{bad}");
}
assert!(
IfaceId::new("acme.v2", 1).is_err(),
"a name may not end in .v<int>"
);
}
#[test]
fn data_keys_round_trip() {
let k = data_key(
&addr(),
&"nav.v2".parse().unwrap(),
KindToken::Stream,
&["position"],
)
.unwrap();
assert_eq!(
k.as_str(),
"zk2/vehicle-01/navigation/nav.v2/stream/position"
);
let p = parse(k.as_str()).unwrap();
assert_eq!(p.to_key().unwrap(), k);
let op = data_key(
&addr(),
&"nav.v2".parse().unwrap(),
KindToken::Op,
&["set_origin"],
)
.unwrap();
assert_eq!(
op.as_str(),
"zk2/vehicle-01/navigation/nav.v2/@op/set_origin"
);
assert_eq!(parse(op.as_str()).unwrap().to_key().unwrap(), op);
}
#[test]
fn control_keys_round_trip() {
let i = InstanceId::from_u64(0x8f3a_5c2e_9b1d_4f70);
let sha = Sha256Hex::new(&"3fa9c2d41b7e9a01".repeat(4)).unwrap();
let iface: IfaceId = "nav.v2".parse().unwrap();
for k in [
instance_key(&addr(), &i).unwrap(),
alive_key(&addr(), &iface, &i, &sha.fp16()).unwrap(),
member_key(&addr(), &iface, "rf0", &i).unwrap(),
contract_key(&iface, &sha).unwrap(),
] {
assert_eq!(parse(k.as_str()).unwrap().to_key().unwrap(), k, "{k}");
}
assert_eq!(
alive_key(&addr(), &iface, &i, &sha.fp16())
.unwrap()
.as_str(),
"zk2/vehicle-01/navigation/@zk/alive/nav.v2/8f3a5c2e9b1d4f70/3fa9c2d41b7e9a01"
);
}
#[test]
fn refusals() {
for bad in [
"v1/h-3fa9c2d41b7e/state/sysinfo/health",
"zk2/vehicle-01/navigation/nav.v2/telemetry/x",
"zk2/Vehicle/navigation/nav.v2/stream/x",
"zk2/vehicle-01/navigation/nav.v2/stream",
"zk2/vehicle-01/navigation/nav.v2/events/applied/not-a-chunk",
"zk2/vehicle-01/navigation/nav.v2/events/applied/01jgxqz4yqk8v6txw3m9f2a7cd",
"zk2/@zk/contract/nav.v2/abc",
"zk2/vehicle-01/navigation/@zk/instance/XYZ",
] {
assert!(parse(bad).is_err(), "{bad}");
}
}
#[test]
fn verbatim_tokens() {
assert!(KindToken::ExplicitStream.is_verbatim());
assert!(KindToken::ExplicitState.is_verbatim());
assert!(KindToken::Op.is_verbatim());
assert!(!KindToken::Events.is_verbatim());
assert!(!KindToken::Stream.is_verbatim());
let verbatim: Vec<&str> = KindToken::ALL
.into_iter()
.filter(|k| k.is_verbatim())
.map(KindToken::as_str)
.collect();
assert_eq!(
verbatim,
VERBATIM[..3],
"VERBATIM lists the verbatim kinds, then @zk"
);
}
fn one_of_each() -> (Vec<(KindToken, Key)>, Vec<Key>) {
let nav: IfaceId = "nav.v2".parse().unwrap();
let data = KindToken::ALL
.into_iter()
.map(|k| {
let resource: &[&str] = if k == KindToken::Events {
&["applied", "01hzy3d1s5k8b2c4e6g8j0m2p4"]
} else {
&["position"]
};
(k, data_key(&addr(), &nav, k, resource).unwrap())
})
.collect();
let i = InstanceId::from_u64(1);
let sha = Sha256Hex::new(&"3fa9c2d41b7e9a01".repeat(4)).unwrap();
let control = vec![
instance_key(&addr(), &i).unwrap(),
alive_key(&addr(), &nav, &i, &sha.fp16()).unwrap(),
member_key(&addr(), &nav, "rf0", &i).unwrap(),
contract_key(&nav, &sha).unwrap(),
];
(data, control)
}
fn reaches(selector: &str, key: &Key) -> bool {
zenoh_keyexpr::keyexpr::new(selector)
.unwrap()
.intersects(&key.clone().into_keyexpr())
}
#[test]
fn ambient_never_reaches_a_verbatim_chunk() {
let (data, control) = one_of_each();
for (kind, key) in &data {
assert_eq!(
reaches(AMBIENT, key),
!kind.is_verbatim(),
"{AMBIENT} and {key}"
);
}
for key in &control {
assert!(!reaches(AMBIENT, key), "{AMBIENT} reached {key}");
}
assert_eq!(blind_spots(AMBIENT).unwrap(), VERBATIM);
}
#[test]
fn kind_selectors_have_fixed_arity() {
let (data, control) = one_of_each();
for kind in KindToken::ALL {
let selector = kind_selector(None, None, None, kind);
assert_eq!(selector, format!("zk2/*/*/*/{kind}/**"));
for (other, key) in &data {
assert_eq!(reaches(&selector, key), *other == kind, "{selector}, {key}");
}
for key in &control {
assert!(!reaches(&selector, key), "{selector} reached {key}");
}
let blind = blind_spots(&selector).unwrap();
assert!(!blind.contains(&kind.as_str()), "{selector} sees {kind}");
assert!(blind.contains(&CONTROL));
}
let deeper = "zk2/vehicle-01/navigation/nav.v2/stream/state/x";
let deeper = Key(OwnedKeyExpr::try_from(deeper.to_owned()).unwrap());
assert!(!reaches(
&kind_selector(None, None, None, KindToken::State),
&deeper
));
let a = addr();
let nav: IfaceId = "nav.v2".parse().unwrap();
assert_eq!(
kind_selector(
Some(&a.system),
Some(&a.service),
Some(&nav),
KindToken::ExplicitState
),
"zk2/vehicle-01/navigation/nav.v2/@state/**"
);
assert_eq!(
kind_selector(Some(&a.system), None, None, KindToken::Op),
"zk2/vehicle-01/*/*/@op/**"
);
}
#[test]
fn blind_spots_name_what_a_selector_cannot_see() {
assert_eq!(blind_spots("**").unwrap(), VERBATIM);
assert_eq!(blind_spots("prod/site/zk2/**").unwrap(), VERBATIM);
assert_eq!(blind_spots("zk2/**/**").unwrap(), VERBATIM, "canonized");
assert_eq!(
blind_spots("zk2/*/*/@zk/**").unwrap(),
["@stream", "@state", "@op"]
);
assert_eq!(
blind_spots("zk2/@zk/contract/**").unwrap(),
["@stream", "@state", "@op"]
);
assert_eq!(
blind_spots("prod/zk2/robot1/*/nav.v2/@stream/**").unwrap(),
["@state", "@op", "@zk"]
);
assert!(blind_spots("a//b").is_err());
}
}