|
| 1 | +// |
| 2 | +// ArithmeticExpression.swift |
| 3 | +// CoreModel |
| 4 | +// |
| 5 | +// Created by Alsey Coleman Miller on 8/16/26. |
| 6 | +// Copyright © 2026 PureSwift. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +public extension FetchRequest.Predicate { |
| 10 | + |
| 11 | + /// An arithmetic operation on two expressions (e.g. `age + 1`). |
| 12 | + struct ArithmeticExpression: Equatable, Hashable, Sendable { |
| 13 | + |
| 14 | + /// The arithmetic function to apply. |
| 15 | + public var function: Function |
| 16 | + |
| 17 | + /// The left operand. |
| 18 | + public var left: Expression |
| 19 | + |
| 20 | + /// The right operand. |
| 21 | + public var right: Expression |
| 22 | + |
| 23 | + public init(function: Function, left: Expression, right: Expression) { |
| 24 | + self.function = function |
| 25 | + self.left = left |
| 26 | + self.right = right |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// MARK: - Supporting Types |
| 32 | + |
| 33 | +public extension FetchRequest.Predicate.ArithmeticExpression { |
| 34 | + |
| 35 | + /// Arithmetic function. |
| 36 | + /// |
| 37 | + /// Raw values match the corresponding `NSExpression` function names, |
| 38 | + /// with operands passed in `(left, right)` order. |
| 39 | + enum Function: String, Sendable, CaseIterable { |
| 40 | + |
| 41 | + /// Addition (`left + right`). |
| 42 | + case add = "add:to:" |
| 43 | + |
| 44 | + /// Subtraction (`left - right`). |
| 45 | + case subtract = "from:subtract:" |
| 46 | + |
| 47 | + /// Multiplication (`left * right`). |
| 48 | + case multiply = "multiply:by:" |
| 49 | + |
| 50 | + /// Division (`left / right`). |
| 51 | + /// |
| 52 | + /// Integer operands divide truncating, the way Swift's `/` and |
| 53 | + /// `NSExpression`'s `divide:by:` both do; floating-point operands |
| 54 | + /// produce a floating-point value. |
| 55 | + case divide = "divide:by:" |
| 56 | + |
| 57 | + /// Remainder (`left % right`), integers only. |
| 58 | + case modulus = "modulus:by:" |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +public extension FetchRequest.Predicate.ArithmeticExpression.Function { |
| 63 | + |
| 64 | + /// The operator symbol (e.g. `+`). |
| 65 | + var symbol: String { |
| 66 | + switch self { |
| 67 | + case .add: return "+" |
| 68 | + case .subtract: return "-" |
| 69 | + case .multiply: return "*" |
| 70 | + case .divide: return "/" |
| 71 | + case .modulus: return "%" |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// MARK: - CustomStringConvertible |
| 77 | + |
| 78 | +extension FetchRequest.Predicate.ArithmeticExpression: CustomStringConvertible { |
| 79 | + |
| 80 | + public var description: String { |
| 81 | + "(" + left.description + " " + function.symbol + " " + right.description + ")" |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// MARK: - Codable |
| 86 | + |
| 87 | +#if !hasFeature(Embedded) |
| 88 | +extension FetchRequest.Predicate.ArithmeticExpression: Codable {} |
| 89 | +extension FetchRequest.Predicate.ArithmeticExpression.Function: Codable {} |
| 90 | +#endif |
0 commit comments