Skip to content

Commit b674155

Browse files
committed
document function calls
1 parent 331a8d8 commit b674155

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

.rustfmt.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
normalize_comments = true
2+
wrap_comments = true

src/lib.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//! This crate provides rusty bindings to SortableJS.
2+
//!
3+
//! The documentation mostly lives with [the official SortableJS documentation](https://github.com/SortableJS/Sortable).
4+
//!
5+
//! Just adding this crate as a dependency should be enough to get everything working when using [trunk](https://trunkrs.dev/). Just be careful to keep alive the return value of `Sortable::apply`, or you will get JavaScript exceptions.
6+
//!
7+
//! You can find example usage of SortableJS from a pure-Rust WASM application
8+
//! in the `examples/` directory.
9+
110
use std::rc::Rc;
211

312
use wasm_bindgen::closure::Closure;
@@ -15,6 +24,10 @@ mod js {
1524
}
1625
}
1726

27+
/// An event raised by one of the Sortable callbacks. See [the official documentation](https://github.com/SortableJS/Sortable#event-object-demo) for details about the fields.
28+
///
29+
/// `raw_event` contains the raw JS event, should additional non-documented
30+
/// fields be needed.
1831
#[derive(Clone, Debug)]
1932
pub struct Event {
2033
pub raw_event: js_sys::Object,
@@ -44,10 +57,15 @@ impl Event {
4457
js_sys::Reflect::get(&raw_event, &JsValue::from_str($field))
4558
.ok()
4659
.map(|evt| {
47-
let float = evt.as_f64()
60+
let float = evt
61+
.as_f64()
4862
.expect("failed casting field of raw event to proper type");
4963
let int = float as usize;
50-
assert!((int as f64 - float).abs() < 0.1, "received index that is not an integer: {}", float);
64+
assert!(
65+
(int as f64 - float).abs() < 0.1,
66+
"received index that is not an integer: {}",
67+
float
68+
);
5169
int
5270
})
5371
};
@@ -117,6 +135,10 @@ macro_rules! callback {
117135
}
118136

119137
impl Options {
138+
/// Create a builder for `Sortable`
139+
///
140+
/// This builder allows for configuration options to be set. Once the
141+
/// desired configuration is done, use `apply` to apply it to a list.
120142
pub fn new() -> Options {
121143
Options {
122144
options: js_sys::Object::new(),
@@ -183,14 +205,20 @@ impl Options {
183205

184206
// TODO: onMove
185207

186-
/// Recover the javascript options that are being built in this object.
208+
/// Recover the javascript options that are being built in this object
187209
///
188210
/// Note that you can set options on this object through `js_sys::Reflect`.
189-
/// This allows setting options that are not planned for by `sortable-js-rs`.
211+
/// This allows setting options that are not planned for by
212+
/// `sortable-js-rs`.
190213
pub fn options(&self) -> &js_sys::Object {
191214
&self.options
192215
}
193216

217+
/// Apply the current configuration as a `Sortable` instance on `elt`
218+
///
219+
/// Do not forget to keep the return value of this function alive for as
220+
/// long as you want the callbacks to be callable, as JS code will error out
221+
/// should an event happen after it was dropped.
194222
pub fn apply(&self, elt: &web_sys::Element) -> Sortable {
195223
js::Sortable::new(elt, &self.options);
196224
Sortable {
@@ -201,7 +229,9 @@ impl Options {
201229

202230
/// Data related to the Sortable instance
203231
///
204-
/// It must be kept alive on the rust sideas long as the instance can call callbacks, as otherwise the link between the js-side callback and the rust-side callback would be lost.
232+
/// It must be kept alive on the rust sideas long as the instance can call
233+
/// callbacks, as otherwise the link between the js-side callback and the
234+
/// rust-side callback would be lost.
205235
pub struct Sortable {
206236
/// Keep the callbacks alive
207237
_callbacks: [Option<Rc<Closure<dyn FnMut(js_sys::Object)>>>; CallbackId::_Total as usize],

0 commit comments

Comments
 (0)