zrx_id/id/expression/operand.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//! Operand.
27
28use std::fmt::{self, Debug};
29
30use super::Expression;
31
32mod convert;
33mod operator;
34mod term;
35
36pub use convert::TryIntoOperand;
37pub use operator::Operator;
38pub use term::Term;
39
40// ----------------------------------------------------------------------------
41// Enums
42// ----------------------------------------------------------------------------
43
44/// Operand.
45#[derive(Clone, PartialEq, Eq)]
46pub enum Operand {
47 /// Expression.
48 Expression(Expression),
49 /// Term.
50 Term(Term),
51}
52
53// ----------------------------------------------------------------------------
54// Trait implementations
55// ----------------------------------------------------------------------------
56
57impl From<Expression> for Operand {
58 /// Creates an operand from an expression.
59 #[inline]
60 fn from(expr: Expression) -> Self {
61 Operand::Expression(expr)
62 }
63}
64
65impl<T> From<T> for Operand
66where
67 T: Into<Term>,
68{
69 /// Creates an operand from a term.
70 #[inline]
71 fn from(term: T) -> Self {
72 Operand::Term(term.into())
73 }
74}
75
76// ----------------------------------------------------------------------------
77
78impl Debug for Operand {
79 /// Formats the operand for debugging.
80 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81 match self {
82 Operand::Expression(expr) => Debug::fmt(expr, f),
83 Operand::Term(term) => Debug::fmt(term, f),
84 }
85 }
86}