lemma-engine 0.8.13

A language that means business.
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
use crate::error::ErrorKind;
use crate::parsing::ast::DateTimeValue;
use crate::parsing::source::Source;
use crate::serialization::data_values_from_map;
use crate::{Engine, Error, SourceType};
use serde::Serialize;
use std::collections::HashMap;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name = Engine)]
pub struct WasmEngine {
    engine: Engine,
}

#[wasm_bindgen]
impl WasmEngine {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        console_error_panic_hook::set_once();
        WasmEngine {
            engine: Engine::new(),
        }
    }

    /// Load Lemma source. Throws with an array of serialized errors
    /// (same shape as `EngineError` in `engine/packages/npm/lemma.d.ts`).
    #[wasm_bindgen(js_name = load)]
    pub fn load_wasm(&mut self, code: &str, attribute: &str) -> Result<(), JsValue> {
        let source = if attribute.trim().is_empty() {
            SourceType::Volatile
        } else {
            SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from(attribute)))
        };
        self.engine.load(code, source).map_err(|load_err| {
            let errors: Vec<JsError> = load_err.errors.iter().map(JsError::from).collect();
            errors
                .serialize(&js_error_serializer())
                .expect("BUG: serialize JsError array")
        })
    }

    /// Load multiple Lemma sources in one planning pass (same as [`Engine::load_batch`]).
    ///
    /// `sources` is a plain object mapping path labels to source text. Labels become
    /// [`SourceType::Path`]; use `""` as a key for [`SourceType::Volatile`].
    ///
    /// `dependency`: when non-empty after trim, sources are tagged as that dependency id.
    ///
    /// Throws with an array of `JsError` on failure.
    #[wasm_bindgen(js_name = load_batch)]
    pub fn load_batch_wasm(
        &mut self,
        sources: JsValue,
        dependency: Option<String>,
    ) -> Result<(), JsValue> {
        let map: HashMap<String, String> = if sources.is_undefined() || sources.is_null() {
            HashMap::new()
        } else {
            serde_wasm_bindgen::from_value(sources).map_err(|e| {
                let err = Error::request(
                    format!(
                        "load_batch: sources must be a plain object with string keys and string values: {e}"
                    ),
                    None::<String>,
                );
                let errors = vec![JsError::from(&err)];
                errors
                    .serialize(&js_error_serializer())
                    .expect("BUG: serialize JsError array")
            })?
        };
        let mut batch: HashMap<SourceType, String> = HashMap::with_capacity(map.len());
        for (key, code) in map {
            let source = if key.trim().is_empty() {
                SourceType::Volatile
            } else {
                SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from(key)))
            };
            batch.insert(source, code);
        }
        let owned_dep = dependency.and_then(|s| {
            let t = s.trim();
            if t.is_empty() {
                None
            } else {
                Some(t.to_string())
            }
        });
        self.engine
            .load_batch(batch, owned_dep.as_deref())
            .map_err(|load_err| {
                let errors: Vec<JsError> = load_err.errors.iter().map(JsError::from).collect();
                errors
                    .serialize(&js_error_serializer())
                    .expect("BUG: serialize JsError array")
            })
    }

    /// Download Lemma source for a registry identifier via [`crate::registry::LemmaBase`]. Returns `{ source, id }`.
    /// Does not load this [`WasmEngine`]; call [`Self::load_batch`], etc., yourself.
    #[wasm_bindgen(js_name = fetch)]
    pub fn fetch_wasm(&self, name: &str) -> js_sys::Promise {
        let _ = self;
        match crate::spec_set_id::parse_spec_set_id(name) {
            Err(e) => {
                let js_err_array = {
                    let errors = vec![JsError::from(&e)];
                    errors
                        .serialize(&js_error_serializer())
                        .expect("BUG: serialize JsError array")
                };
                wasm_bindgen_futures::future_to_promise(async move { Err(js_err_array) })
            }
            Ok(normalized) => {
                #[cfg(not(feature = "registry"))]
                {
                    let _: String = normalized;
                    let err = Error::request(
                        "fetch requires the lemma-engine crate to be built with the `registry` feature",
                        None::<String>,
                    );
                    let js_err_array = {
                        let errors = vec![JsError::from(&err)];
                        errors
                            .serialize(&js_error_serializer())
                            .expect("BUG: serialize JsError array")
                    };
                    wasm_bindgen_futures::future_to_promise(async move { Err(js_err_array) })
                }
                #[cfg(feature = "registry")]
                {
                    wasm_registry_fetch_only_promise(normalized)
                }
            }
        }
    }

    /// Evaluate spec. Returns [`crate::evaluation::Response`] as a JS object. Throws on planning/runtime error.
    ///
    /// `repository`: repository qualifier (`@org/pkg`), or `null`/empty for workspace (same as
    /// [`Engine::run`] `repo: None`).
    #[wasm_bindgen(js_name = run)]
    pub fn run(
        &self,
        repository: Option<String>,
        spec: &str,
        rule_names: JsValue,
        data_values: JsValue,
        effective: Option<String>,
    ) -> Result<JsValue, JsValue> {
        let effective_dt = effective
            .as_ref()
            .filter(|s| !s.trim().is_empty())
            .and_then(|s| s.parse::<DateTimeValue>().ok())
            .unwrap_or_else(DateTimeValue::now);

        let rule_names = parse_rule_names(&rule_names).map_err(js_err)?;
        let data = parse_data_values(&data_values).map_err(js_err)?;

        let repo = repository
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty());

        let mut response = self
            .engine
            .run(repo, spec, Some(&effective_dt), data, false)
            .map_err(|e| error_to_js(&e))?;

        if !rule_names.is_empty() {
            response.filter_rules(&rule_names);
        }

        serialize_engine_json(&response)
    }

    /// Same data as [`Engine::list`]: grouped [`ResolvedRepository`] JSON without planning.
    #[wasm_bindgen(js_name = list)]
    pub fn list_wasm(&self) -> Result<JsValue, JsValue> {
        serialize_engine_json(&self.engine.list())
    }

    /// Planning schema for the spec ([`crate::planning::execution_plan::SpecSchema`]). Throws on error.
    ///
    /// `repository`: qualifier string or `null`/empty for workspace ([`Engine::schema`]).
    #[wasm_bindgen(js_name = schema)]
    pub fn schema(
        &self,
        repository: Option<String>,
        spec: &str,
        effective: Option<String>,
    ) -> Result<JsValue, JsValue> {
        let effective_dt = effective
            .as_ref()
            .filter(|s| !s.trim().is_empty())
            .and_then(|s| s.parse::<DateTimeValue>().ok())
            .unwrap_or_else(DateTimeValue::now);

        let repo = repository
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty());

        let plan = self
            .engine
            .get_plan(repo, spec, Some(&effective_dt))
            .map_err(|e| error_to_js(&e))?;
        let schema = plan.schema();

        serialize_engine_json(&schema)
    }

    #[wasm_bindgen(js_name = invert)]
    pub fn invert(
        &self,
        _spec_name: &str,
        _rule_name: &str,
        _target_json: &str,
        _provided_values_json: &str,
    ) -> Result<JsValue, JsValue> {
        Err(js_err("Inversion not implemented"))
    }

    /// Returns formatted source string on success; throws with error message on failure.
    #[wasm_bindgen(js_name = format)]
    pub fn format_wasm(&self, code: &str, attribute: Option<String>) -> Result<JsValue, JsValue> {
        let attr = match attribute
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
        {
            Some(s) => s,
            None => "inline source (no path)",
        };
        match crate::format_source(
            code,
            crate::parsing::source::SourceType::Path(std::sync::Arc::new(
                std::path::PathBuf::from(attr),
            )),
        ) {
            Ok(formatted) => Ok(JsValue::from_str(&formatted)),
            Err(e) => Err(error_to_js(&e)),
        }
    }

    /// Loaded repositories (workspace and dependencies): `{ name, dependency }`.
    #[wasm_bindgen(js_name = repositories)]
    pub fn repositories(&self) -> Result<JsValue, JsValue> {
        let rows: Vec<serde_json::Value> = self
            .engine
            .list()
            .iter()
            .map(|r| {
                serde_json::json!({
                    "name": r.repository.name,
                    "dependency": r.repository.dependency,
                })
            })
            .collect();
        serialize_engine_json(&rows)
    }
}

#[derive(Serialize)]
struct RegistryFetchPayload {
    source: String,
    id: String,
}

#[cfg(feature = "registry")]
fn wasm_registry_fetch_only_promise(name: String) -> js_sys::Promise {
    wasm_bindgen_futures::future_to_promise(async move {
        use crate::registry::{LemmaBase, Registry, RegistryErrorKind};

        let registry = LemmaBase::new();
        let bundle = match registry.get(&name).await {
            Ok(b) => b,
            Err(registry_error) => {
                let suggestion = match &registry_error.kind {
                    RegistryErrorKind::NotFound => Some(
                        "Check that the repository qualifier is spelled correctly and that the repository exists on the registry.".to_string(),
                    ),
                    RegistryErrorKind::Unauthorized => Some(
                        "Check your authentication credentials or permissions for this registry."
                            .to_string(),
                    ),
                    RegistryErrorKind::NetworkError => Some(
                        "Check your network connection.".to_string(),
                    ),
                    RegistryErrorKind::ServerError => Some(
                        "The registry server returned an internal error. Try again later.".to_string(),
                    ),
                    RegistryErrorKind::Other => None,
                };
                let source = Source::new(
                    SourceType::Volatile,
                    crate::parsing::ast::Span {
                        start: 0,
                        end: 0,
                        line: 1,
                        col: 1,
                    },
                );
                let err = Error::registry(
                    registry_error.message,
                    source,
                    name.clone(),
                    registry_error.kind,
                    suggestion,
                    None,
                    None,
                );
                let errors = vec![JsError::from(&err)];
                return Err(errors
                    .serialize(&js_error_serializer())
                    .expect("BUG: serialize JsError array"));
            }
        };

        let payload = RegistryFetchPayload {
            source: bundle.lemma_source,
            id: name,
        };
        serialize_engine_json(&payload)
    })
}

/// Same JSON as CLI/HTTP. `serde_wasm_bindgen::to_value(serde_json::Value)` drops
/// `IndexMap` entries (e.g. `Response.results` → `{}`); `JSON.parse` matches browser semantics.
fn serialize_engine_json<T: Serialize>(v: &T) -> Result<JsValue, JsValue> {
    let s = serde_json::to_string(v)
        .map_err(|e| js_err(format!("BUG: serde_json::to_string failed: {}", e)))?;
    js_sys::JSON::parse(&s).map_err(|e| {
        let detail = e
            .as_string()
            .unwrap_or_else(|| format!("(non-string error from JSON.parse)"));
        js_err(format!("BUG: JSON.parse failed: {}", detail))
    })
}

fn js_err(msg: impl Into<String>) -> JsValue {
    JsValue::from_str(&msg.into())
}

/// Source slice serialized for JS (`EngineError.source` in TS).
#[derive(Serialize)]
struct JsSource {
    attribute: String,
    line: usize,
    column: usize,
    length: usize,
}

impl<'a> From<&'a Source> for JsSource {
    fn from(s: &'a Source) -> Self {
        JsSource {
            attribute: s.source_type.to_string(),
            line: s.span.line,
            column: s.span.col,
            length: s.span.end.saturating_sub(s.span.start),
        }
    }
}

/// Flat view of [`Error`] for `serde_wasm_bindgen` — matches `EngineError` in
/// `engine/packages/npm/lemma.d.ts`.
#[derive(Serialize)]
struct JsError<'a> {
    kind: ErrorKind,
    message: &'a str,
    related_data: Option<&'a str>,
    spec: Option<&'a str>,
    related_spec: Option<&'a str>,
    source: Option<JsSource>,
    suggestion: Option<&'a str>,
    /// Set for [`Error::MissingRepository`] and [`Error::Registry`] (registry `@` id).
    repository: Option<&'a str>,
}

impl<'a> From<&'a Error> for JsError<'a> {
    fn from(e: &'a Error) -> Self {
        JsError {
            kind: e.kind(),
            message: e.message(),
            related_data: e.related_data(),
            spec: e.spec_context_name(),
            related_spec: e.related_spec(),
            source: e.source_location().map(JsSource::from),
            suggestion: e.suggestion(),
            repository: e.repository(),
        }
    }
}

/// Serializer that emits `null` (not `undefined`) for missing optionals so the object
/// matches the published `EngineError` TypeScript type.
fn js_error_serializer() -> serde_wasm_bindgen::Serializer {
    serde_wasm_bindgen::Serializer::new().serialize_missing_as_null(true)
}

/// Convert an engine [`Error`] into a plain JS object thrown from WASM.
fn error_to_js(e: &Error) -> JsValue {
    let err = JsError::from(e);
    err.serialize(&js_error_serializer())
        .expect("BUG: serialize JsError")
}

fn parse_rule_names(v: &JsValue) -> Result<Vec<String>, String> {
    if v.is_undefined() || v.is_null() {
        return Ok(Vec::new());
    }
    if js_sys::Array::is_array(v) {
        return serde_wasm_bindgen::from_value(v.clone())
            .map_err(|e| format!("rule_names must be an array of strings: {}", e));
    }
    if let Some(s) = v.as_string() {
        let trimmed = s.trim();
        if trimmed.is_empty() || trimmed == "[]" {
            return Ok(Vec::new());
        }
        return serde_json::from_str::<Vec<String>>(trimmed).map_err(|e| {
            format!(
                "rule_names must be an array of strings (or JSON array string): {}",
                e
            )
        });
    }
    Err("rule_names must be an array of strings".into())
}

fn parse_data_values(v: &JsValue) -> Result<HashMap<String, String>, String> {
    if v.is_undefined() || v.is_null() {
        return Ok(HashMap::new());
    }
    let map: HashMap<String, serde_json::Value> = serde_wasm_bindgen::from_value(v.clone())
        .map_err(|e| format!("data_values must be a plain object: {}", e))?;
    Ok(data_values_from_map(map))
}