use crate::error::ZmqError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Role {
Bind,
Connect,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct ZmqEndpoint {
pub(crate) address: String,
pub(crate) role: Role,
}
impl ZmqEndpoint {
pub fn bind(address: impl Into<String>) -> Self {
Self {
address: address.into(),
role: Role::Bind,
}
}
pub fn connect(address: impl Into<String>) -> Self {
Self {
address: address.into(),
role: Role::Connect,
}
}
#[must_use]
pub fn address(&self) -> &str {
&self.address
}
pub(crate) fn validate(&self) -> Result<(), ZmqError> {
if self.address.starts_with("tcp://") || self.address.starts_with("ipc://") {
Ok(())
} else {
Err(ZmqError::Invalid(format!(
"'{}' must use the tcp:// or ipc:// transport",
self.address
)))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unsupported_transports_are_rejected_before_io() {
assert!(ZmqEndpoint::bind("inproc://x").validate().is_err());
assert!(ZmqEndpoint::connect("udp://x:1").validate().is_err());
assert!(ZmqEndpoint::bind("tcp://0.0.0.0:5555").validate().is_ok());
assert!(ZmqEndpoint::bind("ipc:///tmp/x").validate().is_ok());
}
}