Skip to content

Commit

Permalink
Drop SelectoExt in favor of type associated Matcher
Browse files Browse the repository at this point in the history
Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Jul 19, 2024
1 parent a676a86 commit 11634fb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 45 deletions.
51 changes: 8 additions & 43 deletions kube-core/src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,10 @@ pub struct ParseExpressionError(pub String);

// local type aliases
type Expressions = Vec<Expression>;

mod private {
pub trait Sealed {}
impl<R: super::ResourceExt> Sealed for R {}
}

/// Extensions to [`ResourceExt`](crate::ResourceExt)
/// Helper methods for resource selection based on provided Selector
pub trait SelectorExt: private::Sealed {
fn selector_map(&self) -> &BTreeMap<String, String>;

/// Perform a match on the resource using Matcher trait
///
/// ```
/// 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<R: ResourceExt> SelectorExt for R {
fn selector_map(&self) -> &BTreeMap<String, String> {
self.labels()
}
}
type Map = BTreeMap<String, String>;

/// Matcher trait for implementing alternalive Selectors
pub trait Matcher {
pub trait Matcher<T> {
/// Perform a match check on the resource labels
///
/// ```
Expand All @@ -71,7 +36,7 @@ pub trait Matcher {
/// selector.matches(&Default::default());
/// # Ok::<(), ParseExpressionError>(())
/// ```
fn matches(&self, labels: &BTreeMap<String, String>) -> bool;
fn matches(&self, labels: &T) -> bool;
}

/// A selector expression with existing operations
Expand Down Expand Up @@ -153,7 +118,7 @@ impl Selector {
}

/// Create a selector from a map of key=value label matches
fn from_map(map: BTreeMap<String, String>) -> Self {
fn from_map(map: Map) -> Self {
Self(map.into_iter().map(|(k, v)| Expression::Equal(k, v)).collect())
}

Expand Down Expand Up @@ -183,9 +148,9 @@ impl Selector {
}
}

impl Matcher for Selector {
impl Matcher<Map> for Selector {
/// Perform a match check on the resource labels
fn matches(&self, labels: &BTreeMap<String, String>) -> bool {
fn matches(&self, labels: &Map) -> bool {
for expr in self.0.iter() {
if !expr.matches(labels) {
return false;
Expand All @@ -195,8 +160,8 @@ impl Matcher for Selector {
}
}

impl Matcher for Expression {
fn matches(&self, labels: &BTreeMap<String, String>) -> bool {
impl Matcher<Map> for Expression {
fn matches(&self, labels: &Map) -> bool {
match self {
Expression::In(key, values) => match labels.get(key) {
Some(v) => values.contains(v),
Expand Down
2 changes: 1 addition & 1 deletion kube-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, Matcher, ParseExpressionError, Selector};

#[cfg_attr(docsrs, doc(cfg(feature = "schema")))]
#[cfg(feature = "schema")]
Expand Down
1 change: 0 additions & 1 deletion kube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down

0 comments on commit 11634fb

Please sign in to comment.