stof 0.9.19

Data that carries its own logic.
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
//
// Copyright 2025 Formata, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use std::sync::Arc;
use imbl::vector;
use crate::{model::{stof_std::{StdIns, BLOBIFY, CALLSTACK, FORMATS, FORMAT_CONTENT_TYPE, GRAPH_ID, HAS_FORMAT, HAS_LIB_FUNC, LIBS, NANO_ID, PARSE, STD_LIB, STRINGIFY}, LibFunc, Param}, runtime::{instruction::Instructions, instructions::Base, Num, NumT, Type, Val}};

#[cfg(feature = "system")]
use crate::model::stof_std::{ENV, SET_ENV, REMOVE_ENV, ENV_MAP};


#[inline(always)]
/// Parse.
pub fn std_parse() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "parse".into(),
        is_async: false,
        docs: r#"# Std.parse(source: str | blob, context: str | obj = self, format: str = "stof", profile: str = "prod") -> bool
Parse data into this document/graph at the given location (default context is the calling object), using the given format (default is Stof). Formats are extensible and replaceable in Stof, so use whichever formats you have loaded (json, stof, images, pdfs, docx, etc.).
```rust
parse("fn hello() -> str { \"hello\" }");
assert_eq(self.hello(), "hello"); // can now call it
```
"#.into(),
        params: vector![
            Param { name: "source".into(), param_type: Type::Void, default: None, },
            Param { name: "context".into(), param_type: Type::Void, default: Some(Arc::new(Base::Literal(Val::Null))), },
            Param { name: "format".into(), param_type: Type::Str, default: Some(Arc::new(Base::Literal(Val::Null))), }, // default is stof
            Param { name: "profile".into(), param_type: Type::Str, default: Some(Arc::new(Base::Literal(Val::Str("prod".into())))), },
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(PARSE.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Stringify.
pub fn std_stringify() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "stringify".into(),
        is_async: false,
        docs: r#"# Std.stringify(format: str = "json", context: obj = null) -> str
Use a loaded format to export a string from the given context (or entire graph/document). The default format is json, and the standard implementation only exports object fields. Export results will vary depending on the format, some support more than others (it is up to the format implementation to decide how it exports data). You can always create your own to use.
```rust
const object = new { x: 3.14km, y: 42m };
assert_eq(stringify("json", object), "{\"x\":3.14,\"y\":42}"); // lossy as json doesn't have a units concept
```
"#.into(),
        params: vector![
            Param { name: "format".into(), param_type: Type::Str, default: Some(Arc::new(Base::Literal(Val::Null))), },
            Param { name: "context".into(), param_type: Type::Void, default: Some(Arc::new(Base::Literal(Val::Null))), },
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(STRINGIFY.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Blobify.
pub fn std_blobify() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "blobify".into(),
        is_async: false,
        docs: r#"# Std.blobify(format: str = "json", context: obj = null) -> blob
Use a loaded format to export a binary blob from the given context (or entire graph/document). The default format is json, and the standard implementation only exports object fields. Export results will vary depending on the format, some support more than others (it is up to the format implementation to decide how it exports data). You can always create your own to use.
```rust
const object = new { x: 3.14km, y: 42m };
const export = blobify("json", object); // json string like "stringify", but as a utf8 blob
assert(export.len() > 0);
```
"#.into(),
        params: vector![
            Param { name: "format".into(), param_type: Type::Str, default: Some(Arc::new(Base::Literal(Val::Null))), },
            Param { name: "context".into(), param_type: Type::Void, default: Some(Arc::new(Base::Literal(Val::Null))), },
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(BLOBIFY.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Has format?
pub fn std_has_format() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "format".into(),
        is_async: false,
        docs: r#"# Std.format(format: str) -> bool
Is the given format loaded/available to use?
```rust
assert(format("json"));
assert_not(format("step"));
```
"#.into(),
        params: vector![
            Param { name: "format".into(), param_type: Type::Str, default: None, },
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(HAS_FORMAT.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Formats.
pub fn std_formats() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "formats".into(),
        is_async: false,
        docs: r#"# Std.formats() -> set
A set of all available formats, available to use with parse, stringify, and blobify.
```rust
const loaded = formats();
assert(loaded.contains("json"));
```
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(FORMATS.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Format content type.
pub fn std_format_content_type() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "format_content_type".into(),
        is_async: false,
        docs: r#"# Std.format_content_type(format: str) -> str
Returns the available format's content type (HTTP header value), or null if the format is not available. All formats are required to give a content type, even if it doesn't apply to that format.
```rust
assert_eq(format_content_type("json"), "application/json");
```
"#.into(),
        params: vector![
            Param { name: "format".into(), param_type: Type::Str, default: None, },
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(FORMAT_CONTENT_TYPE.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Has lib?
pub fn std_has_lib() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "lib".into(),
        is_async: false,
        docs: r#"# Std.lib(lib: str, func?: str) -> bool
Is the given library (and optional function) loaded/available to use?
```rust
assert(lib("Std")); // standard library is loaded
assert_not(lib("Render")); // no "Render" library loaded

assert(lib("Num", "abs")); // Num.abs(..) lib function is available
```
"#.into(),
        params: vector![
            Param { name: "lib".into(), param_type: Type::Str, default: None, },
            Param { name: "func".into(), param_type: Type::Str, default: Some(Arc::new(Base::Literal(Val::Null))), },
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(HAS_LIB_FUNC.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Libs.
pub fn std_libs() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "libs".into(),
        is_async: false,
        docs: r#"# Std.libs() -> set
Set of all available libraries. This will most likely include standard libraries like Std, Fn, Set, List, etc.
```rust
assert(libs().superset({"Std", "Fn", "Num", "Set"}));
```
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(LIBS.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Nanoid
pub fn std_nanoid() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "nanoid".into(),
        is_async: false,
        docs: r#"# Std.nanoid(length: int = 21) -> str
Generate a URL safe random string ID, using the nanoid algorithm with a specified length (default is 21 characters). Probability of a collision is very low, and inversely proportional to ID length.
```rust
assert_neq(nanoid(), nanoid(33));
```
"#.into(),
        params: vector![
            Param { name: "length".into(), param_type: Type::Num(NumT::Int), default: Some(Arc::new(Base::Literal(Val::Num(Num::Int(21))))), },
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(NANO_ID.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Graph ID.
pub fn std_graph_id() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "graph_id".into(),
        is_async: false,
        docs: r#"# Std.graph_id() -> str
Return this graph's unique string ID.
```rust
assert(graph_id().len() > 10);
```
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(GRAPH_ID.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Max value library function.
pub fn std_max() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "max".into(),
        is_async: false,
        docs: r#"# Std.max(..) -> unknown
Return the maximum value of all given arguments. If an argument is a collection, the max value within the collection will be considered only.
```rust
assert_eq(max(1km, 2m, 3mm), 1km);
```
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: true,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(Arc::new(StdIns::Max(arg_count)));
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Min value library function.
pub fn std_min() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "min".into(),
        is_async: false,
        docs: r#"# Std.min(..) -> unknown
Return the minimum value of all given arguments. If an argument is a collection, the min value within the collection will be considered only.
```rust
assert_eq(min(1km, 2m, 3mm), 3mm);
```
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: true,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(Arc::new(StdIns::Min(arg_count)));
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Callstack.
pub fn std_callstack() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "callstack".into(),
        is_async: false,
        docs: r#"# Std.callstack() -> list
Return the current callstack as a list of function pointers (last function is 'this').
```rust
// inside a function call
for (const func in callstack()) {
    pln(func.obj().path(), ".", func.name());
}
```
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(CALLSTACK.clone());
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Trace.
pub fn std_trace() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "trace".into(),
        is_async: false,
        docs: r#"# Std.trace(..) -> void
Trace this location within your code execution. Will print out your arguments plus process debug information and the current instruction stack. If the last argument given is an integer value, that number of executed instruction stack instructions will be shown (very helpful for deeper debugging).
```rust
trace("Getting here"); // will print "Getting here", then output a trace of the current process info and last 10 executed instructions
trace(70); // last 70 executed instructions (most recent on bottom and numbered)
```
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: true,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(Arc::new(StdIns::Trace(arg_count)));
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Peek.
pub fn std_peek() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "peek".into(),
        is_async: false,
        docs: r#"# Std.peek(..) -> void
Trace this location within your code execution. Will print out your arguments plus process debug information and the next instructions on the instruction stack. If the last argument given is an integer value, that number of (future) instructions will be shown (very helpful for deeper debugging).
```rust
peek("Getting here"); // will print "Getting here", then output a trace of the current process info and next 10 instructions to be executed
peek(70); // next 70 instructions
```
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: true,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(Arc::new(StdIns::Peek(arg_count)));
            Ok(instructions)
        })
    }
}

#[inline(always)]
/// Trace stack.
pub fn std_tracestack() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "dbg_tracestack".into(),
        is_async: false,
        docs: r#"# Std.dbg_tracestack() -> void
Print a snapshot of the current stack.
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(Arc::new(StdIns::TraceStack));
            Ok(instructions)
        })
    }
}

#[cfg(feature = "system")]
#[inline(always)]
/// Env var.
pub fn std_env() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "env".into(),
        is_async: false,
        docs: r#"# Std.env(var: str) -> str
Get an environment variable by name. Requires the "system" feature flag.
```rust
const var = env("HOST");
```
"#.into(),
        params: vector![
            Param { name: "var".into(), param_type: Type::Str, default: None }
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(ENV.clone());
            Ok(instructions)
        })
    }
}

#[cfg(feature = "system")]
#[inline(always)]
/// Set env var.
pub fn std_set_env() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "set_env".into(),
        is_async: false,
        docs: r#"# Std.set_env(var: str, value: str) -> void
Set an environment variable by name with a value. Requires the "system" feature flag.
```rust
set_env("HOST", "localhost");
```
"#.into(),
        params: vector![
            Param { name: "var".into(), param_type: Type::Str, default: None },
            Param { name: "value".into(), param_type: Type::Str, default: None }
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(SET_ENV.clone());
            Ok(instructions)
        })
    }
}

#[cfg(feature = "system")]
#[inline(always)]
/// Remove env var.
pub fn std_remove_env() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "remove_env".into(),
        is_async: false,
        docs: r#"# Std.remove_env(var: str) -> void
Remove an environment variable by name. Requires the "system" feature flag.
```rust
remove_env("HOST");
```
"#.into(),
        params: vector![
            Param { name: "var".into(), param_type: Type::Str, default: None }
        ],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(REMOVE_ENV.clone());
            Ok(instructions)
        })
    }
}

#[cfg(feature = "system")]
#[inline(always)]
/// Env vars.
pub fn std_env_vars() -> LibFunc {
    LibFunc {
        library: STD_LIB.clone(),
        name: "env_vars".into(),
        is_async: false,
        docs: r#"# Std.env_vars() -> map
Get a map of the current environment variables (str, str). Requires the "system" feature flag.
```rust
const vars: map = env_vars();
```
"#.into(),
        params: vector![],
        return_type: None,
        unbounded_args: false,
        args_to_symbol_table: false,
        func: Arc::new(|_as_ref, _arg_count, _env, _graph| {
            let mut instructions = Instructions::default();
            instructions.push(ENV_MAP.clone());
            Ok(instructions)
        })
    }
}