diff --git a/kube-core/src/labels.rs b/kube-core/src/labels.rs index fc6302012..0c7a1a019 100644 --- a/kube-core/src/labels.rs +++ b/kube-core/src/labels.rs @@ -11,7 +11,11 @@ use std::{ }; use thiserror::Error; -use crate::ResourceExt; +mod private { + pub trait Sealed {} + impl Sealed for super::Expression {} + impl Sealed for super::Selector {} +} #[derive(Debug, Error)] #[error("failed to parse value as expression: {0}")] @@ -21,49 +25,18 @@ pub struct ParseExpressionError(pub String); // local type aliases type Expressions = Vec; -mod private { - pub trait Sealed {} - impl Sealed for R {} -} - -/// Extensions to [`ResourceExt`](crate::ResourceExt) -/// Helper methods for resource selection based on provided Selector +/// Selector extension trait for querying selector like objects +/// +/// Only implemented by `Selector` and `Expression`. pub trait SelectorExt: private::Sealed { - fn selector_map(&self) -> &BTreeMap; + /// Collection type to compare with self + type Search; - /// Perform a match on the resource using Matcher trait + /// Perform a match check on the arbitrary components like labels /// /// ``` - /// use k8s_openapi::api::core::v1::Pod; /// use k8s_openapi::apimachinery::pkg::apis::meta::v1::LabelSelector; - /// use kube_core::SelectorExt; - /// use kube_core::{Expression, Selector, ParseExpressionError}; - /// - /// let selector: Selector = LabelSelector::default().try_into()?; - /// let matches = Pod::default().selector_matches(&selector); - /// assert!(matches); - /// let matches = Pod::default().selector_matches(&Expression::Exists("foo".into())); - /// assert!(!matches); - /// # Ok::<(), ParseExpressionError>(()) - /// ``` - fn selector_matches(&self, selector: &impl Matcher) -> bool { - selector.matches(self.selector_map()) - } -} - -impl SelectorExt for R { - fn selector_map(&self) -> &BTreeMap { - self.labels() - } -} - -/// Matcher trait for implementing alternalive Selectors -pub trait Matcher { - /// Perform a match check on the resource labels - /// - /// ``` - /// use k8s_openapi::apimachinery::pkg::apis::meta::v1::LabelSelector; - /// use crate::kube_core::Matcher; + /// use crate::kube_core::SelectorExt; /// use crate::kube_core::Selector; /// use kube_core::ParseExpressionError; /// @@ -71,7 +44,7 @@ pub trait Matcher { /// selector.matches(&Default::default()); /// # Ok::<(), ParseExpressionError>(()) /// ``` - fn matches(&self, labels: &BTreeMap) -> bool; + fn matches(&self, on: &Self::Search) -> bool; } /// A selector expression with existing operations @@ -183,7 +156,9 @@ impl Selector { } } -impl Matcher for Selector { +impl SelectorExt for Selector { + type Search = BTreeMap; + /// Perform a match check on the resource labels fn matches(&self, labels: &BTreeMap) -> bool { for expr in self.0.iter() { @@ -195,7 +170,9 @@ impl Matcher for Selector { } } -impl Matcher for Expression { +impl SelectorExt for Expression { + type Search = BTreeMap; + fn matches(&self, labels: &BTreeMap) -> bool { match self { Expression::In(key, values) => match labels.get(key) { diff --git a/kube-core/src/lib.rs b/kube-core/src/lib.rs index 15f6e88c9..93d0caca1 100644 --- a/kube-core/src/lib.rs +++ b/kube-core/src/lib.rs @@ -52,7 +52,7 @@ pub use resource::{ pub mod response; pub use response::Status; -pub use labels::{Expression, Matcher, ParseExpressionError, Selector, SelectorExt}; +pub use labels::{Expression, ParseExpressionError, Selector, SelectorExt}; #[cfg_attr(docsrs, doc(cfg(feature = "schema")))] #[cfg(feature = "schema")] diff --git a/kube/src/lib.rs b/kube/src/lib.rs index c94a8bb31..52074ff3a 100644 --- a/kube/src/lib.rs +++ b/kube/src/lib.rs @@ -199,7 +199,6 @@ pub mod prelude { #[cfg(feature = "unstable-client")] pub use crate::client::scope::NamespacedRef; #[allow(unreachable_pub)] pub use crate::core::PartialObjectMetaExt as _; - #[allow(unreachable_pub)] pub use crate::core::SelectorExt as _; pub use crate::{core::crd::CustomResourceExt as _, Resource as _, ResourceExt as _}; #[cfg(feature = "runtime")] pub use crate::runtime::utils::WatchStreamExt as _;