lib-tan-core 0.16.0

The core library
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
use tan::{
    context::Context,
    error::Error,
    expr::{format_value, Expr},
    util::{args::unpack_stringable_arg, module_util::require_module},
};

// #todo string push/append/concat, make similar to array: push one char, append/concat another string, ++ handles both.

// #todo rearrange the functions in some logical order, can be alphabetical.

// #todo (compare str1 str2) ; => Ordering
// #todo (to-lowercase str) or (lowercased str)
// #todo (to-uppercase str) or (uppercased str)

// #idea just use the String constructor: (String "hello " num " guys"), or even (Str "hello " num " guys")
// #todo support: (Str (HTML-Expr (p "This is a nice paragraph!")))
pub fn string_new(args: &[Expr]) -> Result<Expr, Error> {
    let output = args.iter().fold(String::new(), |mut str, x| {
        str.push_str(&format_value(x));
        str
    });

    Ok(Expr::String(output))
}

// #todo better name: `size`?
// #insight `count` is not a good name for length/len, better to be used as verb
pub fn string_get_length(args: &[Expr]) -> Result<Expr, Error> {
    let [this] = args else {
        return Err(Error::invalid_arguments(
            "`chars` requires `this` argument",
            None,
        ));
    };

    let Expr::String(s) = this.unpack() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    Ok(Expr::Int(s.len() as i64))
}

// #todo Implement in Tan?
pub fn string_is_empty(args: &[Expr]) -> Result<Expr, Error> {
    let s = unpack_stringable_arg(args, 0, "s")?;
    Ok(Expr::Bool(s.is_empty()))
}

// #todo trim-start
// #todo trim-end

pub fn string_trim(args: &[Expr]) -> Result<Expr, Error> {
    let [this] = args else {
        return Err(Error::invalid_arguments("requires `this` argument", None));
    };

    let Expr::String(s) = this.unpack() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    Ok(Expr::string(s.trim()))
}

// #todo how to implement a mutating function?
// #todo return (Maybe Char) or (Maybe Rune), handle case of empty string.
/// Removes the last character from the string buffer and returns it.
pub fn string_pop(_args: &[Expr]) -> Result<Expr, Error> {
    // #todo handle the string mutation!
    // #todo handle empty string case!!

    todo!()
}

// #todo enforce range within string length
// #todo rename to `cut`? (as in 'cut a slice')
// #todo relation with range?
// #todo pass range as argument?
// #todo support negative index: -1 => length - 1
// #insight negative index _may_ be problematic if the index is computed and returns negative by mistake.
/// (slice str 2 5)
/// (slice str 2)
/// (slice str 2 -2) ; -2 is length - 2
pub fn string_slice(args: &[Expr]) -> Result<Expr, Error> {
    let [this, start, ..] = args else {
        return Err(Error::invalid_arguments(
            "`slice` requires `this` and start arguments",
            None,
        ));
    };

    let Some(s) = this.as_stringable() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    let Expr::Int(start) = start.unpack() else {
        return Err(Error::invalid_arguments(
            "`start` argument should be an Int",
            this.range(),
        ));
    };

    let start = *start;

    let end = if let Some(end) = args.get(2) {
        let Expr::Int(end) = end.unpack() else {
            return Err(Error::invalid_arguments(
                "`end` argument should be an Int",
                this.range(),
            ));
        };
        *end
    } else {
        s.len() as i64
    };

    let start = start as usize;
    let end = if end < 0 {
        // #todo supporting negative index may hide errors if the index is computed
        // #todo offer a link to only support negative values for constant index
        // If the end argument is negative it indexes from the end of the string.
        (s.len() as i64 + end) as usize
    } else {
        end as usize
    };

    let string_slice = &s[start..end];

    Ok(Expr::string(string_slice))
}

// #todo search `recognize_range`.
// #todo this should reuse the plain string_slice method.
/// Cuts a slice out fo a string, defined by a range.
pub fn string_slice_range(args: &[Expr]) -> Result<Expr, Error> {
    let [this, start, ..] = args else {
        return Err(Error::invalid_arguments(
            "`slice` requires `this` and range arguments",
            None,
        ));
    };

    let Expr::String(s) = this.unpack() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    let Expr::IntRange(start, end, ..) = start.unpack() else {
        return Err(Error::invalid_arguments(
            "`range` argument should be a Range",
            this.range(),
        ));
    };

    // #todo support open-ended ranges.
    // #todo extract the following.

    let start = *start;
    let end = *end;

    let start = start as usize;
    let end = if end < 0 {
        // #todo supporting negative index may hide errors if the index is computed
        // #todo offer a link to only support negative values for constant index
        // If the end argument is negative it indexes from the end of the string.
        (s.len() as i64 + end) as usize
    } else {
        end as usize
    };

    let string_slice = &s[start..end];

    Ok(Expr::string(string_slice))
}

/// Returns a char iterable for the chars in the string.
pub fn string_chars(args: &[Expr]) -> Result<Expr, Error> {
    let [this] = args else {
        return Err(Error::invalid_arguments(
            "`chars` requires `this` argument",
            None,
        ));
    };

    let Some(this) = this.as_string() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    let mut exprs: Vec<Expr> = Vec::new();

    for char in this.chars() {
        exprs.push(Expr::Char(char));
    }

    Ok(Expr::array(exprs))
}

pub fn string_constructor_from_chars(args: &[Expr]) -> Result<Expr, Error> {
    let [chars] = args else {
        return Err(Error::invalid_arguments("requires `chars` argument", None));
    };

    let Some(exprs) = chars.as_array() else {
        return Err(Error::invalid_arguments(
            "`chars` argument should be a (Array Char)",
            chars.range(),
        ));
    };

    // #todo verify Array item type!

    let mut chars: Vec<char> = Vec::new();

    for expr in exprs.iter() {
        if let Some(c) = expr.as_char() {
            chars.push(c);
        }
    }

    let string = String::from_iter(chars);

    Ok(Expr::String(string))
}

// #todo overload for string and char!

pub fn char_to_upper_case(args: &[Expr]) -> Result<Expr, Error> {
    let [this] = args else {
        return Err(Error::invalid_arguments(
            "`to-upper-case` requires `this` argument",
            None,
        ));
    };

    let Expr::Char(this) = this.unpack() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a Char",
            this.range(),
        ));
    };

    // #todo omg...
    let uppercased = this.to_uppercase().next().unwrap();

    Ok(Expr::Char(uppercased))
}

// #insight
// Originally the Swift-like `lowercased` was considered, `to-lower-case` was
// preferred, as it's more consistent, allows for (let lowercased (to-lower-case str)),
// and scales to other cases, e.g. `to-kebab-case`, `to-snake-case`, etc)
pub fn string_to_lower_case(args: &[Expr]) -> Result<Expr, Error> {
    let [this] = args else {
        return Err(Error::invalid_arguments("requires `this` argument", None));
    };

    // #todo consider as_stringable?
    let Some(this) = this.as_string() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    let lowercased = this.to_lowercase();

    Ok(Expr::String(lowercased))
}

// #todo make this a String constructor?
// #todo 'join' and 'format' versions?

// #todo use (to-string ..) instead of format-value
// #todo find another name, this is too common: `fmt`? `stringf`?
// (format-string "hello {} {:.5}" name price)
pub fn string_format(args: &[Expr]) -> Result<Expr, Error> {
    let output = args.iter().fold(String::new(), |mut str, x| {
        str.push_str(&format_value(x));
        str
    });

    Ok(Expr::String(output))
}

// name: split
// type: (Func (String String) String)
// macro annotation: (this: String, separator: String) -> String
// (Func (this separator) ..)
// (Func (#String this #String separator) String)
pub fn string_split(args: &[Expr]) -> Result<Expr, Error> {
    // #todo Consider different order of arguments.
    // #todo Don't use `this` name.
    let string = unpack_stringable_arg(args, 0, "this")?;
    let separator = unpack_stringable_arg(args, 1, "separator")?;

    // #todo should return iterator

    let parts: Vec<Expr> = string.split(separator).map(Expr::string).collect();

    Ok(Expr::array(parts))
}

// #todo string_is_matching

pub fn string_contains(args: &[Expr]) -> Result<Expr, Error> {
    let [this, string] = args else {
        return Err(Error::invalid_arguments(
            "`contains` requires `this` and `string` arguments",
            None,
        ));
    };

    let Some(this) = this.as_string() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    let Some(string) = string.as_string() else {
        return Err(Error::invalid_arguments(
            "`string` argument should be a String",
            string.range(),
        ));
    };

    Ok(Expr::Bool(this.contains(string)))
}

pub fn string_starts_with(args: &[Expr]) -> Result<Expr, Error> {
    let [this, prefix] = args else {
        return Err(Error::invalid_arguments(
            "`starts-with` requires `this` and `prefix` arguments",
            None,
        ));
    };

    let Some(this) = this.as_string() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    let Some(prefix) = prefix.as_string() else {
        return Err(Error::invalid_arguments(
            "`prefix` argument should be a String",
            prefix.range(),
        ));
    };

    Ok(Expr::Bool(this.starts_with(prefix)))
}

pub fn string_ends_with(args: &[Expr]) -> Result<Expr, Error> {
    // #todo consider `suffix` instead of `postfix`.
    let [this, postfix] = args else {
        return Err(Error::invalid_arguments(
            "`ends-with` requires `this` and `postfix` arguments",
            None,
        ));
    };

    let Some(this) = this.as_string() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    let Some(postfix) = postfix.as_string() else {
        return Err(Error::invalid_arguments(
            "`postfix` argument should be a String",
            postfix.range(),
        ));
    };

    Ok(Expr::Bool(this.ends_with(postfix)))
}

// #todo implement `replace-once`.

// #todo support replace with array of rules or just use array spread.
// #todo consider a separate function called `replace*` to support multiple arguments?
// #todo or better consider compiler-optimization statically if there is only one replacement.
// #todo IDE hint if a compiler-optimization is performed.
// #todo could allow for multiple replacements (i.e. pairs of rules)
// #todo different name? e.g. rewrite?
pub fn string_replace(args: &[Expr]) -> Result<Expr, Error> {
    // #insight _from, _to are only used to verify that there is at least one
    let [this, _from, _to, ..] = args else {
        return Err(Error::invalid_arguments(
            "`replace` requires `this`, `from`, and `to` arguments",
            None,
        ));
    };

    let Some(this) = this.as_string() else {
        return Err(Error::invalid_arguments(
            "`this` argument should be a String",
            this.range(),
        ));
    };

    let mut output: String = this.to_string();

    let mut i = 1;
    while i < args.len() {
        let from = &args[i];
        let Some(from) = from.as_string() else {
            return Err(Error::invalid_arguments(
                "`from` argument should be a String",
                from.range(),
            ));
        };

        let to = &args[i + 1];
        let Some(to) = to.as_string() else {
            return Err(Error::invalid_arguments(
                "`to` argument should be a String",
                to.range(),
            ));
        };

        output = output.replace(from, to);

        i += 2;
    }

    Ok(Expr::String(output))

    // let Some(from) = from.as_string() else {
    //     return Err(Error::invalid_arguments(
    //         "`from` argument should be a String",
    //         from.range(),
    //     ));
    // };

    // let Some(to) = to.as_string() else {
    //     return Err(Error::invalid_arguments(
    //         "`to` argument should be a String",
    //         to.range(),
    //     ));
    // };

    // Ok(Expr::String(this.replace(from, to)))
}

// #todo move to cmp.rs?
// #todo should this get renamed to `stringable_compare`?
// #todo should be associated with `Ordering` and `Comparable`.
pub fn string_compare(args: &[Expr]) -> Result<Expr, Error> {
    // #todo support multiple arguments.
    let [a, b] = args else {
        return Err(Error::invalid_arguments(
            "requires at least two arguments",
            None,
        ));
    };

    // #todo is this check required if we perform type inference before calling
    // this function?

    let Some(a) = a.as_stringable() else {
        return Err(Error::invalid_arguments(
            &format!("{a} is not a String"),
            a.range(),
        ));
    };

    let Some(b) = b.as_stringable() else {
        return Err(Error::invalid_arguments(
            &format!("{b} is not a String"),
            b.range(),
        ));
    };

    // #todo temp hack until Tan has enums?
    let ordering = match a.cmp(b) {
        std::cmp::Ordering::Less => -1,
        std::cmp::Ordering::Equal => 0,
        std::cmp::Ordering::Greater => 1,
    };

    Ok(Expr::Int(ordering))
}

pub fn setup_lib_string(context: &mut Context) {
    let module = require_module("prelude", context);

    module.insert_invocable("String", Expr::foreign_func(&string_new));

    // #insight it's OK if it's not short, it's not often used.
    // #todo consider a shorter name, e.g. `Str/from`, `Str/from-chars`
    // #todo implement as String constructor method/override?, e.g. `(Str [(Char "h")(Char "i")])`
    module.insert_invocable(
        "String/from-chars",
        Expr::foreign_func(&string_constructor_from_chars),
    );
    // env.insert("String$$Array", Expr::foreign_func(&string_constructor_from_chars)));

    module.insert_invocable("chars", Expr::foreign_func(&string_chars));
    module.insert_invocable("chars$$String", Expr::foreign_func(&string_chars));

    module.insert_invocable("is-empty?", Expr::foreign_func(&string_is_empty));
    module.insert_invocable("is-empty?$$String", Expr::foreign_func(&string_is_empty));

    // #todo rename to `to-uppercase`, more consistent?
    module.insert_invocable("to-upper-case", Expr::foreign_func(&char_to_upper_case));
    module.insert_invocable(
        "to-upper-case$$Char",
        Expr::foreign_func(&char_to_upper_case),
    );

    module.insert_invocable("to-lower-case", Expr::foreign_func(&string_to_lower_case));
    module.insert_invocable(
        "to-lower-case$$String",
        Expr::foreign_func(&string_to_lower_case),
    );

    module.insert_invocable("format-string", Expr::foreign_func(&string_format));

    module.insert_invocable("split", Expr::foreign_func(&string_split));

    module.insert_invocable("replace", Expr::foreign_func(&string_replace));

    // #todo slice is to general works both as noun and verb, try to find an explicit verb? e.g. `cut` or `carve`
    // #todo alternatively use something like `get-slice` or `cut-slice` or `carve-slice`.
    module.insert_invocable("slice", Expr::foreign_func(&string_slice));
    module.insert_invocable("slice$$String$$Int$$Int", Expr::foreign_func(&string_slice));
    module.insert_invocable(
        "slice$$String$$(Range Int)",
        Expr::foreign_func(&string_slice_range),
    );

    // #todo find a better name, `size`?
    // #insight `count` is _not_ a good name, reserve it for verb/action.
    // #todo What about count-of?
    module.insert_invocable("get-length", Expr::foreign_func(&string_get_length));
    module.insert_invocable("get-length$$String", Expr::foreign_func(&string_get_length));

    // #todo write tan unit test
    module.insert_invocable("trim", Expr::foreign_func(&string_trim));

    // module.insert_invocable("contains?", Expr::foreign_func(&string_contains)));
    module.insert_invocable(
        "contains?$$String$$String",
        Expr::foreign_func(&string_contains),
    );

    module.insert_invocable("starts-with?", Expr::foreign_func(&string_starts_with));

    /*
    (if (ends-with filename ".png")
    (if (ends-with? filename ".png")
        (handle-image filename)
        (handle filename)
    )
     */
    // #todo: consider 'ends-with' without '?'.
    module.insert_invocable("ends-with?", Expr::foreign_func(&string_ends_with));
}