@benrogmans/lemma-engine 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Lemma Engine
2
+
3
+ **Logic for man and machine**
4
+
5
+ A declarative logic language engine for expressing rules, facts, and business logic in a way that is both human-readable and machine-executable.
6
+
7
+ ## Features
8
+
9
+ - **Declarative syntax** - Express rules naturally
10
+ - **Type-safe units** - Built-in support for money, mass, length, time, etc.
11
+ - **Rule composition** - Reference other rules and documents
12
+ - **Conditional logic** - Unless clauses for business rules
13
+ - **Date arithmetic** - Native datetime operations
14
+ - **WebAssembly** - Run in the browser
15
+
16
+ ## Quick Start
17
+
18
+ ```rust
19
+ use lemma::{Engine, LemmaResult};
20
+
21
+ fn main() -> LemmaResult<()> {
22
+ let mut engine = Engine::new();
23
+
24
+ engine.add_lemma_code(r#"
25
+ doc pricing
26
+ fact base_price = 100 USD
27
+ fact quantity = 5
28
+ rule total = base_price * quantity
29
+ "#, "pricing.lemma")?;
30
+
31
+ let response = engine.evaluate("pricing", vec![])?;
32
+ println!("{:#?}", response);
33
+
34
+ Ok(())
35
+ }
36
+ ```
37
+
38
+ ## Documentation
39
+
40
+ - **API docs**: Run `cargo doc --open` or visit [docs.rs](https://docs.rs/lemma)
41
+ - **Language guide**: [Lemma language documentation](https://github.com/benrogmans/lemma/tree/main/docs)
42
+ - **Examples**: [Complete examples](https://github.com/benrogmans/lemma/tree/main/docs/examples)
43
+
44
+ ## WebAssembly
45
+
46
+ Compile to WASM for use in browsers (from project root):
47
+
48
+ ```bash
49
+ wasm-pack build lemma --target web --out-dir target/wasm
50
+ ```
51
+
52
+ ## License
53
+
54
+ Apache 2.0
55
+
package/lemma.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export class WasmEngine {
4
+ free(): void;
5
+ [Symbol.dispose](): void;
6
+ constructor();
7
+ addLemmaCode(code: string, source: string): string;
8
+ evaluate(doc_name: string, fact_values_json: string): string;
9
+ listDocuments(): string;
10
+ }
11
+
12
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
13
+
14
+ export interface InitOutput {
15
+ readonly memory: WebAssembly.Memory;
16
+ readonly __wbg_wasmengine_free: (a: number, b: number) => void;
17
+ readonly wasmengine_new: () => number;
18
+ readonly wasmengine_addLemmaCode: (a: number, b: number, c: number, d: number, e: number) => [number, number];
19
+ readonly wasmengine_evaluate: (a: number, b: number, c: number, d: number, e: number) => [number, number];
20
+ readonly wasmengine_listDocuments: (a: number) => [number, number];
21
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
22
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
23
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
24
+ readonly __wbindgen_export_3: WebAssembly.Table;
25
+ readonly __wbindgen_start: () => void;
26
+ }
27
+
28
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
29
+ /**
30
+ * Instantiates the given `module`, which can either be bytes or
31
+ * a precompiled `WebAssembly.Module`.
32
+ *
33
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
34
+ *
35
+ * @returns {InitOutput}
36
+ */
37
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
38
+
39
+ /**
40
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
41
+ * for everything else, calls `WebAssembly.instantiate` directly.
42
+ *
43
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
44
+ *
45
+ * @returns {Promise<InitOutput>}
46
+ */
47
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
package/lemma.js ADDED
@@ -0,0 +1,325 @@
1
+ let wasm;
2
+
3
+ let cachedUint8ArrayMemory0 = null;
4
+
5
+ function getUint8ArrayMemory0() {
6
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
7
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
8
+ }
9
+ return cachedUint8ArrayMemory0;
10
+ }
11
+
12
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
13
+
14
+ cachedTextDecoder.decode();
15
+
16
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
17
+ let numBytesDecoded = 0;
18
+ function decodeText(ptr, len) {
19
+ numBytesDecoded += len;
20
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
21
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
22
+ cachedTextDecoder.decode();
23
+ numBytesDecoded = len;
24
+ }
25
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
26
+ }
27
+
28
+ function getStringFromWasm0(ptr, len) {
29
+ ptr = ptr >>> 0;
30
+ return decodeText(ptr, len);
31
+ }
32
+
33
+ let WASM_VECTOR_LEN = 0;
34
+
35
+ const cachedTextEncoder = new TextEncoder();
36
+
37
+ if (!('encodeInto' in cachedTextEncoder)) {
38
+ cachedTextEncoder.encodeInto = function (arg, view) {
39
+ const buf = cachedTextEncoder.encode(arg);
40
+ view.set(buf);
41
+ return {
42
+ read: arg.length,
43
+ written: buf.length
44
+ };
45
+ }
46
+ }
47
+
48
+ function passStringToWasm0(arg, malloc, realloc) {
49
+
50
+ if (realloc === undefined) {
51
+ const buf = cachedTextEncoder.encode(arg);
52
+ const ptr = malloc(buf.length, 1) >>> 0;
53
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
54
+ WASM_VECTOR_LEN = buf.length;
55
+ return ptr;
56
+ }
57
+
58
+ let len = arg.length;
59
+ let ptr = malloc(len, 1) >>> 0;
60
+
61
+ const mem = getUint8ArrayMemory0();
62
+
63
+ let offset = 0;
64
+
65
+ for (; offset < len; offset++) {
66
+ const code = arg.charCodeAt(offset);
67
+ if (code > 0x7F) break;
68
+ mem[ptr + offset] = code;
69
+ }
70
+
71
+ if (offset !== len) {
72
+ if (offset !== 0) {
73
+ arg = arg.slice(offset);
74
+ }
75
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
76
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
77
+ const ret = cachedTextEncoder.encodeInto(arg, view);
78
+
79
+ offset += ret.written;
80
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
81
+ }
82
+
83
+ WASM_VECTOR_LEN = offset;
84
+ return ptr;
85
+ }
86
+
87
+ let cachedDataViewMemory0 = null;
88
+
89
+ function getDataViewMemory0() {
90
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
91
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
92
+ }
93
+ return cachedDataViewMemory0;
94
+ }
95
+
96
+ const WasmEngineFinalization = (typeof FinalizationRegistry === 'undefined')
97
+ ? { register: () => {}, unregister: () => {} }
98
+ : new FinalizationRegistry(ptr => wasm.__wbg_wasmengine_free(ptr >>> 0, 1));
99
+
100
+ export class WasmEngine {
101
+
102
+ __destroy_into_raw() {
103
+ const ptr = this.__wbg_ptr;
104
+ this.__wbg_ptr = 0;
105
+ WasmEngineFinalization.unregister(this);
106
+ return ptr;
107
+ }
108
+
109
+ free() {
110
+ const ptr = this.__destroy_into_raw();
111
+ wasm.__wbg_wasmengine_free(ptr, 0);
112
+ }
113
+ constructor() {
114
+ const ret = wasm.wasmengine_new();
115
+ this.__wbg_ptr = ret >>> 0;
116
+ WasmEngineFinalization.register(this, this.__wbg_ptr, this);
117
+ return this;
118
+ }
119
+ /**
120
+ * @param {string} code
121
+ * @param {string} source
122
+ * @returns {string}
123
+ */
124
+ addLemmaCode(code, source) {
125
+ let deferred3_0;
126
+ let deferred3_1;
127
+ try {
128
+ const ptr0 = passStringToWasm0(code, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
129
+ const len0 = WASM_VECTOR_LEN;
130
+ const ptr1 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
131
+ const len1 = WASM_VECTOR_LEN;
132
+ const ret = wasm.wasmengine_addLemmaCode(this.__wbg_ptr, ptr0, len0, ptr1, len1);
133
+ deferred3_0 = ret[0];
134
+ deferred3_1 = ret[1];
135
+ return getStringFromWasm0(ret[0], ret[1]);
136
+ } finally {
137
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
138
+ }
139
+ }
140
+ /**
141
+ * @param {string} doc_name
142
+ * @param {string} fact_values_json
143
+ * @returns {string}
144
+ */
145
+ evaluate(doc_name, fact_values_json) {
146
+ let deferred3_0;
147
+ let deferred3_1;
148
+ try {
149
+ const ptr0 = passStringToWasm0(doc_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
150
+ const len0 = WASM_VECTOR_LEN;
151
+ const ptr1 = passStringToWasm0(fact_values_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
152
+ const len1 = WASM_VECTOR_LEN;
153
+ const ret = wasm.wasmengine_evaluate(this.__wbg_ptr, ptr0, len0, ptr1, len1);
154
+ deferred3_0 = ret[0];
155
+ deferred3_1 = ret[1];
156
+ return getStringFromWasm0(ret[0], ret[1]);
157
+ } finally {
158
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
159
+ }
160
+ }
161
+ /**
162
+ * @returns {string}
163
+ */
164
+ listDocuments() {
165
+ let deferred1_0;
166
+ let deferred1_1;
167
+ try {
168
+ const ret = wasm.wasmengine_listDocuments(this.__wbg_ptr);
169
+ deferred1_0 = ret[0];
170
+ deferred1_1 = ret[1];
171
+ return getStringFromWasm0(ret[0], ret[1]);
172
+ } finally {
173
+ wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
174
+ }
175
+ }
176
+ }
177
+ if (Symbol.dispose) WasmEngine.prototype[Symbol.dispose] = WasmEngine.prototype.free;
178
+
179
+ const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']);
180
+
181
+ async function __wbg_load(module, imports) {
182
+ if (typeof Response === 'function' && module instanceof Response) {
183
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
184
+ try {
185
+ return await WebAssembly.instantiateStreaming(module, imports);
186
+
187
+ } catch (e) {
188
+ const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);
189
+
190
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
191
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
192
+
193
+ } else {
194
+ throw e;
195
+ }
196
+ }
197
+ }
198
+
199
+ const bytes = await module.arrayBuffer();
200
+ return await WebAssembly.instantiate(bytes, imports);
201
+
202
+ } else {
203
+ const instance = await WebAssembly.instantiate(module, imports);
204
+
205
+ if (instance instanceof WebAssembly.Instance) {
206
+ return { instance, module };
207
+
208
+ } else {
209
+ return instance;
210
+ }
211
+ }
212
+ }
213
+
214
+ function __wbg_get_imports() {
215
+ const imports = {};
216
+ imports.wbg = {};
217
+ imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function(arg0, arg1) {
218
+ let deferred0_0;
219
+ let deferred0_1;
220
+ try {
221
+ deferred0_0 = arg0;
222
+ deferred0_1 = arg1;
223
+ console.error(getStringFromWasm0(arg0, arg1));
224
+ } finally {
225
+ wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
226
+ }
227
+ };
228
+ imports.wbg.__wbg_new_8a6f238a6ece86ea = function() {
229
+ const ret = new Error();
230
+ return ret;
231
+ };
232
+ imports.wbg.__wbg_stack_0ed75d68575b0f3c = function(arg0, arg1) {
233
+ const ret = arg1.stack;
234
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
235
+ const len1 = WASM_VECTOR_LEN;
236
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
237
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
238
+ };
239
+ imports.wbg.__wbg_wbindgenthrow_451ec1a8469d7eb6 = function(arg0, arg1) {
240
+ throw new Error(getStringFromWasm0(arg0, arg1));
241
+ };
242
+ imports.wbg.__wbindgen_init_externref_table = function() {
243
+ const table = wasm.__wbindgen_export_3;
244
+ const offset = table.grow(4);
245
+ table.set(0, undefined);
246
+ table.set(offset + 0, undefined);
247
+ table.set(offset + 1, null);
248
+ table.set(offset + 2, true);
249
+ table.set(offset + 3, false);
250
+ ;
251
+ };
252
+
253
+ return imports;
254
+ }
255
+
256
+ function __wbg_init_memory(imports, memory) {
257
+
258
+ }
259
+
260
+ function __wbg_finalize_init(instance, module) {
261
+ wasm = instance.exports;
262
+ __wbg_init.__wbindgen_wasm_module = module;
263
+ cachedDataViewMemory0 = null;
264
+ cachedUint8ArrayMemory0 = null;
265
+
266
+
267
+ wasm.__wbindgen_start();
268
+ return wasm;
269
+ }
270
+
271
+ function initSync(module) {
272
+ if (wasm !== undefined) return wasm;
273
+
274
+
275
+ if (typeof module !== 'undefined') {
276
+ if (Object.getPrototypeOf(module) === Object.prototype) {
277
+ ({module} = module)
278
+ } else {
279
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
280
+ }
281
+ }
282
+
283
+ const imports = __wbg_get_imports();
284
+
285
+ __wbg_init_memory(imports);
286
+
287
+ if (!(module instanceof WebAssembly.Module)) {
288
+ module = new WebAssembly.Module(module);
289
+ }
290
+
291
+ const instance = new WebAssembly.Instance(module, imports);
292
+
293
+ return __wbg_finalize_init(instance, module);
294
+ }
295
+
296
+ async function __wbg_init(module_or_path) {
297
+ if (wasm !== undefined) return wasm;
298
+
299
+
300
+ if (typeof module_or_path !== 'undefined') {
301
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
302
+ ({module_or_path} = module_or_path)
303
+ } else {
304
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
305
+ }
306
+ }
307
+
308
+ if (typeof module_or_path === 'undefined') {
309
+ module_or_path = new URL('lemma_bg.wasm', import.meta.url);
310
+ }
311
+ const imports = __wbg_get_imports();
312
+
313
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
314
+ module_or_path = fetch(module_or_path);
315
+ }
316
+
317
+ __wbg_init_memory(imports);
318
+
319
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
320
+
321
+ return __wbg_finalize_init(instance, module);
322
+ }
323
+
324
+ export { initSync };
325
+ export default __wbg_init;
package/lemma_bg.wasm ADDED
Binary file
@@ -0,0 +1,13 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const __wbg_wasmengine_free: (a: number, b: number) => void;
5
+ export const wasmengine_new: () => number;
6
+ export const wasmengine_addLemmaCode: (a: number, b: number, c: number, d: number, e: number) => [number, number];
7
+ export const wasmengine_evaluate: (a: number, b: number, c: number, d: number, e: number) => [number, number];
8
+ export const wasmengine_listDocuments: (a: number) => [number, number];
9
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
10
+ export const __wbindgen_malloc: (a: number, b: number) => number;
11
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
12
+ export const __wbindgen_export_3: WebAssembly.Table;
13
+ export const __wbindgen_start: () => void;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@benrogmans/lemma-engine",
3
+ "type": "module",
4
+ "version": "0.5.1",
5
+ "description": "Lemma: A declarative programming language for business rules. Run in browsers with WebAssembly.",
6
+ "main": "lemma.js",
7
+ "types": "lemma.d.ts",
8
+ "files": [
9
+ "lemma_bg.wasm",
10
+ "lemma.js",
11
+ "lemma.d.ts",
12
+ "lemma_bg.js",
13
+ "lemma_bg.wasm.d.ts"
14
+ ],
15
+ "keywords": [
16
+ "business-rules",
17
+ "logic",
18
+ "declarative",
19
+ "wasm",
20
+ "webassembly"
21
+ ],
22
+ "author": "Ben Rogmans <ben@amrai.nl>",
23
+ "license": "Apache-2.0",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/benrogmans/lemma"
27
+ },
28
+ "homepage": "https://github.com/benrogmans/lemma",
29
+ "bugs": {
30
+ "url": "https://github.com/benrogmans/lemma/issues"
31
+ },
32
+ "scripts": {
33
+ "test": "node test-wasm.js"
34
+ }
35
+ }
36
+