-use wasm_bindgen::prelude::*;
-use fehler::throws;
use std::fmt::Display;
+use fehler::throws;
use js_sys::JsString;
+use thiserror::Error;
+use wasm_bindgen::prelude::*;
-use zcoord::ZCoord;
+use zcoord::{ZCoord,Mutable};
-#[wasm_bindgen]
-pub struct ZCoordIterator (zcoord::Mutable);
+#[derive(Error,Clone,Copy,Debug,Eq,PartialEq)]
+#[error("packed Z coordinate wrong JS type (not a string)")]
+pub struct JsZCoordTypeError;
trait WasmError {
fn e(self) -> JsValue;
fn e(self) -> Result<V, JsValue> { self.map_err(WasmError::e) }
}
+#[throws(JsValue)]
+fn get_packed_str(js: &JsValue) -> String {
+ js.as_string().ok_or(JsZCoordTypeError).e()?
+}
+
#[throws(JsValue)]
#[wasm_bindgen]
pub fn check(packed: &JsValue) {
- let s = packed.as_string().ok_or(
- "packed Z coordinate wrong JS type (not a string)",
- ).e()?;
- ZCoord::check_str(&s).e()?;
+ ZCoord::check_str(&get_packed_str(packed)?).e()?;
}
-//const X : &'static str = "invalid value passed to wasm";
-/*
+#[wasm_bindgen]
+pub struct ZCoordIterator (zcoord::BoxedIterator);
+
#[throws(JsValue)]
#[wasm_bindgen]
-pub fn mutable(s: String) -> ZCoordIterator {
- ZCoordIterator(ZCoord::from_str(&s).ok_or(X)?.clone_mut())
-}*/
+pub fn range(a: &JsValue, b: &JsValue, count: zcoord::RangeCount)
+ -> ZCoordIterator {
+ #[throws(JsValue)]
+ fn get1(js: &JsValue) -> Option<Mutable> {
+ if js.is_null() { return None }
+ let s = get_packed_str(js)?;
+ let m = Mutable::from_str(&s).e()?;
+ Some(m)
+ }
+
+ let a = get1(a)?;
+ let b = get1(b)?;
+ let inner = Mutable::some_range(a.as_ref(),b.as_ref(),count).e()?;
+ ZCoordIterator(inner)
+}
#[wasm_bindgen]
impl ZCoordIterator {
- pub fn next(&mut self) -> u32 { 42 }
+ pub fn next(&mut self) -> JsValue {
+ let packed = match self.0.next() {
+ None => return JsValue::NULL,
+ Some(p) => p,
+ };
+ packed.to_string().into()
+ }
}
#[wasm_bindgen]
//---------- core definitions ----------
+pub type RangeCount = u32;
+
const BITS_PER_DIGIT : usize = 5;
const DIGITS_PER_LIMB : usize = 10;
}
#[throws(RangeBackwards)]
- fn range_core(a: &Mutable, b: &Mutable, count: u32)
+ fn range_core(a: &Mutable, b: &Mutable, count: RangeCount)
-> IteratorCore<AddSubRangeDelta> {
type ASRD = AddSubRangeDelta;
let count = count as RawLimbVal;
}
#[throws(RangeBackwards)]
- pub fn range_upto(&self, other: &Mutable, count: u32) -> RangeIterator {
+ pub fn range_upto(&self, other: &Mutable, count: RangeCount)
+ -> RangeIterator {
Mutable::range_core(self, other, count)?.take(count as usize)
}
}
IteratorCore { current: self, aso }
}
#[throws(LogicError)]
- pub fn some_range(a: Option<&Mutable>, b: Option<&Mutable>, count: u32)
- -> BoxedIterator {
+ pub fn some_range(a: Option<&Mutable>, b: Option<&Mutable>,
+ count: RangeCount) -> BoxedIterator {
fn mk<T:'static + Iterator<Item=ZCoord>>(x: T) -> BoxedIterator
{ Box::new(x) }
match (a, b) {