Merge pull request #1 from siku2/variadic-log

Make web-sys macros variadic
This commit is contained in:
Rob Watson 2020-09-16 09:29:30 +02:00 committed by GitHub
commit bff16f4e6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 53 deletions

View File

@ -16,10 +16,12 @@ doctest = true
[features]
default = ["web_sys"]
web_sys = ["web-sys", "paste"]
web_sys = ["js-sys", "paste", "wasm-bindgen", "web-sys"]
std_web = ["stdweb"]
[dependencies]
web-sys = { version = ">= 0.3", optional = true, features = ["console"] }
stdweb = { version = ">= 0.4", optional = true }
js-sys = { version = ">= 0.3", optional = true }
paste = { version = "1.0", optional = true }
stdweb = { version = ">= 0.4", optional = true }
wasm-bindgen = { version = ">= 0.2", optional = true }
web-sys = { version = ">= 0.3", optional = true, features = ["console"] }

View File

@ -77,4 +77,3 @@ The crate currently exposes the following macros:
## License
Licensed under MIT or Apache-2.

View File

@ -1,79 +1,105 @@
#[doc(hidden)]
pub extern crate web_sys;
use paste::paste;
#[doc(hidden)]
pub mod __macro {
pub use js_sys::Array;
pub use wasm_bindgen::JsValue;
pub use web_sys::console;
}
#[doc(hidden)]
#[macro_export]
macro_rules! __jsvalue {
($value: expr) => {
::std::convert::Into::<$crate::__macro::JsValue>::into($value)
};
}
macro_rules! websys_rules {
($level:ident) => {
// $dollar is a workaround to allow repetition in nested macros.
// It needs to be the token `$`.
($dollar:tt, $level:ident) => {
paste! {
#[doc = "Call the browser's `console." $level "()` function.\n\n\
The web-sys crate accepts up to a maximum of seven arguments, all of which must implement `Into<JsValue>`.\n\n\
The web-sys crate accepts any amount of arguments, all of which must implement `Into<JsValue>`.\n\n\
See the [wasm-bindgen documentation](../wasm_bindgen/struct.JsValue.html) for more information."]
#[macro_export]
macro_rules! [<console_ $level>] {
() => {
$crate::web_sys::console::[<$level _0>]()
$crate::__macro::console::[<$level _0>]()
};
($a:expr) => {
$crate::web_sys::console::[<$level _1>](&$a.into())
($a:expr $dollar(,)?) => {
$crate::__macro::console::[<$level _1>](&$crate::__jsvalue!($a))
};
($a:expr, $b:expr) => {
$crate::web_sys::console::[<$level _2>](&$a.into(), &$b.into())
($a:expr, $b:expr $dollar(,)?) => {
$crate::__macro::console::[<$level _2>](&$crate::__jsvalue!($a), &$crate::__jsvalue!($b))
};
($a:expr, $b:expr, $c:expr) => {
$crate::web_sys::console::[<$level _3>](&$a.into(), &$b.into(), &$c.into())
($a:expr, $b:expr, $c:expr $dollar(,)?) => {
$crate::__macro::console::[<$level _3>](&$crate::__jsvalue!($a), &$crate::__jsvalue!($b), &$crate::__jsvalue!($c))
};
($a:expr, $b:expr, $c:expr, $d:expr) => {
$crate::web_sys::console::[<$level _4>](&$a.into(), &$b.into(), &$c.into(), &$d.into())
};
($a:expr, $b:expr, $c:expr, $d:expr, $e:expr) => {
$crate::web_sys::console::[<$level _5>](
&$a.into(),
&$b.into(),
&$c.into(),
&$d.into(),
&$e.into(),
($a:expr, $b:expr, $c:expr, $d:expr $dollar(,)?) => {
$crate::__macro::console::[<$level _4>](
&$crate::__jsvalue!($a),
&$crate::__jsvalue!($b),
&$crate::__jsvalue!($c),
&$crate::__jsvalue!($d),
)
};
($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr) => {
$crate::web_sys::console::[<$level _6>](
&$a.into(),
&$b.into(),
&$c.into(),
&$d.into(),
&$e.into(),
&$f.into(),
($a:expr, $b:expr, $c:expr, $d:expr, $e:expr $dollar(,)?) => {
$crate::__macro::console::[<$level _5>](
&$crate::__jsvalue!($a),
&$crate::__jsvalue!($b),
&$crate::__jsvalue!($c),
&$crate::__jsvalue!($d),
&$crate::__jsvalue!($e),
)
};
($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr) => {
$crate::web_sys::console::[<$level _7>](
&$a.into(),
&$b.into(),
&$c.into(),
&$d.into(),
&$e.into(),
&$f.into(),
&$g.into(),
($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr $dollar(,)?) => {
$crate::__macro::console::[<$level _6>](
&$crate::__jsvalue!($a),
&$crate::__jsvalue!($b),
&$crate::__jsvalue!($c),
&$crate::__jsvalue!($d),
&$crate::__jsvalue!($e),
&$crate::__jsvalue!($f),
)
};
($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr $dollar(,)?) => {
$crate::__macro::console::[<$level _7>](
&$crate::__jsvalue!($a),
&$crate::__jsvalue!($b),
&$crate::__jsvalue!($c),
&$crate::__jsvalue!($d),
&$crate::__jsvalue!($e),
&$crate::__jsvalue!($f),
&$crate::__jsvalue!($g),
)
};
($dollar($dollar item:expr),+ $dollar(,)?) => {
{
let args = ::std::vec![$dollar($crate::__jsvalue!($dollar item)),+];
let args = ::std::iter::IntoIterator::into_iter(args).collect::<$crate::__macro::Array>();
$crate::__macro::console::$level(&args)
}
};
}
}
}
}
websys_rules!(debug);
websys_rules!(dir);
websys_rules!(dirxml);
websys_rules!(error);
websys_rules!(info);
websys_rules!(log);
websys_rules!(trace);
websys_rules!(warn);
websys_rules!($, debug);
websys_rules!($, dir);
websys_rules!($, dirxml);
websys_rules!($, error);
websys_rules!($, info);
websys_rules!($, log);
websys_rules!($, trace);
websys_rules!($, warn);
#[doc = "Call the browser's `console.clear()` function."]
#[macro_export]
macro_rules! console_clear {
() => {
$crate::web_sys::console::clear()
$crate::__macro::console::clear()
};
}

View File

@ -48,7 +48,8 @@
//! 2.0,
//! 3f32,
//! true,
//! false
//! false,
//! Some("option"),
//! );
//! # }
//! ```