1use crate::ops::{Add, AsProxy};
2
3use rbs::Value;
4use std::borrow::Cow;
5
6fn op_add_u64(value: &Value, other: u64) -> u64 {
7 value.u64() + other
8}
9
10fn op_add_i64(value: &Value, other: i64) -> i64 {
11 value.i64() + other
12}
13
14fn op_add_f64(value: &Value, other: f64) -> f64 {
15 value.f64() + other
16}
17
18macro_rules! impl_numeric_add {
19 ($($eq:ident [$($ty:ty)*]-> $return_ty:ty)*) => {
20 $($(
21 impl Add<$ty> for Value {
22 type Output = $return_ty;
23 fn op_add(self, other: $ty) -> Self::Output {
24 $eq(&self, other as _)
25 }
26 }
27
28 impl Add<Value> for $ty {
29 type Output = $return_ty;
30 fn op_add(self, other: Value) -> Self::Output {
31 $eq(&other, self as _)
32 }
33 }
34
35 impl Add<&Value> for $ty {
36 type Output = $return_ty;
37 fn op_add(self, other: &Value) -> Self::Output {
38 $eq(other, self as _)
39 }
40 }
41
42 impl Add<&&Value> for $ty {
43 type Output = $return_ty;
44 fn op_add(self, other: &&Value) -> Self::Output {
45 $eq(*other, self as _)
46 }
47 }
48
49 impl<'a> Add<$ty> for &'a Value {
50 type Output = $return_ty;
51 fn op_add(self, other: $ty) -> Self::Output {
52 $eq(self, other as _)
53 }
54 }
55
56 impl<'a> Add<&$ty> for &'a Value {
57 type Output = $return_ty;
58 fn op_add(self, other: &$ty) -> Self::Output {
59 $eq(self, *other as _)
60 }
61 }
62 )*)*
63 }
64}
65
66impl_numeric_add! {
67 op_add_u64[u8 u16 u32 u64] -> u64
68 op_add_i64[i8 i16 i32 i64 isize] -> i64
69 op_add_f64[f32 f64] -> f64
70}
71
72impl Add<&Value> for Value {
74 type Output = Value;
75 fn op_add(self, rhs: &Value) -> Self::Output {
76 return match self {
77 Value::String(s) => Value::String(s + rhs.as_str().unwrap_or("")),
78 Value::I32(s) => Value::I32(s + rhs.i32()),
79 Value::I64(s) => Value::I64(s + rhs.i64()),
80 Value::U32(s) => Value::U32(s + rhs.u32()),
81 Value::U64(s) => Value::U64(s + rhs.u64()),
82 Value::F64(s) => Value::F64(s + rhs.as_f64().unwrap_or_default()),
83 Value::F32(v) => Value::F64(v as f64 + rhs.as_f64().unwrap_or_default()),
84 _ => {
85 return Value::Null;
86 }
87 };
88 }
89}
90
91impl Add<&&Value> for Value {
92 type Output = Value;
93 fn op_add(self, rhs: &&Value) -> Self::Output {
94 return match self {
95 Value::String(s) => Value::String(s + rhs.as_str().unwrap_or("")),
96 Value::I32(s) => Value::I32(s + rhs.i32()),
97 Value::I64(s) => Value::I64(s + rhs.i64()),
98 Value::U32(s) => Value::U32(s + rhs.u32()),
99 Value::U64(s) => Value::U64(s + rhs.u64()),
100 Value::F64(s) => Value::F64(s + rhs.as_f64().unwrap_or_default()),
101 Value::F32(s) => Value::F64((s as f64 + rhs.as_f64().unwrap_or_default()) as f64),
102 _ => {
103 return Value::Null;
104 }
105 };
106 }
107}
108
109impl Add<Value> for Value {
110 type Output = Value;
111 fn op_add(self, rhs: Value) -> Self::Output {
112 return match self {
113 Value::String(s) => Value::String(s + rhs.as_str().unwrap_or("")),
114 Value::I32(s) => Value::I32(s + rhs.i32()),
115 Value::I64(s) => Value::I64(s + rhs.i64()),
116 Value::U32(s) => Value::U32(s + rhs.u32()),
117 Value::U64(s) => Value::U64(s + rhs.u64()),
118 Value::F64(s) => Value::F64(s + rhs.as_f64().unwrap_or_default()),
119 Value::F32(s) => {
120 Value::F64((s as f64 as f64 + rhs.as_f64().unwrap_or_default()) as f64)
121 }
122 _ => {
123 return Value::Null;
124 }
125 };
126 }
127}
128
129impl Add<&Value> for &Value {
130 type Output = Value;
131 fn op_add(self, rhs: &Value) -> Self::Output {
132 return match self {
133 Value::String(s) => Value::String(s.to_owned() + rhs.as_str().unwrap_or("")),
134 Value::I32(s) => Value::I32(s + rhs.i32()),
135 Value::I64(s) => Value::I64(s + rhs.i64()),
136 Value::U32(s) => Value::U32(s + rhs.u32()),
137 Value::U64(s) => Value::U64(s + rhs.u64()),
138 Value::F64(s) => Value::F64(s + rhs.as_f64().unwrap_or_default()),
139 Value::F32(s) => Value::F64(*s as f64 + rhs.as_f64().unwrap_or_default()),
140 _ => {
141 return Value::Null;
142 }
143 };
144 }
145}
146
147impl Add<&&Value> for &Value {
148 type Output = Value;
149 fn op_add(self, rhs: &&Value) -> Self::Output {
150 return match self {
151 Value::String(s) => Value::String(s.to_owned() + rhs.as_str().unwrap_or("")),
152 Value::I32(s) => Value::I32(s + rhs.i32()),
153 Value::I64(s) => Value::I64(s + rhs.i64()),
154 Value::U32(s) => Value::U32(s + rhs.u32()),
155 Value::U64(s) => Value::U64(s + rhs.u64()),
156 Value::F64(s) => Value::F64(s + rhs.as_f64().unwrap_or_default()),
157 Value::F32(s) => Value::F64(*s as f64 + rhs.as_f64().unwrap_or_default()),
158 _ => {
159 return Value::Null;
160 }
161 };
162 }
163}
164
165impl Add<Value> for &Value {
166 type Output = Value;
167 fn op_add(self, rhs: Value) -> Self::Output {
168 return match self {
169 Value::String(s) => Value::String(s.to_owned() + rhs.str()),
170 Value::I32(s) => Value::I32(s + rhs.i32()),
171 Value::I64(s) => Value::I64(s + rhs.i64()),
172 Value::U32(s) => Value::U32(s + rhs.u32()),
173 Value::U64(s) => Value::U64(s + rhs.u64()),
174 Value::F64(s) => Value::F64(s + rhs.as_f64().unwrap_or_default()),
175 Value::F32(s) => Value::F64(*s as f64 + rhs.as_f64().unwrap_or_default()),
176 _ => {
177 return Value::Null;
178 }
179 };
180 }
181}
182
183impl Add<Value> for &str {
185 type Output = String;
186 fn op_add(self, rhs: Value) -> Self::Output {
187 return match rhs {
188 Value::String(s) => self.to_string() + s.as_str(),
189 _ => String::new(),
190 };
191 }
192}
193
194impl Add<&Value> for &str {
195 type Output = String;
196 fn op_add(self, rhs: &Value) -> Self::Output {
197 return match rhs {
198 Value::String(s) => self.to_string() + s.as_str(),
199 _ => String::new(),
200 };
201 }
202}
203
204impl Add<&str> for Value {
205 type Output = String;
206 fn op_add(self, rhs: &str) -> Self::Output {
207 return match self {
208 Value::String(s) => s + rhs,
209 _ => String::new(),
210 };
211 }
212}
213
214impl Add<&str> for &Value {
215 type Output = String;
216 fn op_add(self, rhs: &str) -> Self::Output {
217 return match self {
218 Value::String(s) => s.to_string() + rhs,
219 _ => String::new(),
220 };
221 }
222}
223
224impl Add<&&str> for Value {
225 type Output = String;
226 fn op_add(self, rhs: &&str) -> Self::Output {
227 return match self {
228 Value::String(s) => s + *rhs,
229 _ => String::new(),
230 };
231 }
232}
233
234impl Add<&&str> for &Value {
235 type Output = String;
236 fn op_add(self, rhs: &&str) -> Self::Output {
237 return match self {
238 Value::String(s) => s.to_string() + *rhs,
239 _ => String::new(),
240 };
241 }
242}
243
244impl Add<String> for Value {
245 type Output = String;
246 #[must_use]
247 fn op_add(self, rhs: String) -> Self::Output {
248 return match self {
249 Value::String(s) => s + rhs.as_str(),
250 _ => String::new(),
251 };
252 }
253}
254
255impl Add<String> for &Value {
256 type Output = String;
257 fn op_add(self, rhs: String) -> Self::Output {
258 return match self {
259 Value::String(s) => s.to_string() + rhs.as_str(),
260 _ => String::new(),
261 };
262 }
263}
264
265impl Add<Value> for &String {
267 type Output = String;
268 fn op_add(self, rhs: Value) -> Self::Output {
269 return match rhs {
270 Value::String(s) => self.to_string() + s.as_str(),
271 _ => String::new(),
272 };
273 }
274}
275
276impl Add<&Value> for &String {
277 type Output = String;
278 fn op_add(self, rhs: &Value) -> Self::Output {
279 return match rhs {
280 Value::String(s) => self.to_string() + s.as_str(),
281 _ => String::new(),
282 };
283 }
284}
285
286impl Add<&String> for Value {
287 type Output = String;
288 fn op_add(self, rhs: &String) -> Self::Output {
289 return match self {
290 Value::String(s) => s + rhs.as_str(),
291 _ => String::new(),
292 };
293 }
294}
295
296impl Add<&String> for &Value {
297 type Output = String;
298 fn op_add(self, rhs: &String) -> Self::Output {
299 return match self {
300 Value::String(s) => s.to_string() + rhs.as_str(),
301 _ => String::new(),
302 };
303 }
304}
305
306impl Add<Value> for String {
307 type Output = String;
308 fn op_add(self, rhs: Value) -> Self::Output {
309 return match rhs {
310 Value::String(s) => self + s.as_str(),
311 _ => String::new(),
312 };
313 }
314}
315
316impl Add<&Value> for String {
317 type Output = String;
318 fn op_add(self, rhs: &Value) -> Self::Output {
319 return match rhs {
320 Value::String(s) => self + s.as_str(),
321 _ => String::new(),
322 };
323 }
324}
325
326macro_rules! add_self {
327 ([$($ty:ty)*]) => {
328 $(
329impl Add<$ty> for $ty{
330 type Output = $ty;
331 fn op_add(self, rhs: $ty) -> Self::Output {
332 self+rhs
333 }
334 }
335impl Add<&$ty> for $ty{
336 type Output = $ty;
337 fn op_add(self, rhs: &$ty) -> Self::Output {
338 self+*rhs
339 }
340 }
341impl Add<$ty> for &$ty{
342 type Output = $ty;
343 fn op_add(self, rhs: $ty) -> Self::Output {
344 *self+rhs
345 }
346 }
347impl Add<&$ty> for &$ty{
348 type Output = $ty;
349 fn op_add(self, rhs: &$ty) -> Self::Output {
350 *self+*rhs
351 }
352 }
353 )*
354 };
355}
356add_self!([u8 u16 u32 u64]);
357add_self!([i8 i16 i32 i64 isize]);
358add_self!([f32 f64]);
359
360impl Add<String> for String {
361 type Output = String;
362
363 fn op_add(self, rhs: String) -> Self::Output {
364 self + &rhs
365 }
366}
367
368impl Add<&str> for String {
369 type Output = String;
370
371 fn op_add(self, rhs: &str) -> Self::Output {
372 self + rhs
373 }
374}
375
376impl Add<&&str> for String {
377 type Output = String;
378
379 fn op_add(self, rhs: &&str) -> Self::Output {
380 self + *rhs
381 }
382}
383
384impl Add<String> for &str {
385 type Output = String;
386
387 fn op_add(self, rhs: String) -> Self::Output {
388 self.to_string() + &rhs
389 }
390}
391
392impl Add<&String> for &str {
393 type Output = String;
394
395 fn op_add(self, rhs: &String) -> Self::Output {
396 self.to_string() + rhs.as_str()
397 }
398}
399
400impl Add<&&String> for &str {
401 type Output = String;
402
403 fn op_add(self, rhs: &&String) -> Self::Output {
404 self.to_string() + rhs.as_str()
405 }
406}