git_bug/query/queryable/
mod.rs

1// git-bug-rs - A rust library for interfacing with git-bug repositories
2//
3// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
4// SPDX-License-Identifier: GPL-3.0-or-later
5//
6// This file is part of git-bug-rs/git-gub.
7//
8// You should have received a copy of the License along with this program.
9// If not, see <https://www.gnu.org/licenses/agpl.txt>.
10
11//! The actual code to match an [`Entity`][`crate::replica::entity::Entity`]
12//! from [`Query`][`super::Query`].
13
14/// Specific trait that specifies that something can be matched by a
15/// [`Query`][`super::Query`].
16pub trait Queryable {
17    /// The type defining the possible keys a user can input.
18    type KeyValue: std::fmt::Debug + QueryKeyValue + Clone;
19
20    /// Check whether the key value pair is a match for this [`Queryable`]
21    /// object.
22    fn matches(&self, key: &Self::KeyValue) -> bool;
23}
24
25/// The trait, implemented by the [`Queryable::KeyValue`] types.
26pub trait QueryKeyValue {
27    /// The Error that is returned when trying to construct an [`QueryKeyValue`]
28    /// from a string.
29    type Err;
30
31    /// A general state value that is inserted into each `from_*` call.
32    /// It can be used to track user state between these calls.
33    ///
34    /// Set this to [`()`] if you do not need this.
35    type UserState;
36
37    /// Construct this [`QueryKeyValue`] from a key name and a value.
38    ///
39    /// # Errors
40    /// If `value` is not a correct value for the key `key`.
41    fn from_key_value(
42        user_state: &Self::UserState,
43        key: &str,
44        value: String,
45    ) -> Result<Self, Self::Err>
46    where
47        Self: Sized;
48
49    /// Construct this [`QueryKeyValue`] only from a value using the default
50    /// key.
51    ///
52    /// # Errors
53    /// If `value` is not a correct value for the default key.
54    fn from_value(user_state: &Self::UserState, value: String) -> Result<Self, Self::Err>
55    where
56        Self: Sized;
57
58    /// Split this [`QueryKeyValue`] into it's `key` and `value` parts.
59    ///
60    /// This is used to print a [`Query`][`super::Query`] in it's normalized
61    /// from.
62    fn to_key_and_value(&self) -> (&str, &str)
63    where
64        Self: Sized;
65}