linkrs-core 0.1.0

Core data structures, types, and errors for Linkrs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Value Calculation Module
//!
//! This module provides methods for arithmetic, logical, and bitwise operations on values.
use crate::value::Value;

impl Value {
    /// Addition operation
    pub fn add(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            // Same type operations
            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a.wrapping_add(*b))),
            (Int(a), Int(b)) => Ok(Int(a.wrapping_add(*b))),
            (BigInt(a), BigInt(b)) => Ok(BigInt(a.wrapping_add(*b))),
            (Float(a), Float(b)) => Ok(Float(a + b)),
            (Double(a), Double(b)) => Ok(Double(a + b)),

            // Cross-type operations: promote to larger type
            (SmallInt(a), Int(b)) => Ok(Int(*a as i32 + b)),
            (Int(a), SmallInt(b)) => Ok(Int(a + *b as i32)),
            (SmallInt(a), BigInt(b)) => Ok(BigInt(*a as i64 + b)),
            (BigInt(a), SmallInt(b)) => Ok(BigInt(a + *b as i64)),
            (Int(a), BigInt(b)) => Ok(BigInt(*a as i64 + b)),
            (BigInt(a), Int(b)) => Ok(BigInt(a + *b as i64)),

            // Integer to float promotion
            (SmallInt(a), Float(b)) => Ok(Float(*a as f32 + b)),
            (Float(a), SmallInt(b)) => Ok(Float(a + *b as f32)),
            (Int(a), Float(b)) => Ok(Float(*a as f32 + b)),
            (Float(a), Int(b)) => Ok(Float(a + *b as f32)),
            (BigInt(a), Float(b)) => Ok(Float(*a as f32 + b)),
            (Float(a), BigInt(b)) => Ok(Float(a + *b as f32)),

            (SmallInt(a), Double(b)) => Ok(Double(*a as f64 + b)),
            (Double(a), SmallInt(b)) => Ok(Double(a + *b as f64)),
            (Int(a), Double(b)) => Ok(Double(*a as f64 + b)),
            (Double(a), Int(b)) => Ok(Double(a + *b as f64)),
            (BigInt(a), Double(b)) => Ok(Double(*a as f64 + b)),
            (Double(a), BigInt(b)) => Ok(Double(a + *b as f64)),

            (Float(a), Double(b)) => Ok(Double(*a as f64 + b)),
            (Double(a), Float(b)) => Ok(Double(a + *b as f64)),

            // String concatenation
            (String(a), String(b)) => Ok(String(format!("{}{}", a, b).into())),
            (String(a), FixedString(b)) => Ok(String(format!("{}{}", a, b).into())),
            (FixedString(a), String(b)) => Ok(String(format!("{}{}", a, b).into())),
            (FixedString(a), FixedString(b)) => Ok(String(format!("{}{}", a, b).into())),
            // High-precision decimal addition
            (Decimal128(a), Decimal128(b)) => a.add(b).map(Decimal128),
            _ => Err("Cannot perform addition on these value types".to_string()),
        }
    }

    /// Subtraction operation
    pub fn sub(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a.wrapping_sub(*b))),
            (Int(a), Int(b)) => Ok(Int(a.wrapping_sub(*b))),
            (BigInt(a), BigInt(b)) => Ok(BigInt(a.wrapping_sub(*b))),
            (Float(a), Float(b)) => Ok(Float(a - b)),
            (Double(a), Double(b)) => Ok(Double(a - b)),

            // Cross-type operations
            (SmallInt(a), Int(b)) => Ok(Int(*a as i32 - b)),
            (Int(a), SmallInt(b)) => Ok(Int(a - *b as i32)),
            (SmallInt(a), BigInt(b)) => Ok(BigInt(*a as i64 - b)),
            (BigInt(a), SmallInt(b)) => Ok(BigInt(a - *b as i64)),
            (Int(a), BigInt(b)) => Ok(BigInt(*a as i64 - b)),
            (BigInt(a), Int(b)) => Ok(BigInt(a - *b as i64)),

            // Integer to float
            (SmallInt(a), Float(b)) => Ok(Float(*a as f32 - b)),
            (Float(a), SmallInt(b)) => Ok(Float(a - *b as f32)),
            (Int(a), Float(b)) => Ok(Float(*a as f32 - b)),
            (Float(a), Int(b)) => Ok(Float(a - *b as f32)),
            (BigInt(a), Float(b)) => Ok(Float(*a as f32 - b)),
            (Float(a), BigInt(b)) => Ok(Float(a - *b as f32)),

            (SmallInt(a), Double(b)) => Ok(Double(*a as f64 - b)),
            (Double(a), SmallInt(b)) => Ok(Double(a - *b as f64)),
            (Int(a), Double(b)) => Ok(Double(*a as f64 - b)),
            (Double(a), Int(b)) => Ok(Double(a - *b as f64)),
            (BigInt(a), Double(b)) => Ok(Double(*a as f64 - b)),
            (Double(a), BigInt(b)) => Ok(Double(a - *b as f64)),

            (Float(a), Double(b)) => Ok(Double(*a as f64 - b)),
            (Double(a), Float(b)) => Ok(Double(a - *b as f64)),

            _ => Err("Cannot perform subtraction on these value types".to_string()),
        }
    }

    /// Multiplication operation
    pub fn mul(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a.wrapping_mul(*b))),
            (Int(a), Int(b)) => Ok(Int(a.wrapping_mul(*b))),
            (BigInt(a), BigInt(b)) => Ok(BigInt(a.wrapping_mul(*b))),
            (Float(a), Float(b)) => Ok(Float(a * b)),
            (Double(a), Double(b)) => Ok(Double(a * b)),

            // Cross-type operations
            (SmallInt(a), Int(b)) => Ok(Int(*a as i32 * b)),
            (Int(a), SmallInt(b)) => Ok(Int(a * *b as i32)),
            (SmallInt(a), BigInt(b)) => Ok(BigInt(*a as i64 * b)),
            (BigInt(a), SmallInt(b)) => Ok(BigInt(a * *b as i64)),
            (Int(a), BigInt(b)) => Ok(BigInt(*a as i64 * b)),
            (BigInt(a), Int(b)) => Ok(BigInt(a * *b as i64)),

            // Integer to float
            (SmallInt(a), Float(b)) => Ok(Float(*a as f32 * b)),
            (Float(a), SmallInt(b)) => Ok(Float(a * *b as f32)),
            (Int(a), Float(b)) => Ok(Float(*a as f32 * b)),
            (Float(a), Int(b)) => Ok(Float(a * *b as f32)),
            (BigInt(a), Float(b)) => Ok(Float(*a as f32 * b)),
            (Float(a), BigInt(b)) => Ok(Float(a * *b as f32)),

            (SmallInt(a), Double(b)) => Ok(Double(*a as f64 * b)),
            (Double(a), SmallInt(b)) => Ok(Double(a * *b as f64)),
            (Int(a), Double(b)) => Ok(Double(*a as f64 * b)),
            (Double(a), Int(b)) => Ok(Double(a * *b as f64)),
            (BigInt(a), Double(b)) => Ok(Double(*a as f64 * b)),
            (Double(a), BigInt(b)) => Ok(Double(a * *b as f64)),

            (Float(a), Double(b)) => Ok(Double(*a as f64 * b)),
            (Double(a), Float(b)) => Ok(Double(a * *b as f64)),

            _ => Err("Cannot perform multiplication on these value types".to_string()),
        }
    }

    /// Division operation
    pub fn div(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(SmallInt(a / b))
                }
            }
            (Int(a), Int(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Int(a / b))
                }
            }
            (BigInt(a), BigInt(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(BigInt(a / b))
                }
            }
            (Float(a), Float(b)) => {
                if *b == 0.0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Float(a / b))
                }
            }
            (Double(a), Double(b)) => {
                if *b == 0.0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Double(a / b))
                }
            }

            // Cross-type: promote to larger type
            (SmallInt(a), Int(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Int(*a as i32 / b))
                }
            }
            (Int(a), SmallInt(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Int(a / *b as i32))
                }
            }
            (Int(a), BigInt(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(BigInt(*a as i64 / b))
                }
            }
            (BigInt(a), Int(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(BigInt(a / *b as i64))
                }
            }

            // Integer to double for division
            (SmallInt(a), Double(b)) => {
                if *b == 0.0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Double(*a as f64 / b))
                }
            }
            (Double(a), SmallInt(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Double(a / *b as f64))
                }
            }
            (Int(a), Double(b)) => {
                if *b == 0.0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Double(*a as f64 / b))
                }
            }
            (Double(a), Int(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Double(a / *b as f64))
                }
            }
            (BigInt(a), Double(b)) => {
                if *b == 0.0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Double(*a as f64 / b))
                }
            }
            (Double(a), BigInt(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Double(a / *b as f64))
                }
            }

            _ => Err("Cannot perform division on these value types".to_string()),
        }
    }

    /// Modular operation
    pub fn rem(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(SmallInt(a % b))
                }
            }
            (Int(a), Int(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(Int(a % b))
                }
            }
            (BigInt(a), BigInt(b)) => {
                if *b == 0 {
                    Err("Division by zero".to_string())
                } else {
                    Ok(BigInt(a % b))
                }
            }
            _ => Err("Modulo operation is only supported for integer types".to_string()),
        }
    }

    /// Power operation
    pub fn pow(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => {
                if *b < 0 {
                    Err("Negative exponent not supported for integer power operation".to_string())
                } else {
                    Ok(SmallInt(a.pow(*b as u32)))
                }
            }
            (Int(a), Int(b)) => {
                if *b < 0 {
                    Err("Negative exponent not supported for integer power operation".to_string())
                } else {
                    Ok(Int(a.pow(*b as u32)))
                }
            }
            (BigInt(a), BigInt(b)) => {
                if *b < 0 {
                    Err("Negative exponent not supported for integer power operation".to_string())
                } else {
                    Ok(BigInt(a.pow(*b as u32)))
                }
            }
            (Float(a), Float(b)) => Ok(Float(a.powf(*b))),
            (Double(a), Double(b)) => Ok(Double(a.powf(*b))),
            (SmallInt(a), Double(b)) => Ok(Double((*a as f64).powf(*b))),
            (Double(a), SmallInt(b)) => Ok(Double(a.powi(*b as i32))),
            (Int(a), Double(b)) => Ok(Double((*a as f64).powf(*b))),
            (Double(a), Int(b)) => Ok(Double(a.powi(*b))),
            (BigInt(a), Double(b)) => Ok(Double((*a as f64).powf(*b))),
            (Double(a), BigInt(b)) => Ok(Double(a.powi(*b as i32))),
            _ => Err("Cannot perform power operation on these value types".to_string()),
        }
    }

    /// Negation operation
    pub fn neg(&self) -> Result<Value, String> {
        use Value::*;
        match self {
            SmallInt(a) => Ok(SmallInt(-a)),
            Int(a) => Ok(Int(-a)),
            BigInt(a) => Ok(BigInt(-a)),
            Float(a) => Ok(Float(-a)),
            Double(a) => Ok(Double(-a)),
            _ => Err("Negation is only supported for numeric types".to_string()),
        }
    }

    /// Logic and Operations
    pub fn and(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (Bool(a), Bool(b)) => Ok(Bool(*a && *b)),
            _ => Err("Logical AND is only supported for boolean types".to_string()),
        }
    }

    /// Logical OR operation
    pub fn or(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (Bool(a), Bool(b)) => Ok(Bool(*a || *b)),
            _ => Err("Logical OR is only supported for boolean types".to_string()),
        }
    }

    /// Logical NOT operation
    pub fn not(&self) -> Result<Value, String> {
        use Value::*;
        match self {
            Bool(a) => Ok(Bool(!a)),
            _ => Err("Logical NOT is only supported for boolean types".to_string()),
        }
    }

    /// Bitwise AND operation
    pub fn bit_and(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a & b)),
            (Int(a), Int(b)) => Ok(Int(a & b)),
            (BigInt(a), BigInt(b)) => Ok(BigInt(a & b)),
            _ => Err("Bitwise AND is only supported for integer types".to_string()),
        }
    }

    /// Bitwise OR operation
    pub fn bit_or(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a | b)),
            (Int(a), Int(b)) => Ok(Int(a | b)),
            (BigInt(a), BigInt(b)) => Ok(BigInt(a | b)),
            _ => Err("Bitwise OR is only supported for integer types".to_string()),
        }
    }

    /// Bitwise XOR operation
    pub fn bit_xor(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a ^ b)),
            (Int(a), Int(b)) => Ok(Int(a ^ b)),
            (BigInt(a), BigInt(b)) => Ok(BigInt(a ^ b)),
            _ => Err("Bitwise XOR is only supported for integer types".to_string()),
        }
    }

    /// Left shift operation
    pub fn bit_shl(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => {
                if *b < 0 || *b >= 16 {
                    Err("Shift count out of range".to_string())
                } else {
                    Ok(SmallInt(a << *b as u32))
                }
            }
            (Int(a), Int(b)) => {
                if *b < 0 || *b >= 32 {
                    Err("Shift count out of range".to_string())
                } else {
                    Ok(Int(a << *b as u32))
                }
            }
            (BigInt(a), BigInt(b)) => {
                if *b < 0 || *b >= 64 {
                    Err("Shift count out of range".to_string())
                } else {
                    Ok(BigInt(a << *b as u32))
                }
            }
            _ => Err("Bitwise left shift is only supported for integer types".to_string()),
        }
    }

    /// Right-shift operation
    pub fn bit_shr(&self, other: &Value) -> Result<Value, String> {
        use Value::*;
        match (self, other) {
            (SmallInt(a), SmallInt(b)) => {
                if *b < 0 || *b >= 16 {
                    Err("Shift count out of range".to_string())
                } else {
                    Ok(SmallInt(a >> *b as u32))
                }
            }
            (Int(a), Int(b)) => {
                if *b < 0 || *b >= 32 {
                    Err("Shift count out of range".to_string())
                } else {
                    Ok(Int(a >> *b as u32))
                }
            }
            (BigInt(a), BigInt(b)) => {
                if *b < 0 || *b >= 64 {
                    Err("Shift count out of range".to_string())
                } else {
                    Ok(BigInt(a >> *b as u32))
                }
            }
            _ => Err("Bitwise right shift is only supported for integer types".to_string()),
        }
    }

    /// Bitwise NOT operation
    pub fn bit_not(&self) -> Result<Value, String> {
        use Value::*;
        match self {
            SmallInt(a) => Ok(SmallInt(!a)),
            Int(a) => Ok(Int(!a)),
            BigInt(a) => Ok(BigInt(!a)),
            _ => Err("Bitwise NOT is only supported for integer types".to_string()),
        }
    }

    /// Absolute value operation
    pub fn abs(&self) -> Result<Value, String> {
        use Value::*;
        match self {
            SmallInt(a) => Ok(SmallInt(a.abs())),
            Int(a) => Ok(Int(a.abs())),
            BigInt(a) => Ok(BigInt(a.abs())),
            Float(a) => Ok(Float(a.abs())),
            Double(a) => Ok(Double(a.abs())),
            _ => Err("Absolute value is only supported for numeric types".to_string()),
        }
    }

    /// Get the length of a value (for strings, lists, maps, sets)
    pub fn len(&self) -> Result<Value, String> {
        use Value::*;
        match self {
            String(s) => Ok(Int(s.len() as i32)),
            FixedString(data) => Ok(Int(data.len() as i32)),
            List(l) => Ok(Int(l.values.len() as i32)),
            Map(m) => Ok(Int(m.len() as i32)),
            Set(s) => Ok(Int(s.len() as i32)),
            Blob(b) => Ok(Int(b.len() as i32)),
            _ => Err(
                "Length operation is only supported for string, blob, list, map, or set types"
                    .to_string(),
            ),
        }
    }
}