den_stdlib_console/
lib.rs1#[rquickjs::module(rename = "camelCase", rename_vars = "camelCase")]
2pub mod console {
3 use colored::Colorize;
4 use indexmap::indexmap;
5 use rquickjs::{module::Exports, Coerced, Ctx, IntoJs, Result};
6
7 #[rquickjs::function]
8 pub fn log(msg: Coerced<String>) {
9 println!("{}", msg.0);
10 }
11
12 #[rquickjs::function]
13 pub fn info(msg: Coerced<String>) {
14 println!("{}", msg.0.bright_cyan());
15 }
16
17 #[rquickjs::function]
18 pub fn warn(msg: Coerced<String>) {
19 println!("{}", msg.0.bright_yellow());
20 }
21
22 #[rquickjs::function]
23 pub fn error(msg: Coerced<String>) {
24 println!("{}", msg.0.bright_red());
25 }
26
27 #[qjs(evaluate)]
28 pub fn evaluate<'js>(ctx: &Ctx<'js>, _: &Exports<'js>) -> Result<()> {
29 ctx.globals().set(
30 "console",
31 indexmap! {
32 "log" => js_log.into_js(ctx)?,
33 "info" => js_info.into_js(ctx)?,
34 "warn" => js_warn.into_js(ctx)?,
35 "error" => js_error.into_js(ctx)?,
36 },
37 )?;
38 Ok(())
39 }
40}