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
//
// 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::{libraries::data::{ATTACH, DATA_LIB, DROP, DROP_FROM, EXISTS, FIELD, FROM_BLOB, FROM_ID, ID, INVALIDATE, MOVE, OBJS, TAGNAME, TO_BLOB, VALIDATE}, LibFunc, Param}, runtime::{instruction::Instructions, instructions::Base, Type, Val}};


/// Id.
pub fn data_id() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "id".into(),
        is_async: false,
        docs: r#"# Data.id(ptr: data) -> str
Get the id for this data pointer, which can be used to later construct another reference.
```rust
const func: fn = self.hi;
const id = func.data().id();
assert_eq(Data.from_id(id), func.data());
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, 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(ID.clone());
            Ok(instructions)
        })
    }
}

/// Libname.
pub fn data_libname() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "libname".into(),
        is_async: false,
        docs: r#"# Data.libname(ptr: data) -> str
Get the library name for this data pointer, if applicable.
```rust
const func: fn = self.hi;
assert_eq(func.data().libname(), "Fn");
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, 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(TAGNAME.clone());
            Ok(instructions)
        })
    }
}

/// Exists?
pub fn data_exists() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "exists".into(),
        is_async: false,
        docs: r#"# Data.exists(ptr: data) -> bool
Does this data pointer point to existing data? Will be false if the data has been dropped from the document.
```rust
const func: fn = self.hi;
const ptr = func.data();
drop(func);
assert_not(ptr.exists());
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, 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(EXISTS.clone());
            Ok(instructions)
        })
    }
}

/// Invalidate.
pub fn data_invalidate() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "invalidate".into(),
        is_async: false,
        docs: r#"# Data.invalidate(data: data, symbol: str = 'value') -> bool
Invalidate this data, optionally with the given symbol. Will throw an error if the data doesn't exist. Returns true if the data is newly invalidated with the given symbol.
```rust
const func: fn = self.hi;
const ptr = func.data();
assert(ptr.invalidate('something_happened')); // marks data as invalid
assert(ptr.validate('something_happened'));
assert_not(ptr.validate('something_happened')); // already validated
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, default: None },
            Param { name: "symbol".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(INVALIDATE.clone());
            Ok(instructions)
        })
    }
}

/// Validate.
pub fn data_validate() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "validate".into(),
        is_async: false,
        docs: r#"# Data.validate(data: data, symbol?: str) -> bool
Validate this data, optionally with the given symbol. Will throw an error if the data doesn't exist. Returns true if the data was previously invalidated with the given symbol (or any symbol if null). This will remove the symbol (or all symbols if null) from this data's dirty set (no longer invalid).
```rust
const func: fn = self.hi;
const ptr = func.data();
assert(ptr.invalidate('something_happened'));
assert(ptr.validate('something_happened'));     // marks the data as valid again
assert_not(ptr.validate('something_happened')); // already validated
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, default: None },
            Param { name: "symbol".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(VALIDATE.clone());
            Ok(instructions)
        })
    }
}

/// Objects.
pub fn data_objs() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "objs".into(),
        is_async: false,
        docs: r#"# Data.objs(ptr: data) -> list
List of objects that this data is attached to (will always have at least one).
```rust
const func: fn = self.hi;
assert_eq(func.data().objs().front(), self);
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, 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(OBJS.clone());
            Ok(instructions)
        })
    }
}

/// To blob.
pub fn data_to_blob() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "blob".into(),
        is_async: false,
        docs: r#"# Data.blob(ptr: data) -> blob
Uses bincode serialization to serialize the data (name, attributes, value, etc.), turning it into a blob.
```rust
const func: fn = self.hi;
const bin = func.data().blob(); // entire function as a blob
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, 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(TO_BLOB.clone());
            Ok(instructions)
        })
    }
}

/// From blob.
pub fn data_from_blob() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "load_blob".into(),
        is_async: false,
        docs: r#"# Data.load_blob(bytes: blob, context: obj | str = self) -> data
Uses bincode to deserialize the data blob (name, attributes, value, etc.), adding it to the desired context object.
```rust
const func: fn = self.hi;
const bin = func.data().blob(); // entire function as a blob

const other = new {};
const dref = Data.load_blob(bin, other); // copy of the function is now on "other"
```"#.into(),
        params: vector![
            Param { name: "bytes".into(), param_type: Type::Blob, default: None },
            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(FROM_BLOB.clone());
            Ok(instructions)
        })
    }
}

/// Drop.
pub fn data_drop() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "drop".into(),
        is_async: false,
        docs: r#"# Data.drop(ptr: data) -> bool
Drop this data from the document, returning true if the data existed and was removed.
```rust
const func: fn = self.hi;
assert(func.data().drop());
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, 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(DROP.clone());
            Ok(instructions)
        })
    }
}

/// Drop from.
pub fn data_drop_from() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "drop_from".into(),
        is_async: false,
        docs: r#"# Data.drop_from(ptr: data, obj: obj) -> bool
Drop this data from a specific object. If the given object was the only reference, the data will be dropped completely from the document.
```rust
const func: fn = self.hi;
assert(func.data().drop_from(self));
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, default: None },
            Param { name: "obj".into(), param_type: Type::Void, 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(DROP_FROM.clone());
            Ok(instructions)
        })
    }
}

/// Attach.
pub fn data_attach() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "attach".into(),
        is_async: false,
        docs: r#"# Data.attach(ptr: data, obj: obj) -> bool
Attach this data to an additional object. This data will now be accessible using the same name from the object.
```rust
const func: fn = self.hi;
const other = new {};
assert(func.data().attach(other));
assert_eq(other.hi, func);
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, default: None },
            Param { name: "obj".into(), param_type: Type::Void, 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(ATTACH.clone());
            Ok(instructions)
        })
    }
}

/// Move.
pub fn data_move() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "move".into(),
        is_async: false,
        docs: r#"# Data.move(ptr: data, from: obj, to: obj) -> bool
Combines a drop and attach, removing this data from an object and placing it on another.
```rust
const func: fn = self.hi;
const other = new {};
assert(func.data().move(self, other));
assert_not(self.hi); // func is now on other
```"#.into(),
        params: vector![
            Param { name: "data".into(), param_type: Type::Void, default: None },
            Param { name: "from".into(), param_type: Type::Void, default: None },
            Param { name: "to".into(), param_type: Type::Void, 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(MOVE.clone());
            Ok(instructions)
        })
    }
}

/// From ID.
pub fn data_from_id() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "from_id".into(),
        is_async: false,
        docs: r#"# Data.from_id(id: str) -> data
Create a new data pointer with a string ID.
```rust
const func: fn = self.hi;
const id = func.data().id();
assert_eq(Data.from_id(id), func.data());
```"#.into(),
        params: vector![
            Param { name: "id".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(FROM_ID.clone());
            Ok(instructions)
        })
    }
}

/// From Field Path.
pub fn data_from_field() -> LibFunc {
    LibFunc {
        library: DATA_LIB.clone(),
        name: "field".into(),
        is_async: false,
        docs: r#"# Data.field(path: str) -> data
Create a data pointer to a field, using a path/name from the current object context.
```rust
const ptr = Data.field('myfield'); // self.myfield
assert(ptr.exists());
```"#.into(),
        params: vector![
            Param { name: "path".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(FIELD.clone());
            Ok(instructions)
        })
    }
}