A demo of using JavaScript in a Rust program, via the "deno_core" crate.

Overview

js-in-rs

created by Austin Poor

A demo of using JavaScript in a Rust program, via the deno_core crate.

Usage

js-in-rs is a sample grep-like application, written in Rust, where a JavaScript filter is applied to each line of an input file to determine if it should be printed to stdout.

The CLI is written using clap. Here's what the (very minimal) help output looks like:

$ js-in-rs --help
Usage: js-in-rs <FILE_PATH> <JS_MATCHER>

Arguments:
  <FILE_PATH>   Path to the file to be read
  <JS_MATCHER>  JS matcher to be used

Options:
  -h, --help  Print help

The filter code is used to create a JS function that will be applied to each line in the input file.

For example, if you wanted to only print lines with more than 20 characters, you could use the filter line.length > 20 which would be formatted as a JS function (line) => line.length > 20 and then applied to the line as !!(line) => line.length > 20)("...").

Here's an example of running the app to only display lines with between 20 and 50 characters, excluding leading and trailing whitespace.

$ js-in-rs src/main.rs "line.trim().length > 20 && line.trim().length < 50"
Output
use anyhow::{Result, Error};
use deno_core::JsRuntime;
use deno_core::RuntimeOptions;
#[derive(Parser, Debug)]
    /// Path to the file to be read
    /// JS matcher to be used
fn main() -> Result<()> {
    // Parse the arguments...
    let args = Args::parse();
    // Does the path exist?
        return Err(Error::msg("File does not exist!"));
    // Validate the JS matcher...
    if args.js_matcher.trim().is_empty() {
    // Read in the file...
    // Parse the matcher...
    let mut runtime = JsRuntime::new(
        RuntimeOptions::default(),
    for line in raw.lines() {
        // Serialize the line...
        let s = serde_json::to_string(&line)?;
        let js_matcher = format!(
            "!!((line) => {})({})",
            args.js_matcher.clone().trim(),
        let result = runtime.execute_script(
                let scope = &mut runtime.handle_scope();
                let local = v8::Local::new(scope, global);
                match deserialized_value {
                            serde_json::Value::Bool(b) => {
                                    println!("{}", line);
                            _ => return Err(Error::msg(format!(
                                "JS matcher must return a boolean value!",
                    Err(err) => return Err(Error::msg(
                        format!("Cannot deserialize value: {err:?}"),

Here's another example where I've added a ; in the middle of the && operator, to show how errors are reported:

$ js-in-rs src/main.rs 'line.trim().length > 20 &;& line.trim().length < 50'
Output
Error: Eval error: Uncaught SyntaxError: Unexpected token ';'
    at matcher.js:1:39
You might also like...
A Vulkan renderer for egui using Ash.
A Vulkan renderer for egui using Ash.

egui-ash-renderer A Vulkan renderer for egui using Ash. This is meant to add support for egui in your existing Vulkan/ash applications. Not a full efr

A simple, cross-platform GUI automation module for Rust.

AutoPilot AutoPilot is a Rust port of the Python C extension AutoPy, a simple, cross-platform GUI automation library for Python. For more information,

A data-first Rust-native UI design toolkit.
A data-first Rust-native UI design toolkit.

Druid A data-first Rust-native UI toolkit. Druid is an experimental Rust-native UI toolkit. Its main goal is to offer a polished user experience. Ther

The Rust UI-Toolkit.
The Rust UI-Toolkit.

The Orbital Widget Toolkit is a cross-platform (G)UI toolkit for building scalable user interfaces with the programming language Rust. It's based on t

An easy-to-use, 2D GUI library written entirely in Rust.

Conrod An easy-to-use, 2D GUI library written entirely in Rust. Guide What is Conrod? A Brief Summary Screenshots and Videos Feature Overview Availabl

Rust bindings to Core Foundation and other low level libraries on Mac OS X and iOS

core-foundation-rs Compatibility Targets macOS 10.7 by default. To enable features added in macOS 10.8, set Cargo feature mac_os_10_8_features. To hav

Rust bindings for the FLTK GUI library.
Rust bindings for the FLTK GUI library.

fltk-rs Rust bindings for the FLTK Graphical User Interface library. The FLTK crate is a crossplatform lightweight gui library which can be statically

Build beautiful desktop apps with flutter and rust. 🌠 (wip)
Build beautiful desktop apps with flutter and rust. 🌠 (wip)

flutter-rs Build flutter desktop app in dart & rust. Get Started Install requirements Rust flutter sdk Develop install the cargo flutter command cargo

Idiomatic, GTK+-based, GUI library, inspired by Elm, written in Rust
Idiomatic, GTK+-based, GUI library, inspired by Elm, written in Rust

Relm Asynchronous, GTK+-based, GUI library, inspired by Elm, written in Rust. This library is in beta stage: it has not been thoroughly tested and its

Owner
Austin Poor
Full Stack Developer / Data Scientist
Austin Poor
SixtyFPS is a toolkit to efficiently develop fluid graphical user interfaces for any display: embedded devices and desktop applications. We support multiple programming languages, such as Rust, C++ or JavaScript.

SixtyFPS is a toolkit to efficiently develop fluid graphical user interfaces for any display: embedded devices and desktop applications. We support multiple programming languages, such as Rust, C++ or JavaScript.

SixtyFPS 5.5k Jan 1, 2023
Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.

Deno Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust. Features Secure by default. No file,

Derek Jones 2 Aug 13, 2022
rsautogui aims to be a cross-platform GUI automation rust crate.

rsautogui rsautogui aims to be a cross-platform GUI automation rust crate. It lets you control the mouse and keyboard to automate interactions with ot

null 5 Sep 18, 2022
Crate for simple implementation of Component for Native API 1C:Enterprise written in rust

Гайд по использованию на русском языке можно посмотреть здесь и задать вопросы по использованию, но не оставляйте там комментарии об ошибках, т.к. там

Maxim Kozlov 40 Sep 30, 2023
Neovim GUI written in Rust, using relm4 and gtk4-rs

Reovim Neovim GUI written in Rust, using relm4 and gtk4-rs. Thanks Neovide Configuration To setup font add next line to init.vim set guifont=Cascadia\

songww 70 Dec 13, 2022
An Anime Game Launcher variant written on Rust, GTK4 and libadwaita, using Anime Game Core library

An Anime Game Launcher GTK The launcher variant written on Rust, GTK4 and libadwaita, using Anime Game Core library You could also try the main branch

An Anime Team 77 Jan 9, 2023
Honkers Launcher variant written on Rust, GTK4 and libadwaita, using Anime Game Core library

You could also try the main branch Development Folder Description ui Blueprint UI files ui/.dist UI files compiled by the blueprint src Rust source co

An Anime Team 9 Nov 2, 2022
An example of searching iBeacon using gtk-rs and btleplug.

Beacon Searcher Screenshot Compile & Run Install GTK 3 dev packages: macOS: $ brew install gtk+3 $ brew install gnome-icon-theme Debian / Ubuntu: $ s

Ling, Wei-Cheng 0 Dec 21, 2021
Egui node graph is a featureful, customizable library to create node graph applications using egui

Egui node graph is a featureful, customizable library to create node graph applications using egui. The library takes care of presenting a node graph to your users, and allows customizing many aspects of the interaction, creating the semantics you want for your specific application.

null 367 Jan 8, 2023
An easy to use command line project manager for projects using the ReCT programming language

☢️ A powerful project manager for the ReCT programming language! ☢️ ReCTx makes your projects easier to manage by allowing you to configure everything

Remy 1 Nov 28, 2022