Compare commits

..

1 Commits

Author SHA1 Message Date
Rob Watson 90c504d025 WIP: badges 2020-10-19 17:58:38 +02:00
9 changed files with 107 additions and 525 deletions

View File

@ -1,10 +1,10 @@
[package]
name = "weblog"
version = "0.4.0-beta.1"
authors = ["Rob Watson"]
version = "0.3.0"
authors = ["Rob Watson <rfwatson@users.noreply.github.com>"]
edition = "2018"
description = "weblog is a crate that defines a set of macros for calling `console.log()` and other members of the browser's console API when targeting Wasm."
repository = "https://git.netflux.io/rob/weblog"
repository = "https://github.com/rfwatson/weblog"
keywords = ["wasm", "webassembly", "console", "log", "logging"]
license = "MIT OR Apache-2.0"
@ -20,6 +20,6 @@ std_web = ["stdweb"]
[dependencies]
stdweb = { version = ">= 0.4", optional = true }
weblog-proc-macro = { path = "./weblog-proc-macro", version = "0.4.0-beta.1", optional = true }
weblog-proc-macro = { path = "./weblog-proc-macro", version = "0.3.0", optional = true }
web-sys = { version = "0.3", features = ["console"], optional = true }
wasm-bindgen = { version = "0.2", optional = true }

View File

@ -1,8 +1,7 @@
[package]
name = "weblog-example-web-sys"
version = "0.4.0-beta.1"
repository = "https://git.netflux.io/rob/weblog"
authors = ["Rob Watson"]
version = "0.3.0"
authors = ["Rob Watson <rfwatson@users.noreply.github.com>"]
edition = "2018"
[lib]
@ -10,3 +9,4 @@ crate-type = ["cdylib"]
[dependencies]
weblog = { path = "../.." }
wasm-bindgen = "0.2"

View File

@ -10,20 +10,14 @@ pub fn main() {
console_log!("Hello", "there", "world");
// Various types.
console_log!("f32", 1f32, "f64", 2f64);
console_log!("u8", 3u8, "i8", 4i8);
console_log!("u16", 5u16, "i16", 6i16);
console_log!("u32", 7u32, "i32", 8i8);
//console_log!("u64", 9u64, "i64", 10i64);
console_log!("usize", 11usize, "isize", 12isize);
// More types.
console_log!(
1.0,
2f64,
true,
"&str",
String::from("owned string"),
Some("an option"),
(None as Option<i32>),
None as Option<i32>,
);
// Various levels.

View File

@ -1,4 +1,2 @@
#[cfg(feature = "std_web")]
pub mod std_web;
#[cfg(feature = "web_sys")]
pub mod web_sys;

View File

@ -1,347 +0,0 @@
#[doc(hidden)]
pub use ::wasm_bindgen;
#[doc(hidden)]
pub use ::web_sys;
use ::wasm_bindgen::JsValue;
pub struct ConsoleArg(JsValue);
impl ConsoleArg {
pub fn into_inner(self) -> JsValue {
self.0
}
}
use crate::ToConsole;
macro_rules! into_jsvalue {
($($n:ident),*) => ($(
impl ToConsole for $n {
#[inline]
fn to_console(self) -> ConsoleArg {
ConsoleArg(self.into())
}
}
)*)
}
// TODO u64 and f64?
// TODO: impl<'a, T> From<&'a T> for JsValue where T: JsCast ?
into_jsvalue!(bool, f32, f64, i8, i16, i32, u8, u16, u32, String);
impl<'a> ToConsole for &'a String {
fn to_console(self) -> ConsoleArg {
ConsoleArg(self.into())
}
}
impl<'a> ToConsole for &'a str {
fn to_console(self) -> ConsoleArg {
ConsoleArg(self.into())
}
}
impl<T> ToConsole for Option<T>
where
T: ToConsole,
{
fn to_console(self) -> ConsoleArg {
match self {
Some(s) => s.to_console(),
None => ConsoleArg(JsValue::undefined()),
}
}
}
impl ToConsole for usize {
fn to_console(self) -> ConsoleArg {
// usize/isize is always 32-bit in Wasm.
// But perhaps this should have a guard of some kind.
ConsoleArg((self as u32).into())
}
}
impl ToConsole for isize {
fn to_console(self) -> ConsoleArg {
// As above.
ConsoleArg((self as i32).into())
}
}
// It is necessary to expose these wrapper macros in order to pass the $crate
// meta-variable to the proc macro. This in turn allows the macros to be
// re-exported from third-party crates without breakage.
//
// TODO: a declarative macro to remove the duplication here would be nice.
#[doc(hidden)]
pub use ::weblog_proc_macro::console_assert as __weblog_console_assert;
#[macro_export]
/// Call the browser's `console.assert()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/assert)
macro_rules! console_assert {
($($input:tt)*) => {
$crate::__weblog_console_assert! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_clear as __weblog_console_clear;
#[macro_export]
/// Call the browser's `console.clear()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/clear)
macro_rules! console_clear {
($($input:tt)*) => {
$crate::__weblog_console_clear! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_count as __weblog_console_count;
#[macro_export]
/// Call the browser's `console.count()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/count)
macro_rules! console_count {
($($input:tt)*) => {
$crate::__weblog_console_count! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_count_reset as __weblog_console_count_reset;
#[macro_export]
/// Call the browser's `console.count_reset()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/count_reset)
macro_rules! console_count_reset {
($($input:tt)*) => {
$crate::__weblog_console_count_reset! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_debug as __weblog_console_debug;
#[macro_export]
/// Call the browser's `console.debug()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/debug)
macro_rules! console_debug {
($($input:tt)*) => {
$crate::__weblog_console_debug! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_dir as __weblog_console_dir;
#[macro_export]
/// Call the browser's `console.dir()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/dir)
macro_rules! console_dir {
($($input:tt)*) => {
$crate::__weblog_console_dir! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_dirxml as __weblog_console_dirxml;
#[macro_export]
/// Call the browser's `console.dirxml()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/dirxml)
macro_rules! console_dirxml {
($($input:tt)*) => {
$crate::__weblog_console_dirxml! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_error as __weblog_console_error;
#[macro_export]
/// Call the browser's `console.error()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/error)
macro_rules! console_error {
($($input:tt)*) => {
$crate::__weblog_console_error! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_exception as __weblog_console_exception;
#[macro_export]
/// Call the browser's `console.exception()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/exception)
macro_rules! console_exception {
($($input:tt)*) => {
$crate::__weblog_console_exception! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_info as __weblog_console_info;
#[macro_export]
/// Call the browser's `console.info()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/info)
macro_rules! console_info {
($($input:tt)*) => {
$crate::__weblog_console_info! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_log as __weblog_console_log;
#[macro_export]
/// Call the browser's `console.log()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/log)
macro_rules! console_log {
($($input:tt)*) => {
$crate::__weblog_console_log! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_table as __weblog_console_table;
#[macro_export]
/// Call the browser's `console.table()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/table)
macro_rules! console_table {
($($input:tt)*) => {
$crate::__weblog_console_table! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_time as __weblog_console_time;
#[macro_export]
/// Call the browser's `console.time()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time)
macro_rules! console_time {
($($input:tt)*) => {
$crate::__weblog_console_time! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_time_end as __weblog_console_time_end;
#[macro_export]
/// Call the browser's `console.time_end()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_end)
macro_rules! console_time_end {
($($input:tt)*) => {
$crate::__weblog_console_time_end! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_time_log as __weblog_console_time_log;
#[macro_export]
/// Call the browser's `console.time_log()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_log)
macro_rules! console_time_log {
($($input:tt)*) => {
$crate::__weblog_console_time_log! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_time_stamp as __weblog_console_time_stamp;
#[macro_export]
/// Call the browser's `console.time_stamp()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_stamp)
macro_rules! console_time_stamp {
($($input:tt)*) => {
$crate::__weblog_console_time_stamp! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_trace as __weblog_console_trace;
#[macro_export]
/// Call the browser's `console.trace()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/trace)
macro_rules! console_trace {
($($input:tt)*) => {
$crate::__weblog_console_trace! {
#![crate = $crate]
$($input)*
}
};
}
#[doc(hidden)]
pub use ::weblog_proc_macro::console_warn as __weblog_console_warn;
#[macro_export]
/// Call the browser's `console.warn()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/warn)
macro_rules! console_warn {
($($input:tt)*) => {
$crate::__weblog_console_warn! {
#![crate = $crate]
$($input)*
}
};
}

View File

@ -74,14 +74,14 @@
//! ```toml
//! weblog = { version = "0.3", default-features = false, features = ["stdweb"] }
//! ```
//!
mod console;
pub trait ToConsole {
fn to_console(self) -> ConsoleArg;
}
#[cfg(feature = "web_sys")]
pub use weblog_proc_macro::*;
#[cfg(feature = "web_sys")]
pub use ::web_sys;
#[cfg(feature = "std_web")]
pub use self::console::std_web::*;
#[cfg(feature = "web_sys")]
pub use self::console::web_sys::*;

View File

@ -1,8 +1,8 @@
[package]
name = "weblog-proc-macro"
version = "0.4.0-beta.1"
authors = ["Rob Watson"]
repository = "https://git.netflux.io/rob/weblog"
version = "0.3.0"
authors = ["Rob Watson <rfwatson@users.noreply.github.com>"]
repository = "https://github.com/rfwatson/weblog"
description = "weblog is a crate that defines a set of macros for calling `console.log()` and other members of the browser's console API when targeting Wasm."
edition = "2018"
keywords = ["wasm", "webassembly", "console", "log", "logging"]

View File

@ -1,46 +1,34 @@
//! weblog-proc-macro contains procedural macro definitions for the
//! [`weblog`](https://crates.io/crates/weblog) crate. See its documentation for more information.
extern crate proc_macro2;
mod weblog_impl;
use proc_macro::TokenStream;
use std::iter;
use syn::parse_macro_input;
use weblog_impl::{quote_console_func, ArgMode, ConsoleFunc, InputTokens};
use weblog_impl::{quote_console_func, ArgMode, ConsoleFunc};
#[doc(hidden)]
/// Call the browser's `console.assert()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/assert)
#[proc_macro]
pub fn console_assert(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("assert_with_condition_and_data", 1),
iter::once(ArgMode::PassThrough).chain(iter::repeat(ArgMode::Console)),
crate_name,
args,
iter::once(ArgMode::PassThrough).chain(iter::repeat(ArgMode::IntoJsValue)),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.clear()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/clear)
#[proc_macro]
pub fn console_clear(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::fixed("clear", 0),
iter::empty(),
crate_name,
args,
)
quote_console_func(ConsoleFunc::fixed("clear", 0), iter::empty(), input)
}
#[doc(hidden)]
/// Call the browser's `console.count()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/count)
#[proc_macro]
pub fn console_count(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
let name = if input.is_empty() {
"count"
} else {
"count_with_label"
@ -49,17 +37,15 @@ pub fn console_count(input: TokenStream) -> TokenStream {
quote_console_func(
ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::PassThrough),
crate_name,
args,
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.countReset()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/countReset)
#[proc_macro]
pub fn console_count_reset(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
let name = if input.is_empty() {
"count_reset"
} else {
"count_reset_with_label"
@ -68,121 +54,103 @@ pub fn console_count_reset(input: TokenStream) -> TokenStream {
quote_console_func(
ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::PassThrough),
crate_name,
args,
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.debug()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/debug)
#[proc_macro]
pub fn console_debug(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("debug", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.dir()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/dir)
#[proc_macro]
pub fn console_dir(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("dir", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.dirxml()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/dirxml)
#[proc_macro]
pub fn console_dirxml(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("dirxml", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.error()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/error)
#[proc_macro]
pub fn console_error(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("error", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.exception()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/exception)
#[proc_macro]
pub fn console_exception(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("exception", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.info()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/info)
#[proc_macro]
pub fn console_info(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("info", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.log()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/log)
#[proc_macro]
pub fn console_log(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("log", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.table()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/table)
#[proc_macro]
pub fn console_table(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("table", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.time()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/time)
#[proc_macro]
pub fn console_time(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
let name = if input.is_empty() {
"time"
} else {
"time_with_label"
@ -191,17 +159,15 @@ pub fn console_time(input: TokenStream) -> TokenStream {
quote_console_func(
ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::PassThrough),
crate_name,
args,
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.timeEnd()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/timeEnd)
#[proc_macro]
pub fn console_time_end(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
let name = if input.is_empty() {
"time_end"
} else {
"time_end_with_label"
@ -210,30 +176,26 @@ pub fn console_time_end(input: TokenStream) -> TokenStream {
quote_console_func(
ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::PassThrough),
crate_name,
args,
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.timeLog()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/timeLog)
#[proc_macro]
pub fn console_time_log(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("time_log_with_label_and_data", 1),
iter::once(ArgMode::PassThrough).chain(iter::repeat(ArgMode::Console)),
crate_name,
args,
iter::once(ArgMode::PassThrough).chain(iter::repeat(ArgMode::IntoJsValue)),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.timeStamp()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/timeStamp)
#[proc_macro]
pub fn console_time_stamp(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
let name = if args.is_empty() {
let name = if input.is_empty() {
"time_stamp"
} else {
"time_stamp_with_data"
@ -241,34 +203,29 @@ pub fn console_time_stamp(input: TokenStream) -> TokenStream {
quote_console_func(
ConsoleFunc::fixed(name, 1),
iter::once(ArgMode::Console),
crate_name,
args,
iter::once(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.trace()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/trace)
#[proc_macro]
pub fn console_trace(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("trace", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}
#[doc(hidden)]
/// Call the browser's `console.warn()` function.
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Console/warn)
#[proc_macro]
pub fn console_warn(input: TokenStream) -> TokenStream {
let InputTokens { crate_name, args } = parse_macro_input!(input as InputTokens);
quote_console_func(
ConsoleFunc::variadic("warn", 0),
iter::repeat(ArgMode::Console),
crate_name,
args,
iter::repeat(ArgMode::IntoJsValue),
input,
)
}

View File

@ -4,11 +4,7 @@ use quote::quote;
use std::cmp;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::{bracketed, Expr, Result, Token};
// The maximum number of variadic arguments accepted by web-sys function
// groups such as `console::log_n`:
const MAX_VARIADIC_ARGS: usize = 7;
use syn::{parse_macro_input, Expr, Result, Token};
// quote_console_func builds and quotes a call to the browser's console API, based on the
// provided console function and argument types and the input token stream provider by
@ -18,8 +14,7 @@ const MAX_VARIADIC_ARGS: usize = 7;
//
// * `func`: an object containing the target function name and parameter configuration.
// * `arg_modes`: an Iterator that will be called once per argument parsed from the input tokens.
// * `crate_name`: an identifier that resolves to the name of the calling crate.
// * `in_args`: zero or more punctuated expressions passed as arguments by the caller.
// * `input`: the raw input tokens, as provided by the macro caller.
pub fn quote_console_func(
ConsoleFunc {
name,
@ -27,9 +22,9 @@ pub fn quote_console_func(
is_variadic,
}: ConsoleFunc,
mut arg_modes: impl Iterator<Item = ArgMode>,
crate_name: Ident,
in_args: Punctuated<Expr, Token![,]>,
input: TokenStream,
) -> TokenStream {
let InArgs(in_args) = parse_macro_input!(input as InArgs);
let mut out_args = in_args
.iter()
.map(|arg| quote_arg(arg, arg_modes.next().unwrap_or_default()));
@ -39,7 +34,7 @@ pub fn quote_console_func(
let num_variadic = cmp::max(num_provided - num_fixed, 0);
let ident = {
let func_name = if is_variadic && num_variadic <= MAX_VARIADIC_ARGS {
let func_name = if is_variadic && num_variadic <= 7 {
format!("{}_{}", name, num_variadic)
} else {
name
@ -52,7 +47,7 @@ pub fn quote_console_func(
for _ in 0..num_fixed {
args.push(out_args.next().unwrap());
}
if num_variadic > MAX_VARIADIC_ARGS {
if num_variadic > 7 {
let variadic_args = out_args.collect::<Punctuated<_, Token![,]>>();
let ary = quote! {
&::std::iter::FromIterator::from_iter(::std::iter::IntoIterator::into_iter(::std::vec![#variadic_args]))
@ -65,19 +60,16 @@ pub fn quote_console_func(
}
}
(quote! { #crate_name::web_sys::console::#ident(#args) }).into()
(quote! { ::weblog::web_sys::console::#ident(#args) }).into()
}
#[inline]
fn quote_arg(arg: &Expr, arg_mode: ArgMode) -> TokenStream2 {
match arg_mode {
ArgMode::PassThrough => {
quote! { #arg }
}
ArgMode::Console => {
quote! {
&#arg.to_console().into_inner()
}
ArgMode::IntoJsValue => {
quote! { &::std::convert::Into::<::wasm_bindgen::JsValue>::into(#arg) }
}
}
}
@ -112,7 +104,7 @@ impl ConsoleFunc {
#[derive(Debug, Copy, Clone)]
pub enum ArgMode {
PassThrough,
Console,
IntoJsValue,
}
impl Default for ArgMode {
@ -121,24 +113,12 @@ impl Default for ArgMode {
}
}
pub struct InputTokens {
pub crate_name: Ident,
pub args: Punctuated<Expr, Token![,]>,
}
// InArgs is a struct required during parsing of input arguments.
struct InArgs(Punctuated<Expr, Token![,]>);
impl Parse for InputTokens {
impl Parse for InArgs {
fn parse(input: ParseStream) -> Result<Self> {
input.parse::<Token![#]>()?;
input.parse::<Token![!]>()?;
let bracketed;
bracketed!(bracketed in input);
bracketed.parse::<Token![crate]>()?;
bracketed.parse::<Token![ = ]>()?;
let crate_name = bracketed.parse::<Ident>()?;
let args: Punctuated<Expr, Token![,]> = Punctuated::parse_terminated(&input)?;
Ok(Self { crate_name, args })
let in_args: Punctuated<Expr, Token![,]> = Punctuated::parse_terminated(&input)?;
Ok(Self(in_args))
}
}