git-bug 0.2.4

A rust library for interfacing with git-bug repositories
Documentation
// git-bug-rs - A rust library for interfacing with git-bug repositories
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of git-bug-rs/git-gub.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/agpl.txt>.

//! The actual code to match an [`Entity`][`crate::replica::entity::Entity`]
//! from [`Query`][`super::Query`].

/// Specific trait that specifies that something can be matched by a
/// [`Query`][`super::Query`].
pub trait Queryable {
    /// The type defining the possible keys a user can input.
    type KeyValue: std::fmt::Debug + QueryKeyValue + Clone;

    /// Check whether the key value pair is a match for this [`Queryable`]
    /// object.
    fn matches(&self, key: &Self::KeyValue) -> bool;
}

/// The trait, implemented by the [`Queryable::KeyValue`] types.
pub trait QueryKeyValue {
    /// The Error that is returned when trying to construct an [`QueryKeyValue`]
    /// from a string.
    type Err;

    /// A general state value that is inserted into each `from_*` call.
    /// It can be used to track user state between these calls.
    ///
    /// Set this to [`()`] if you do not need this.
    type UserState;

    /// Construct this [`QueryKeyValue`] from a key name and a value.
    ///
    /// # Errors
    /// If `value` is not a correct value for the key `key`.
    fn from_key_value(
        user_state: &Self::UserState,
        key: &str,
        value: String,
    ) -> Result<Self, Self::Err>
    where
        Self: Sized;

    /// Construct this [`QueryKeyValue`] only from a value using the default
    /// key.
    ///
    /// # Errors
    /// If `value` is not a correct value for the default key.
    fn from_value(user_state: &Self::UserState, value: String) -> Result<Self, Self::Err>
    where
        Self: Sized;

    /// Split this [`QueryKeyValue`] into it's `key` and `value` parts.
    ///
    /// This is used to print a [`Query`][`super::Query`] in it's normalized
    /// from.
    fn to_key_and_value(&self) -> (&str, &str)
    where
        Self: Sized;
}