#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RoutingEdgeResource {
Ingress,
DnsEndpoint,
}
impl RoutingEdgeResource {
pub const ALL: [Self; 2] = [Self::Ingress, Self::DnsEndpoint];
pub const fn api_version(self) -> &'static str {
match self {
Self::Ingress => "networking.k8s.io/v1",
Self::DnsEndpoint => "externaldns.k8s.io/v1alpha1",
}
}
pub const fn kind(self) -> &'static str {
match self {
Self::Ingress => "Ingress",
Self::DnsEndpoint => "DNSEndpoint",
}
}
pub const fn wire_identity(self) -> crate::k8s_wire_identity::K8sWireIdentity {
crate::k8s_wire_identity::K8sWireIdentity::new(self.api_version(), self.kind())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_enumerates_every_variant_exactly_once() {
assert_eq!(RoutingEdgeResource::ALL.len(), 2);
let mut seen = std::collections::HashSet::new();
for v in RoutingEdgeResource::ALL {
assert!(seen.insert(v), "duplicate variant in ALL: {v:?}");
}
}
#[test]
fn ingress_api_version_is_networking_k8s_io_v1() {
assert_eq!(
RoutingEdgeResource::Ingress.api_version(),
"networking.k8s.io/v1"
);
}
#[test]
fn dns_endpoint_api_version_is_externaldns_k8s_io_v1alpha1() {
assert_eq!(
RoutingEdgeResource::DnsEndpoint.api_version(),
"externaldns.k8s.io/v1alpha1"
);
}
#[test]
fn ingress_kind_is_pascalcase_ingress() {
assert_eq!(RoutingEdgeResource::Ingress.kind(), "Ingress");
}
#[test]
fn dns_endpoint_kind_is_pascalcase_dns_endpoint() {
assert_eq!(RoutingEdgeResource::DnsEndpoint.kind(), "DNSEndpoint");
}
#[test]
fn every_variants_api_version_and_kind_are_distinct_across_the_closed_set() {
let mut api_versions = std::collections::HashSet::new();
let mut kinds = std::collections::HashSet::new();
for v in RoutingEdgeResource::ALL {
assert!(
api_versions.insert(v.api_version()),
"duplicate api_version at {v:?}: {}",
v.api_version()
);
assert!(
kinds.insert(v.kind()),
"duplicate kind at {v:?}: {}",
v.kind()
);
}
}
#[test]
fn api_version_and_kind_are_const_fn_reachable() {
const I_AV: &str = RoutingEdgeResource::Ingress.api_version();
const I_K: &str = RoutingEdgeResource::Ingress.kind();
const D_AV: &str = RoutingEdgeResource::DnsEndpoint.api_version();
const D_K: &str = RoutingEdgeResource::DnsEndpoint.kind();
assert_eq!(I_AV, "networking.k8s.io/v1");
assert_eq!(I_K, "Ingress");
assert_eq!(D_AV, "externaldns.k8s.io/v1alpha1");
assert_eq!(D_K, "DNSEndpoint");
}
#[test]
fn wire_identity_pairs_api_version_and_kind_per_variant() {
for v in RoutingEdgeResource::ALL {
let id = v.wire_identity();
assert_eq!(id.api_version, v.api_version());
assert_eq!(id.kind, v.kind());
}
}
#[test]
fn wire_identity_is_const_reachable() {
const I: crate::k8s_wire_identity::K8sWireIdentity =
RoutingEdgeResource::Ingress.wire_identity();
const D: crate::k8s_wire_identity::K8sWireIdentity =
RoutingEdgeResource::DnsEndpoint.wire_identity();
assert_eq!(I.api_version, "networking.k8s.io/v1");
assert_eq!(I.kind, "Ingress");
assert_eq!(D.api_version, "externaldns.k8s.io/v1alpha1");
assert_eq!(D.kind, "DNSEndpoint");
}
#[test]
fn projections_are_pure_functions_of_the_variant() {
for v in RoutingEdgeResource::ALL {
assert_eq!(v.api_version(), v.api_version());
assert_eq!(v.kind(), v.kind());
}
}
}