Skip to main content

bonds_core/
query.rs

1use std::path::{Path, PathBuf};
2
3/// Metadata matching rules for bond queries.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum MetadataFilter {
6    /// Match bonds that contain a metadata key, regardless of value.
7    HasKey(String),
8    /// Match bonds where `key == value`.
9    KeyValue { key: String, value: String },
10}
11
12/// Composable filter object for querying bonds.
13///
14/// All fields are optional. When multiple fields are set, they are combined with AND logic.
15#[derive(Debug, Clone, Default, PartialEq, Eq)]
16pub struct BondQuery {
17    pub source: Option<PathBuf>,
18    pub target: Option<PathBuf>,
19    pub metadata: Option<MetadataFilter>,
20}
21
22impl BondQuery {
23    /// Start with an empty query (matches all bonds).
24    pub fn new() -> Self {
25        Self::default()
26    }
27
28    /// Add an exact source-path filter.
29    pub fn with_source<P: AsRef<Path>>(mut self, source: P) -> Self {
30        self.source = Some(source.as_ref().to_path_buf());
31        self
32    }
33
34    /// Add an exact target-path filter.
35    pub fn with_target<P: AsRef<Path>>(mut self, target: P) -> Self {
36        self.target = Some(target.as_ref().to_path_buf());
37        self
38    }
39
40    /// Add a metadata key-exists filter.
41    pub fn with_metadata_key(mut self, key: impl Into<String>) -> Self {
42        self.metadata = Some(MetadataFilter::HasKey(key.into()));
43        self
44    }
45
46    /// Add a metadata key/value filter.
47    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
48        self.metadata = Some(MetadataFilter::KeyValue {
49            key: key.into(),
50            value: value.into(),
51        });
52        self
53    }
54}