zrx_id/id/expression/operand/term.rs
1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Term.
27
28use std::fmt::{self, Debug, Display};
29
30use crate::id::selector::Selector;
31use crate::id::Id;
32
33// ----------------------------------------------------------------------------
34// Enums
35// ----------------------------------------------------------------------------
36
37/// Term.
38///
39/// Terms can either be identifiers or selectors, both of which are convertible
40/// into [`Selector`]. By providing [`Id`], the term represents an exact match
41/// on identifiers, whereas providing a [`Selector`] allows for more complex
42/// matching criteria.
43#[derive(Clone, PartialEq, Eq)]
44pub enum Term {
45 /// Identifier.
46 Id(Id),
47 /// Selector.
48 Selector(Selector),
49}
50
51// ----------------------------------------------------------------------------
52// Trait implementations
53// ----------------------------------------------------------------------------
54
55impl From<Id> for Term {
56 /// Creates a term from an identifier.
57 #[inline]
58 fn from(id: Id) -> Self {
59 Term::Id(id)
60 }
61}
62
63impl From<Selector> for Term {
64 /// Creates a term from a selector.
65 #[inline]
66 fn from(selector: Selector) -> Self {
67 Term::Selector(selector)
68 }
69}
70
71// ----------------------------------------------------------------------------
72
73impl Display for Term {
74 /// Formats the term for display.
75 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76 match self {
77 Term::Id(id) => Display::fmt(id, f),
78 Term::Selector(selector) => Display::fmt(selector, f),
79 }
80 }
81}
82
83impl Debug for Term {
84 /// Formats the term for debugging.
85 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 match self {
87 Term::Id(id) => Debug::fmt(id, f),
88 Term::Selector(selector) => Debug::fmt(selector, f),
89 }
90 }
91}