{"id":13648541,"url":"https://github.com/a-poor/js-in-rs","last_synced_at":"2025-08-03T13:30:36.770Z","repository":{"id":161749977,"uuid":"631340633","full_name":"a-poor/js-in-rs","owner":"a-poor","description":"A demo of using JavaScript in a Rust program, via the \"deno_core\" crate.","archived":false,"fork":false,"pushed_at":"2023-05-04T19:19:11.000Z","size":12,"stargazers_count":40,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-29T11:01:11.744Z","etag":null,"topics":["cli","demo","deno","javascript","rust"],"latest_commit_sha":null,"homepage":"https://austinpoor.com/blog/js-in-rs","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/a-poor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-04-22T17:59:48.000Z","updated_at":"2024-10-31T19:08:17.000Z","dependencies_parsed_at":"2024-01-14T10:59:27.758Z","dependency_job_id":"c865a3ce-c000-40b2-b21a-a395e31a2827","html_url":"https://github.com/a-poor/js-in-rs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a-poor%2Fjs-in-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a-poor%2Fjs-in-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a-poor%2Fjs-in-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a-poor%2Fjs-in-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/a-poor","download_url":"https://codeload.github.com/a-poor/js-in-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228544799,"owners_count":17934648,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","demo","deno","javascript","rust"],"created_at":"2024-08-02T01:04:20.157Z","updated_at":"2024-12-07T00:44:05.260Z","avatar_url":"https://github.com/a-poor.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# js-in-rs\n\n_created by Austin Poor_\n\nA demo of using JavaScript in a Rust program, via the [deno_core](https://docs.rs/deno_core/) crate.\n\n## Usage\n\n`js-in-rs` is a sample `grep`-like application, written in Rust, where a JavaScript filter is applied to each\nline of an input file to determine if it should be printed to `stdout`.\n\nThe CLI is written using [clap](https://docs.rs/clap/). Here's what the (very minimal) help output looks like:\n\n```sh\n$ js-in-rs --help\nUsage: js-in-rs \u003cFILE_PATH\u003e \u003cJS_MATCHER\u003e\n\nArguments:\n  \u003cFILE_PATH\u003e   Path to the file to be read\n  \u003cJS_MATCHER\u003e  JS matcher to be used\n\nOptions:\n  -h, --help  Print help\n```\n\nThe filter code is used to create a JS function that will be applied to each line in the input file.\n\nFor example, if you wanted to only print lines with more than 20 characters, you could use the filter\n`line.length \u003e 20` which would be formatted as a JS function `(line) =\u003e line.length \u003e 20` and then\napplied to the line as `!!(line) =\u003e line.length \u003e 20)(\"...\")`.\n\nHere's an example of running the app to only display lines with between 20 and 50 characters, excluding\nleading and trailing whitespace.\n\n```sh\n$ js-in-rs src/main.rs \"line.trim().length \u003e 20 \u0026\u0026 line.trim().length \u003c 50\"\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eOutput\u003c/summary\u003e\n\n```\nuse anyhow::{Result, Error};\nuse deno_core::JsRuntime;\nuse deno_core::RuntimeOptions;\n#[derive(Parser, Debug)]\n    /// Path to the file to be read\n    /// JS matcher to be used\nfn main() -\u003e Result\u003c()\u003e {\n    // Parse the arguments...\n    let args = Args::parse();\n    // Does the path exist?\n        return Err(Error::msg(\"File does not exist!\"));\n    // Validate the JS matcher...\n    if args.js_matcher.trim().is_empty() {\n    // Read in the file...\n    // Parse the matcher...\n    let mut runtime = JsRuntime::new(\n        RuntimeOptions::default(),\n    for line in raw.lines() {\n        // Serialize the line...\n        let s = serde_json::to_string(\u0026line)?;\n        let js_matcher = format!(\n            \"!!((line) =\u003e {})({})\",\n            args.js_matcher.clone().trim(),\n        let result = runtime.execute_script(\n                let scope = \u0026mut runtime.handle_scope();\n                let local = v8::Local::new(scope, global);\n                match deserialized_value {\n                            serde_json::Value::Bool(b) =\u003e {\n                                    println!(\"{}\", line);\n                            _ =\u003e return Err(Error::msg(format!(\n                                \"JS matcher must return a boolean value!\",\n                    Err(err) =\u003e return Err(Error::msg(\n                        format!(\"Cannot deserialize value: {err:?}\"),\n```\n\u003c/details\u003e\n\n\nHere's another example where I've added a `;` in the middle of the `\u0026\u0026` operator, to show how errors are reported:\n\n```sh\n$ js-in-rs src/main.rs 'line.trim().length \u003e 20 \u0026;\u0026 line.trim().length \u003c 50'\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eOutput\u003c/summary\u003e\n\n```\nError: Eval error: Uncaught SyntaxError: Unexpected token ';'\n    at matcher.js:1:39\n```\n\u003c/details\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa-poor%2Fjs-in-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fa-poor%2Fjs-in-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa-poor%2Fjs-in-rs/lists"}