Skip to main content

bonds_core/
query.rs

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