Skip to content

Commit

Permalink
Improving error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Jul 26, 2022
1 parent 8c1f485 commit 623547f
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 43 deletions.
2 changes: 1 addition & 1 deletion examples/async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct User {
impl User {
async fn fetch(user: &str) -> Result<Self, JsValue> {
let user = fetch_github(&format!("https://api.github.com/users/{}", user)).await?;
Ok(serde_json::from_str::<Self>(&user).unwrap_throw())
Ok(serde_json::from_str::<Self>(&user).unwrap())
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/async/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub async fn fetch_github(url: &str) -> Result<String, JsValue> {
headers.set("Accept", "application/vnd.github.v3+json")?;

let future = window()
.unwrap_throw()
.unwrap()
.fetch_with_str_and_init(
url,
RequestInit::new()
Expand All @@ -131,7 +131,7 @@ pub async fn fetch_github(url: &str) -> Result<String, JsValue> {
let value = JsFuture::from(response.text()?)
.await?
.as_string()
.unwrap_throw();
.unwrap();

Ok(value)
}
9 changes: 4 additions & 5 deletions examples/todomvc/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;
use std::cell::Cell;
use wasm_bindgen::prelude::*;
use web_sys::{Url, HtmlInputElement};
use serde_derive::{Serialize, Deserialize};
use futures_signals::signal::{Signal, SignalExt, Mutable};
Expand All @@ -21,7 +20,7 @@ pub enum Route {
impl Route {
// This could use more advanced URL parsing, but it isn't needed
pub fn from_url(url: &str) -> Self {
let url = Url::new(&url).unwrap_throw();
let url = Url::new(&url).unwrap();
match url.hash().as_str() {
"#/active" => Route::Active,
"#/completed" => Route::Completed,
Expand Down Expand Up @@ -72,19 +71,19 @@ impl App {
pub fn deserialize() -> Arc<Self> {
local_storage()
.get_item("todos-rust-dominator")
.unwrap_throw()
.unwrap()
.and_then(|state_json| {
serde_json::from_str(state_json.as_str()).ok()
})
.unwrap_or_else(App::new)
}

pub fn serialize(&self) {
let state_json = serde_json::to_string(self).unwrap_throw();
let state_json = serde_json::to_string(self).unwrap();

local_storage()
.set_item("todos-rust-dominator", state_json.as_str())
.unwrap_throw();
.unwrap();
}

pub fn route(&self) -> impl Signal<Item = Route> {
Expand Down
3 changes: 1 addition & 2 deletions examples/todomvc/src/todo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::sync::Arc;
use wasm_bindgen::prelude::*;
use serde_derive::{Serialize, Deserialize};
use futures_signals::map_ref;
use futures_signals::signal::{Signal, SignalExt, Mutable};
Expand Down Expand Up @@ -126,7 +125,7 @@ impl Todo {
.event(clone!(todo => move |event: events::KeyDown| {
match event.key().as_str() {
"Enter" => {
element.blur().unwrap_throw();
element.blur().unwrap();
},
"Escape" => {
todo.cancel_editing();
Expand Down
3 changes: 1 addition & 2 deletions examples/todomvc/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use wasm_bindgen::prelude::*;
use web_sys::{window, Storage};


pub fn local_storage() -> Storage {
window().unwrap_throw().local_storage().unwrap_throw().unwrap_throw()
window().unwrap().local_storage().unwrap().unwrap()
}

#[inline]
Expand Down
5 changes: 3 additions & 2 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use wasm_bindgen::closure::Closure;
use web_sys::window;

use crate::operations::spawn_future;
use crate::utils::UnwrapJsExt;


struct RafState {
Expand All @@ -37,7 +38,7 @@ impl Raf {
window()
.unwrap_throw()
.request_animation_frame(callback.as_ref().unchecked_ref())
.unwrap_throw()
.unwrap_js()
}

let closure = {
Expand Down Expand Up @@ -71,7 +72,7 @@ impl Drop for Raf {
window()
.unwrap_throw()
.cancel_animation_frame(state.id)
.unwrap_throw();
.unwrap_js();
}
}

Expand Down
69 changes: 46 additions & 23 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen::{JsCast, intern};
use js_sys::Reflect;
use web_sys::{HtmlElement, Element, Node, Window, History, Document, Text, Comment, DomTokenList, CssStyleSheet, CssStyleDeclaration, HtmlStyleElement, CssStyleRule, EventTarget};
use crate::utils::UnwrapJsExt;


// TODO move this into wasm-bindgen or gloo or something
// TODO maybe use Object for obj ?
#[track_caller]
pub(crate) fn set_property(obj: &JsValue, name: &str, value: &JsValue) {
Reflect::set(obj, &JsValue::from(name), value).unwrap_throw();
Reflect::set(obj, &JsValue::from(name), value).unwrap_js();
}


thread_local! {
static WINDOW: Window = web_sys::window().unwrap_throw();
static DOCUMENT: Document = WINDOW.with(|w| w.document().unwrap_throw());
static HISTORY: History = WINDOW.with(|w| w.history().unwrap_throw());
static HISTORY: History = WINDOW.with(|w| w.history().unwrap_js());
}

pub(crate) fn window_event_target() -> EventTarget {
Expand All @@ -30,30 +32,34 @@ pub(crate) fn ready_state() -> String {
DOCUMENT.with(|d| d.ready_state())
}

#[track_caller]
pub(crate) fn current_url() -> String {
WINDOW.with(|w| w.location().href().unwrap_throw())
WINDOW.with(|w| w.location().href().unwrap_js())
}

#[track_caller]
pub(crate) fn go_to_url(url: &str) {
HISTORY.with(|h| {
h.push_state_with_url(&JsValue::NULL, "", Some(url)).unwrap_throw();
h.push_state_with_url(&JsValue::NULL, "", Some(url)).unwrap_js();
});
}

#[track_caller]
pub(crate) fn create_stylesheet() -> CssStyleSheet {
DOCUMENT.with(|document| {
// TODO use createElementNS ?
// TODO use dyn_into ?
let e: HtmlStyleElement = document.create_element("style").unwrap_throw().unchecked_into();
let e: HtmlStyleElement = document.create_element("style").unwrap_js().unchecked_into();
e.set_type("text/css");
append_child(&document.head().unwrap_throw(), &e);
// TODO use dyn_into ?
e.sheet().unwrap_throw().unchecked_into()
})
}

#[track_caller]
pub(crate) fn make_style_rule(sheet: &CssStyleSheet, selector: &str) -> Result<CssStyleRule, JsValue> {
let rules = sheet.css_rules().unwrap_throw();
let rules = sheet.css_rules().unwrap_js();
let length = rules.length();
// TODO don't return u32 ?
sheet.insert_rule_with_index(&format!("{}{{}}", selector), length)?;
Expand All @@ -66,12 +72,14 @@ pub(crate) fn get_element_by_id(id: &str) -> Element {
DOCUMENT.with(|d| d.get_element_by_id(id).unwrap_throw())
}

#[track_caller]
pub(crate) fn create_element(name: &str) -> Element {
DOCUMENT.with(|d| d.create_element(name).unwrap_throw())
DOCUMENT.with(|d| d.create_element(name).unwrap_js())
}

#[track_caller]
pub(crate) fn create_element_ns(namespace: &str, name: &str) -> Element {
DOCUMENT.with(|d| d.create_element_ns(Some(namespace), name).unwrap_throw())
DOCUMENT.with(|d| d.create_element_ns(Some(namespace), name).unwrap_js())
}

pub(crate) fn create_text_node(value: &str) -> Text {
Expand All @@ -94,68 +102,83 @@ pub(crate) fn create_empty_node() -> Node {
}

// TODO check that the attribute *actually* was changed
#[track_caller]
pub(crate) fn set_attribute(elem: &Element, key: &str, value: &str) {
elem.set_attribute(key, value).unwrap_throw();
elem.set_attribute(key, value).unwrap_js();
}

#[track_caller]
pub(crate) fn set_attribute_ns(elem: &Element, namespace: &str, key: &str, value: &str) {
elem.set_attribute_ns(Some(namespace), key, value).unwrap_throw();
elem.set_attribute_ns(Some(namespace), key, value).unwrap_js();
}

#[track_caller]
pub(crate) fn remove_attribute(elem: &Element, key: &str) {
elem.remove_attribute(key).unwrap_throw();
elem.remove_attribute(key).unwrap_js();
}

#[track_caller]
pub(crate) fn remove_attribute_ns(elem: &Element, namespace: &str, key: &str) {
elem.remove_attribute_ns(Some(namespace), key).unwrap_throw();
elem.remove_attribute_ns(Some(namespace), key).unwrap_js();
}

#[track_caller]
pub(crate) fn add_class(classes: &DomTokenList, value: &str) {
classes.add_1(value).unwrap_throw();
classes.add_1(value).unwrap_js();
}

#[track_caller]
pub(crate) fn remove_class(classes: &DomTokenList, value: &str) {
classes.remove_1(value).unwrap_throw();
classes.remove_1(value).unwrap_js();
}

#[track_caller]
pub(crate) fn get_style(style: &CssStyleDeclaration, name: &str) -> String {
style.get_property_value(name).unwrap_throw()
style.get_property_value(name).unwrap_js()
}

#[track_caller]
pub(crate) fn remove_style(style: &CssStyleDeclaration, name: &str) {
// TODO don't return String ?
style.remove_property(name).unwrap_throw();
style.remove_property(name).unwrap_js();
}

#[track_caller]
pub(crate) fn set_style(style: &CssStyleDeclaration, name: &str, value: &str, important: bool) {
let priority = if important { intern("important") } else { intern("") };
style.set_property_with_priority(name, value, priority).unwrap_throw();
style.set_property_with_priority(name, value, priority).unwrap_js();
}

#[track_caller]
pub(crate) fn insert_child_before(parent: &Node, child: &Node, other: &Node) {
// TODO don't return Node ?
parent.insert_before(child, Some(other)).unwrap_throw();
parent.insert_before(child, Some(other)).unwrap_js();
}

#[track_caller]
pub(crate) fn replace_child(parent: &Node, child: &Node, other: &Node) {
// TODO don't return Node ?
parent.replace_child(child, other).unwrap_throw();
parent.replace_child(child, other).unwrap_js();
}

#[track_caller]
pub(crate) fn append_child(parent: &Node, child: &Node) {
// TODO don't return Node ?
parent.append_child(child).unwrap_throw();
parent.append_child(child).unwrap_js();
}

#[track_caller]
pub(crate) fn remove_child(parent: &Node, child: &Node) {
// TODO don't return Node ?
parent.remove_child(child).unwrap_throw();
parent.remove_child(child).unwrap_js();
}

#[track_caller]
pub(crate) fn focus(elem: &HtmlElement) {
elem.focus().unwrap_throw();
elem.focus().unwrap_js();
}

#[track_caller]
pub(crate) fn blur(elem: &HtmlElement) {
elem.blur().unwrap_throw();
elem.blur().unwrap_js();
}
Loading

0 comments on commit 623547f

Please sign in to comment.