summaryrefslogtreecommitdiff
path: root/src/eval/ops.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-06-18 11:59:05 +0200
committerLaurenz <laurmaedje@gmail.com>2021-06-18 11:59:05 +0200
commitbca035172c463e6ac4aaf2591d7d4af2da51c522 (patch)
treed17ba4c0208caab1d30f6f2d19821cbd203e37fa /src/eval/ops.rs
parent8b6391040e3fb2ab5f739e26f88621d63ad5d3cc (diff)
Join semantics
Diffstat (limited to 'src/eval/ops.rs')
-rw-r--r--src/eval/ops.rs29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/eval/ops.rs b/src/eval/ops.rs
index b6bd5402..2537f4a5 100644
--- a/src/eval/ops.rs
+++ b/src/eval/ops.rs
@@ -3,6 +3,33 @@ use std::cmp::Ordering::*;
use super::{TemplateNode, Value};
use Value::*;
+/// Join a value with another value.
+pub fn join(lhs: Value, rhs: Value) -> Result<Value, Value> {
+ Ok(match (lhs, rhs) {
+ (_, Error) => Error,
+ (Error, _) => Error,
+
+ (a, None) => a,
+ (None, b) => b,
+
+ (Str(a), Str(b)) => Str(a + &b),
+ (Array(a), Array(b)) => Array(concat(a, b)),
+ (Dict(a), Dict(b)) => Dict(concat(a, b)),
+
+ (Template(a), Template(b)) => Template(concat(a, b)),
+ (Template(mut a), Str(b)) => Template({
+ a.push(TemplateNode::Str(b));
+ a
+ }),
+ (Str(a), Template(mut b)) => Template({
+ b.insert(0, TemplateNode::Str(a));
+ b
+ }),
+
+ (a, _) => return Err(a),
+ })
+}
+
/// Apply the plus operator to a value.
pub fn pos(value: Value) -> Value {
match value {
@@ -60,8 +87,6 @@ pub fn add(lhs: Value, rhs: Value) -> Value {
(Dict(a), Dict(b)) => Dict(concat(a, b)),
(Template(a), Template(b)) => Template(concat(a, b)),
- (Template(a), None) => Template(a),
- (None, Template(b)) => Template(b),
(Template(mut a), Str(b)) => Template({
a.push(TemplateNode::Str(b));
a