Documentation

This commit is contained in:
bakaq
2025-02-20 00:48:51 -03:00
parent 3b89c1124d
commit 080a4fed03

View File

@@ -1,7 +1,5 @@
//! Wasm interface //! Wasm interface
#![allow(missing_docs)]
use std::mem; use std::mem;
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
@@ -11,6 +9,7 @@ use wasm_bindgen::prelude::*;
use crate::*; use crate::*;
/// A builder for a `Machine`.
#[wasm_bindgen(js_name = MachineBuilder)] #[wasm_bindgen(js_name = MachineBuilder)]
#[derive(Default)] #[derive(Default)]
pub struct WasmMachineBuilder { pub struct WasmMachineBuilder {
@@ -19,11 +18,13 @@ pub struct WasmMachineBuilder {
#[wasm_bindgen(js_class = MachineBuilder)] #[wasm_bindgen(js_class = MachineBuilder)]
impl WasmMachineBuilder { impl WasmMachineBuilder {
/// Creates a new `MachineBuilder` with the default configuration.
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor)]
pub fn new() -> Self { pub fn new() -> Self {
Default::default() Default::default()
} }
/// Creates a new `Machine`.
pub fn build(&mut self) -> WasmMachine { pub fn build(&mut self) -> WasmMachine {
WasmMachine { WasmMachine {
inner: Ok(std::mem::take(&mut self.inner).build()), inner: Ok(std::mem::take(&mut self.inner).build()),
@@ -31,6 +32,7 @@ impl WasmMachineBuilder {
} }
} }
/// The Scryer Prolog `Machine`.
#[wasm_bindgen(js_name = Machine)] #[wasm_bindgen(js_name = Machine)]
pub struct WasmMachine { pub struct WasmMachine {
inner: Result<Machine, Receiver<Machine>>, inner: Result<Machine, Receiver<Machine>>,
@@ -52,6 +54,10 @@ impl WasmMachine {
Ok(()) Ok(())
} }
/// Runs a query.
///
/// You can only have one query at a time. If you try to do anything with this machine while
/// doing a query an error will be thrown.
#[wasm_bindgen(js_name = runQuery)] #[wasm_bindgen(js_name = runQuery)]
pub fn run_query(&mut self, query: String) -> Result<JsValue, JsValue> { pub fn run_query(&mut self, query: String) -> Result<JsValue, JsValue> {
self.ensure_machine_ownership()?; self.ensure_machine_ownership()?;
@@ -76,6 +82,7 @@ impl WasmMachine {
Ok(query_state) Ok(query_state)
} }
/// Consults a module.
#[wasm_bindgen(js_name = consultModuleString)] #[wasm_bindgen(js_name = consultModuleString)]
pub fn consult_module_string( pub fn consult_module_string(
&mut self, &mut self,
@@ -101,6 +108,7 @@ struct WasmQueryStateInner {
query_state: QueryState<'this>, query_state: QueryState<'this>,
} }
/// The state of a running query.
#[wasm_bindgen(js_name = QueryState)] #[wasm_bindgen(js_name = QueryState)]
pub struct WasmQueryState { pub struct WasmQueryState {
inner: Option<WasmQueryStateInner>, inner: Option<WasmQueryStateInner>,
@@ -108,6 +116,12 @@ pub struct WasmQueryState {
#[wasm_bindgen(js_class = QueryState)] #[wasm_bindgen(js_class = QueryState)]
impl WasmQueryState { impl WasmQueryState {
/// Gets the next leaf answer.
///
/// This follows the Javascript iterator protocol, so it returns an object that
/// contains a `done` field and a `value` field. If `done` is `false`, then the query ended
/// and control of the `Machine` will be given back to the `Machine` that created this query.
/// Any call after that will result in an error.
#[wasm_bindgen(js_name = next)] #[wasm_bindgen(js_name = next)]
pub fn next_answer(&mut self) -> Result<JsValue, JsValue> { pub fn next_answer(&mut self) -> Result<JsValue, JsValue> {
let ret = js_sys::Object::new(); let ret = js_sys::Object::new();
@@ -146,6 +160,10 @@ impl WasmQueryState {
Ok(ret.into()) Ok(ret.into())
} }
/// Drops the query.
///
/// This is useful to end a query early. Like finishing a query, control will be given back
/// to the `Machine` and any call to `next` after that will result in an error.
#[wasm_bindgen(js_name = drop)] #[wasm_bindgen(js_name = drop)]
pub fn drop_inner(&mut self) { pub fn drop_inner(&mut self) {
let ouroboros_impl_wasm_query_state_inner::Heads { let ouroboros_impl_wasm_query_state_inner::Heads {