git_bug/query/normalize/
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//! Normalization of a [`Query`].
12
13use std::fmt::Display;
14
15use super::{Matcher, Query, queryable::Queryable};
16use crate::query::queryable::QueryKeyValue;
17
18impl<E: Queryable> Query<E> {
19    /// Turn this [`Query`] into it's normal representations, that actually
20    /// conforms to the EBNF given in the module documentation.
21    ///
22    /// # Note
23    /// This is just calling the [`Display`] implementation.
24    #[must_use]
25    pub fn into_normal_representation(self) -> String {
26        self.to_string()
27    }
28}
29
30impl<E: Queryable> Display for Query<E> {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        if let Some(root) = &self.root {
33            root.fmt(f)
34        } else {
35            write!(f, "")
36        }
37    }
38}
39
40impl<E: Queryable> Display for Matcher<E> {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        match self {
43            Matcher::Or { lhs, rhs } => write!(f, r"({lhs} OR {rhs})"),
44            Matcher::And { lhs, rhs } => write!(f, r"({lhs} AND {rhs})"),
45            Matcher::Match { key_value } => {
46                let (key, value) = key_value.to_key_and_value();
47                write!(f, r#""{key}:{value}""#)
48            }
49        }
50    }
51}