Skip to content

Commit 0813a45

Browse files
authored
DOC: Fill out API docs for catlog-wasm and warn on missing docs. (#663)
1 parent 9aec81b commit 0813a45

File tree

13 files changed

+63
-103
lines changed

13 files changed

+63
-103
lines changed

packages/catlog-wasm/README.md

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1+
//! Auxiliary structs for data passed to/from analyses.
2+
13
use serde::{Deserialize, Serialize};
24
use tsify::Tsify;
35
use uuid::Uuid;
46

57
use super::result::JsResult;
68
use catlog::stdlib::analyses;
79

10+
/// The result of an ODE analysis, containing the solution when successful.
811
#[derive(Serialize, Deserialize, Tsify)]
912
#[tsify(into_wasm_abi, from_wasm_abi)]
1013
pub struct ODEResult(pub JsResult<analyses::ode::ODESolution<Uuid>, String>);
1114

15+
/// Input data for a Lokta-Volterra analysis of a model.
1216
#[derive(Serialize, Deserialize, Tsify)]
1317
#[tsify(into_wasm_abi, from_wasm_abi)]
1418
pub struct LotkaVolterraModelData(pub analyses::ode::LotkaVolterraProblemData<Uuid>);
1519

20+
/// Input data for a linear ODE analysis of a model.
1621
#[derive(Serialize, Deserialize, Tsify)]
1722
#[tsify(into_wasm_abi, from_wasm_abi)]
1823
pub struct LinearODEModelData(pub analyses::ode::LinearODEProblemData<Uuid>);
1924

25+
/// Input data for a mass-action dynamics analysis of a model.
2026
#[derive(Serialize, Deserialize, Tsify)]
2127
#[tsify(into_wasm_abi, from_wasm_abi)]
2228
pub struct MassActionModelData(pub analyses::ode::MassActionProblemData<Uuid>);

packages/catlog-wasm/src/lib.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/*! Rust-TypeScript interop for categorical logic.
2+
3+
This crate provides [WebAssembly](https://webassembly.org/) (Wasm) bindings for
4+
the [`catlog`] crate using Rust's `wasm-bindgen`. The aim is to keep the logic
5+
here as simple as possible, with [`catlog`] doing all the real work. However,
6+
the translation is nontrivial because there is a single, catch-all type
7+
definition for a double theory in TypeScript, but several kinds of double
8+
theories implemented in Rust (discrete theories, modal theories, and so on). The
9+
same is true for other structures, such as models of theories and diagrams in
10+
models.
11+
*/
12+
13+
#![warn(missing_docs)]
14+
115
pub mod notation;
216
pub mod result;
317

@@ -8,6 +22,7 @@ pub mod theory;
822

923
pub mod analyses;
1024
#[allow(clippy::new_without_default)]
25+
#[allow(missing_docs)]
1126
pub mod theories;
1227

1328
use wasm_bindgen::prelude::*;
@@ -29,14 +44,16 @@ export type Ustr = string;
2944
export type NonEmpty<T> = Array<T>;
3045
"#;
3146

47+
/** Set panic hook to get better error messages on panics.
48+
49+
When the `console_error_panic_hook` feature is enabled, we can call the
50+
`set_panic_hook` function at least once during initialization, and then we will
51+
get better error messages if our code ever panics.
52+
53+
For more details see <https://github.com/rustwasm/console_error_panic_hook#readme>
54+
*/
3255
#[wasm_bindgen]
3356
pub fn set_panic_hook() {
34-
// When the `console_error_panic_hook` feature is enabled, we can call the
35-
// `set_panic_hook` function at least once during initialization, and then
36-
// we will get better error messages if our code ever panics.
37-
//
38-
// For more details see
39-
// https://github.com/rustwasm/console_error_panic_hook#readme
4057
#[cfg(feature = "console_error_panic_hook")]
4158
console_error_panic_hook::set_once();
4259
}

packages/catlog-wasm/src/model.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Wasm bindings for models of a double theory.
2+
13
use all_the_same::all_the_same;
24
use derive_more::{From, TryInto};
35
use serde::{Deserialize, Serialize};
@@ -31,11 +33,15 @@ See [`DblTheoryBox`] for motivation.
3133
#[derive(From, TryInto)]
3234
#[try_into(ref, ref_mut)]
3335
pub enum DblModelBox {
36+
/// A model of a discrete double theory.
3437
Discrete(DiscreteDblModel),
38+
/// A model of a discrete tabulator theory.
3539
DiscreteTab(DiscreteTabModel),
40+
/// A model of a modal double theory.
3641
Modal(ModalDblModel),
3742
}
3843

44+
/// Wasm binding of a model of a double theory.
3945
#[wasm_bindgen]
4046
pub struct DblModel(#[wasm_bindgen(skip)] pub DblModelBox);
4147

@@ -455,6 +461,7 @@ impl DblModel {
455461
})
456462
}
457463

464+
/// Validates the model, returning any validation failures.
458465
pub fn validate(&self) -> ModelValidationResult {
459466
all_the_same!(match &self.0 {
460467
DblModelBox::[Discrete, DiscreteTab, Modal](model) => {
@@ -478,6 +485,7 @@ pub fn collect_product(ob: Ob) -> Result<Vec<Ob>, String> {
478485
Ok(vec.into_iter().map(|ob| Quoter.quote(&ob)).collect())
479486
}
480487

488+
/// Elaborates a model defined by a notebook into a catlog model.
481489
#[wasm_bindgen(js_name = "elaborateModel")]
482490
pub fn elaborate_model(doc: &ModelDocumentContent, theory: &DblTheory) -> DblModel {
483491
let mut model = DblModel::new(theory);

packages/catlog-wasm/src/model_diagram.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ use super::theory::DblTheory;
2323
/// A box containing a diagram in a model of a double theory.
2424
#[derive(From)]
2525
pub enum DblModelDiagramBox {
26+
/// A diagram in a model of a discrete double theory.
2627
Discrete(diagram::DblModelDiagram<DiscreteDblModelMapping, DiscreteDblModel>),
2728
// DiscreteTab(), # TODO: Not implemented.
2829
}
2930

30-
/// Wasm bindings for a diagram in a model of a double theory.
31+
/// Wasm binding for a diagram in a model of a double theory.
3132
#[wasm_bindgen]
3233
pub struct DblModelDiagram(#[wasm_bindgen(skip)] pub DblModelDiagramBox);
3334

@@ -203,6 +204,7 @@ pub struct ModelDiagramValidationResult(
203204
pub JsResult<(), Vec<diagram::InvalidDiscreteDblModelDiagram<Uuid>>>,
204205
);
205206

207+
/// Elaborates a diagram defined by a notebook into a catlog diagram.
206208
#[wasm_bindgen(js_name = "elaborateDiagram")]
207209
pub fn elaborate_diagram(doc: &DiagramDocumentContent, theory: &DblTheory) -> DblModelDiagram {
208210
let mut diagram = DblModelDiagram::new(theory);

packages/catlog-wasm/src/model_morphism.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Wasm bindings for morphisms between models of a double theory.
2+
13
use std::hash::Hash;
24

35
use serde::{Deserialize, Serialize};

packages/catlog-wasm/src/notation.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ singleton.
99
*/
1010
pub struct Elaborator;
1111

12+
/** A type that can elaborated into another.
13+
14+
Says that objects of type `T` can be elaborated into objects of type `S`.
15+
*/
1216
pub trait CanElaborate<T, S> {
1317
/// Transform notation into syntax.
1418
fn elab(&self, x: &T) -> Result<S, String>;
@@ -21,6 +25,10 @@ Unlike elaboration, quotation is infallible.
2125
*/
2226
pub struct Quoter;
2327

28+
/** A typed that quoted into another.
29+
30+
Says that objects of type `T` can be quoted as objects of type `S`.
31+
*/
2432
pub trait CanQuote<T, S> {
2533
/// Transform syntax or value into notation.
2634
fn quote(&self, x: &T) -> S;

packages/catlog-wasm/src/theories.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/*! Wasm bindings for double theories from the `catlog` standard library.
1+
/*! Wasm bindings for the standard library of theories in `catlog`.
22
3-
Each struct in this module provides a [`DblTheory`] plus possibly
4-
theory-specific analysis methods.
3+
Each struct in this module provides a [`DblTheory`], possibly with additional
4+
methods for theory-specific analyses.
55
*/
66

77
use std::rc::Rc;

packages/catlog-wasm/src/theory.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,15 @@ explicitly enumerate the supported kinds of double theories in this enum.
261261
#[derive(From, TryInto)]
262262
#[try_into(ref)]
263263
pub enum DblTheoryBox {
264+
/// A discrete double theory.
264265
Discrete(Rc<theory::UstrDiscreteDblTheory>),
266+
/// A discrete tabulator theory.
265267
DiscreteTab(Rc<theory::UstrDiscreteTabTheory>),
268+
/// A modal double theory.
266269
Modal(Rc<theory::UstrModalDblTheory>),
267270
}
268271

269-
/** Wasm bindings for a double theory.
270-
*/
272+
/// Wasm binding for a double theory.
271273
#[wasm_bindgen]
272274
pub struct DblTheory(#[wasm_bindgen(skip)] pub DblTheoryBox);
273275

packages/catlog/src/stdlib/analyses/ode/mass_action.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ impl StockFlowMassActionAnalysis {
169169
};
170170
}
171171

172-
let terms: Vec<(Id, Polynomial<Id, Parameter<Id>, u8>)> = terms
172+
let terms: Vec<_> = terms
173173
.into_iter()
174174
.map(|(flow, term)| {
175175
let param = Parameter::generator(flow.clone());
176-
(flow, [(param, term)].into_iter().collect())
176+
let poly: Polynomial<_, _, _> = [(param, term)].into_iter().collect();
177+
(flow, poly)
177178
})
178179
.collect();
179180

0 commit comments

Comments
 (0)