use std::fmt;
use crate::ids::{ClusterId, IndexName};
#[derive(Clone, Debug)]
pub struct Target {
pub cluster: ClusterId,
pub index: IndexName,
pub endpoint: Option<String>,
}
impl Target {
#[must_use]
pub fn new(cluster: ClusterId, index: IndexName) -> Self {
Self {
cluster,
index,
endpoint: None,
}
}
#[must_use]
pub fn with_endpoint(mut self, endpoint: Option<String>) -> Self {
self.endpoint = endpoint;
self
}
}
impl PartialEq for Target {
fn eq(&self, other: &Self) -> bool {
self.cluster == other.cluster && self.index == other.index
}
}
impl Eq for Target {}
impl std::hash::Hash for Target {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.cluster.hash(state);
self.index.hash(state);
}
}
impl fmt::Display for Target {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.cluster, self.index)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn target_exposes_cluster_and_index_and_displays_path_like() {
let target = Target::new(ClusterId::from("us-2"), IndexName::from("orders-7"));
assert_eq!(target.cluster.as_str(), "us-2");
assert_eq!(target.index.as_str(), "orders-7");
assert_eq!(target.to_string(), "us-2/orders-7");
}
#[test]
fn targets_compare_by_both_fields() {
let a = Target::new(ClusterId::from("c"), IndexName::from("i"));
let b = Target::new(ClusterId::from("c"), IndexName::from("j"));
assert_ne!(a, b);
assert_eq!(a, a.clone());
}
}