support any number of arguments

This commit is contained in:
Simon Berger 2020-09-15 16:48:32 +02:00
parent fbc37f48f7
commit e9876577fe
No known key found for this signature in database
GPG Key ID: BFC5D85EC64F3B8C
4 changed files with 81 additions and 53 deletions

View File

@ -16,10 +16,12 @@ doctest = true
[features] [features]
default = ["web_sys"] default = ["web_sys"]
web_sys = ["web-sys", "paste"] web_sys = ["js-sys", "paste", "wasm-bindgen", "web-sys"]
std_web = ["stdweb"] std_web = ["stdweb"]
[dependencies] [dependencies]
web-sys = { version = ">= 0.3", optional = true, features = ["console"] } js-sys = { version = ">= 0.3", optional = true }
stdweb = { version = ">= 0.4", optional = true }
paste = { version = "1.0", 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 ## License
Licensed under MIT or Apache-2. Licensed under MIT or Apache-2.

View File

@ -1,79 +1,105 @@
#[doc(hidden)]
pub extern crate web_sys;
use paste::paste; 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 { 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! { paste! {
#[doc = "Call the browser's `console." $level "()` function.\n\n\ #[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."] See the [wasm-bindgen documentation](../wasm_bindgen/struct.JsValue.html) for more information."]
#[macro_export] #[macro_export]
macro_rules! [<console_ $level>] { macro_rules! [<console_ $level>] {
() => { () => {
$crate::web_sys::console::[<$level _0>]() $crate::__macro::console::[<$level _0>]()
}; };
($a:expr) => { ($a:expr $dollar(,)?) => {
$crate::web_sys::console::[<$level _1>](&$a.into()) $crate::__macro::console::[<$level _1>](&$crate::__jsvalue!($a))
}; };
($a:expr, $b:expr) => { ($a:expr, $b:expr $dollar(,)?) => {
$crate::web_sys::console::[<$level _2>](&$a.into(), &$b.into()) $crate::__macro::console::[<$level _2>](&$crate::__jsvalue!($a), &$crate::__jsvalue!($b))
}; };
($a:expr, $b:expr, $c:expr) => { ($a:expr, $b:expr, $c:expr $dollar(,)?) => {
$crate::web_sys::console::[<$level _3>](&$a.into(), &$b.into(), &$c.into()) $crate::__macro::console::[<$level _3>](&$crate::__jsvalue!($a), &$crate::__jsvalue!($b), &$crate::__jsvalue!($c))
}; };
($a:expr, $b:expr, $c:expr, $d:expr) => { ($a:expr, $b:expr, $c:expr, $d:expr $dollar(,)?) => {
$crate::web_sys::console::[<$level _4>](&$a.into(), &$b.into(), &$c.into(), &$d.into()) $crate::__macro::console::[<$level _4>](
}; &$crate::__jsvalue!($a),
($a:expr, $b:expr, $c:expr, $d:expr, $e:expr) => { &$crate::__jsvalue!($b),
$crate::web_sys::console::[<$level _5>]( &$crate::__jsvalue!($c),
&$a.into(), &$crate::__jsvalue!($d),
&$b.into(),
&$c.into(),
&$d.into(),
&$e.into(),
) )
}; };
($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr) => { ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr $dollar(,)?) => {
$crate::web_sys::console::[<$level _6>]( $crate::__macro::console::[<$level _5>](
&$a.into(), &$crate::__jsvalue!($a),
&$b.into(), &$crate::__jsvalue!($b),
&$c.into(), &$crate::__jsvalue!($c),
&$d.into(), &$crate::__jsvalue!($d),
&$e.into(), &$crate::__jsvalue!($e),
&$f.into(),
) )
}; };
($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr) => { ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr $dollar(,)?) => {
$crate::web_sys::console::[<$level _7>]( $crate::__macro::console::[<$level _6>](
&$a.into(), &$crate::__jsvalue!($a),
&$b.into(), &$crate::__jsvalue!($b),
&$c.into(), &$crate::__jsvalue!($c),
&$d.into(), &$crate::__jsvalue!($d),
&$e.into(), &$crate::__jsvalue!($e),
&$f.into(), &$crate::__jsvalue!($f),
&$g.into(),
) )
}; };
($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!($, debug);
websys_rules!(dir); websys_rules!($, dir);
websys_rules!(dirxml); websys_rules!($, dirxml);
websys_rules!(error); websys_rules!($, error);
websys_rules!(info); websys_rules!($, info);
websys_rules!(log); websys_rules!($, log);
websys_rules!(trace); websys_rules!($, trace);
websys_rules!(warn); websys_rules!($, warn);
#[doc = "Call the browser's `console.clear()` function."] #[doc = "Call the browser's `console.clear()` function."]
#[macro_export] #[macro_export]
macro_rules! console_clear { macro_rules! console_clear {
() => { () => {
$crate::web_sys::console::clear() $crate::__macro::console::clear()
}; };
} }

View File

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