Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web-sys services conversion #827

Merged
merged 11 commits into from
Jan 15, 2020
98 changes: 79 additions & 19 deletions src/services/console.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
//! This module contains a service implementation to use browser's console.

#[allow(unused_imports)]
use stdweb::{_js_impl, js};
use cfg_if::cfg_if;
use cfg_match::cfg_match;
cfg_if! {
if #[cfg(feature = "std_web")] {
#[allow(unused_imports)]
use stdweb::{_js_impl, js};
} else if #[cfg(feature = "web_sys")] {
use wasm_bindgen::JsValue;
use web_sys::console;
}
}

/// A service to use methods of a
/// [Console](https://developer.mozilla.org/en-US/docs/Web/API/Console).
Expand All @@ -17,101 +26,152 @@ impl ConsoleService {
/// [console.log](https://developer.mozilla.org/en-US/docs/Web/API/Console/log)
/// method implementation.
pub fn log(&mut self, message: &str) {
js! { @(no_return) console.log(@{message}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.log(@{message}); },
feature = "web_sys" => console::log_1(&JsValue::from_str(message)),
};
}

/// [console.warn](https://developer.mozilla.org/en-US/docs/Web/API/Console/warn)
/// method implementation.
pub fn warn(&mut self, message: &str) {
js! { @(no_return) console.warn(@{message}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.warn(@{message}); },
feature = "web_sys" => console::warn_1(&JsValue::from_str(message)),
};
}

/// [console.info](https://developer.mozilla.org/en-US/docs/Web/API/Console/info)
/// method implementation.
pub fn info(&mut self, message: &str) {
js! { @(no_return) console.info(@{message}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.info(@{message}); },
feature = "web_sys" => console::info_1(&JsValue::from_str(message)),
};
}

/// [console.error](https://developer.mozilla.org/en-US/docs/Web/API/Console/error)
/// method implementation.
pub fn error(&mut self, message: &str) {
js! { @(no_return) console.error(@{message}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.error(@{message}); },
feature = "web_sys" => console::error_1(&JsValue::from_str(message)),
};
}

/// [console.debug](https://developer.mozilla.org/en-US/docs/Web/API/Console/debug)
/// method implementation.
pub fn debug(&mut self, message: &str) {
js! { @(no_return) console.debug(@{message}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.debug(@{message}); },
feature = "web_sys" => console::debug_1(&JsValue::from_str(message)),
};
}

/// [console.count_named](https://developer.mozilla.org/en-US/docs/Web/API/Console/count_named)
/// method implementation.
pub fn count_named(&mut self, name: &str) {
js! { @(no_return) console.count(@{name}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.count(@{name}); },
feature = "web_sys" => console::count_with_label(name),
};
}

/// [console.count](https://developer.mozilla.org/en-US/docs/Web/API/Console/count)
/// method implementation.
pub fn count(&mut self) {
js! { @(no_return) console.count(); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.count(); },
feature = "web_sys" => console::count(),
};
}

/// [console.time_named](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_named)
/// method implementation.
pub fn time_named(&mut self, name: &str) {
js! { @(no_return) console.time(@{name}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.time(@{name}); },
feature = "web_sys" => console::time_with_label(name),
};
}

/// [console.time_named_end](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_named_end)
/// method implementation.
pub fn time_named_end(&mut self, name: &str) {
js! { @(no_return) console.timeEnd(@{name}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.timeEnd(@{name}); },
feature = "web_sys" => console::time_end_with_label(name),
};
}

/// [console.time](https://developer.mozilla.org/en-US/docs/Web/API/Console/time)
/// method implementation.
pub fn time(&mut self) {
js! { @(no_return) console.time(); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.time(); },
feature = "web_sys" => console::time(),
};
}
/// [console.time_end](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_end)
/// method implementation.
pub fn time_end(&mut self) {
js! { @(no_return) console.timeEnd(); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.timeEnd(); },
feature = "web_sys" => console::time_end(),
};
}

/// [console.clear](https://developer.mozilla.org/en-US/docs/Web/API/Console/clear)
/// method implementation.
pub fn clear(&mut self) {
js! { @(no_return) console.clear(); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.clear(); },
feature = "web_sys" => console::clear(),
};
}

/// [console.group](https://developer.mozilla.org/en-US/docs/Web/API/Console/group)
/// method implementation.
pub fn group(&mut self) {
js! { @(no_return) console.group(); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.group(); },
feature = "web_sys" => console::group_0(),
};
}

/// [console.group_collapsed](https://developer.mozilla.org/en-US/docs/Web/API/Console/group_collapsed)
/// method implementation.
pub fn group_collapsed(&mut self) {
js! { @(no_return) console.groupCollapsed(); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.groupCollapsed(); },
feature = "web_sys" => console::group_collapsed_0(),
};
}

/// [console.group_end](https://developer.mozilla.org/en-US/docs/Web/API/Console/group_end)
/// method implementation.
pub fn group_end(&mut self) {
js! { @(no_return) console.groupEnd(); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.groupEnd(); },
feature = "web_sys" => console::group_end(),
};
}

/// [console.trace](https://developer.mozilla.org/en-US/docs/Web/API/Console/trace)
/// method implementation.
pub fn trace(&mut self) {
js! { @(no_return) console.trace(); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.trace(); },
feature = "web_sys" => console::trace_0(),
};
}

/// [console.assert](https://developer.mozilla.org/en-US/docs/Web/API/Console/assert)
/// method implementation.
pub fn assert(&mut self, condition: bool, message: &str) {
js! { @(no_return) console.assert(@{condition}, @{message}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) console.assert(@{condition}, @{message}); },
feature = "web_sys" => console::assert_with_condition_and_data_1(condition, &String::from(message).into()),
};
}
}
32 changes: 24 additions & 8 deletions src/services/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
//! This module contains the implementation of a service
//! to show alerts and confirm dialogs in a browser.

use stdweb::Value;
#[allow(unused_imports)]
use stdweb::{_js_impl, js};
use cfg_if::cfg_if;
use cfg_match::cfg_match;
cfg_if! {
if #[cfg(feature = "std_web")] {
use stdweb::Value;
#[allow(unused_imports)]
use stdweb::{_js_impl, js};
} else if #[cfg(feature = "web_sys")] {
use crate::utils;
}
}

/// A dialog service.
#[derive(Default, Debug)]
Expand All @@ -18,16 +26,24 @@ impl DialogService {
/// Calls [alert](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert)
/// function.
pub fn alert(&mut self, message: &str) {
js! { @(no_return) alert(@{message}); }
cfg_match! {
feature = "std_web" => js! { @(no_return) alert(@{message}); },
feature = "web_sys" => utils::window().alert_with_message(message).unwrap(),
};
}

/// Calls [confirm](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm)
/// function.
pub fn confirm(&mut self, message: &str) -> bool {
let value: Value = js! { return confirm(@{message}); };
match value {
Value::Bool(result) => result,
_ => false,
cfg_match! {
feature = "std_web" => ({
let value: Value = js! { return confirm(@{message}); };
match value {
Value::Bool(result) => result,
_ => false,
}
}),
feature = "web_sys" => utils::window().confirm_with_message(message).unwrap(),
}
}
}
Loading