-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Daniel Egger <[email protected]>
- Loading branch information
1 parent
41b3708
commit 5900d11
Showing
5 changed files
with
3,539 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
authors = ["Daniel Egger <[email protected]>", "Axiros GmbH"] | ||
description = "The Rust USP toolkit for Rhai" | ||
edition = "2021" | ||
license = "BSD-3-Clause" | ||
name = "rhai-rusp" | ||
readme = "README.md" | ||
repository = "https://github.com/axiros/rusp" | ||
version = "0.95.0" | ||
|
||
[badges] | ||
[badges.travis-ci] | ||
repository = "axiros/rusp" | ||
|
||
[dependencies] | ||
rhai = { workspace = true } | ||
anyhow = { workspace = true } | ||
clap = { version = "4", features = ["derive"] } | ||
quick-protobuf = { workspace = true } | ||
rusp-lib = { workspace = true } | ||
rhai-rusp-lib = { workspace = true } | ||
serde = { workspace = true } | ||
serde_derive = { workspace = true } | ||
serde_json = { workspace = true } | ||
|
||
[[bin]] | ||
name = "rusp-run" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
use clap::Parser; | ||
use rhai::packages::Package; | ||
use rhai::{Engine, EvalAltResult, Position}; | ||
use rhai_rusp_lib::RuspPackage; | ||
|
||
use std::convert::Into; | ||
use std::path::PathBuf; | ||
use std::{fs::File, io::Read, path::Path, process::exit}; | ||
|
||
fn eprint_error(input: &str, mut err: EvalAltResult) { | ||
fn eprint_line(lines: &[&str], pos: Position, err_msg: &str) { | ||
let line = pos.line().unwrap(); | ||
let line_no = format!("{line}: "); | ||
|
||
eprintln!("{line_no}{}", lines[line - 1]); | ||
|
||
for (i, err_line) in err_msg.to_string().lines().enumerate() { | ||
// Display position marker | ||
println!( | ||
"{0:>1$}{err_line}", | ||
if i > 0 { "| " } else { "^ " }, | ||
line_no.len() + pos.position().unwrap() + 1, | ||
); | ||
} | ||
eprintln!(); | ||
} | ||
|
||
let lines: Vec<_> = input.lines().collect(); | ||
|
||
// Print error | ||
let pos = err.take_position(); | ||
|
||
if pos.is_none() { | ||
// No position | ||
eprintln!("{err}"); | ||
} else { | ||
// Specific position | ||
eprint_line(&lines, pos, &err.to_string()); | ||
} | ||
} | ||
|
||
#[derive(Parser)] | ||
#[command( | ||
author, | ||
version, | ||
name = "rusp-run", | ||
about = "the Rust USP toolkit, rhai runner" | ||
)] | ||
struct Rusp { | ||
#[arg(long = "script", short = 's')] | ||
/// Inline rhai script | ||
script: Option<String>, | ||
/// Output filename of file to encode USP Protobuf message to | ||
filename: Option<PathBuf>, | ||
} | ||
|
||
fn main() { | ||
let args = Rusp::parse(); | ||
|
||
// Initialize scripting engine | ||
let mut engine = Engine::new(); | ||
|
||
// Create rusp package and add the package into the engine | ||
engine.register_static_module("rusp", RuspPackage::new().as_shared_module()); | ||
engine.set_optimization_level(rhai::OptimizationLevel::Simple); | ||
|
||
if let Some(filename) = args.filename { | ||
let mut contents = String::new(); | ||
let filename = match Path::new(&filename).canonicalize() { | ||
Err(err) => { | ||
eprintln!("Error script file path: {filename:?}\n{err}"); | ||
exit(1); | ||
} | ||
Ok(f) => match f.strip_prefix(std::env::current_dir().unwrap().canonicalize().unwrap()) | ||
{ | ||
Ok(f) => f.into(), | ||
_ => f, | ||
}, | ||
}; | ||
|
||
let mut f = match File::open(&filename) { | ||
Err(err) => { | ||
eprintln!( | ||
"Error reading script file: {}\n{}", | ||
filename.to_string_lossy(), | ||
err | ||
); | ||
exit(1); | ||
} | ||
Ok(f) => f, | ||
}; | ||
|
||
if let Err(err) = f.read_to_string(&mut contents) { | ||
eprintln!( | ||
"Error reading script file: {}\n{}", | ||
filename.to_string_lossy(), | ||
err | ||
); | ||
exit(1); | ||
} | ||
|
||
let contents = if contents.starts_with("#!") { | ||
// Skip shebang | ||
&contents[contents.find('\n').unwrap_or(0)..] | ||
} else { | ||
&contents[..] | ||
}; | ||
|
||
if let Err(err) = engine | ||
.compile(contents) | ||
.map_err(Into::into) | ||
.and_then(|mut ast| { | ||
ast.set_source(filename.to_string_lossy().to_string()); | ||
engine.run_ast(&ast) | ||
}) | ||
{ | ||
let filename = filename.to_string_lossy(); | ||
|
||
eprintln!("{:=<1$}", "", filename.len()); | ||
eprintln!("{filename}"); | ||
eprintln!("{:=<1$}", "", filename.len()); | ||
eprintln!(); | ||
|
||
eprint_error(contents, *err); | ||
} | ||
} else if let Some(contents) = args.script { | ||
let filename = "<script>"; | ||
|
||
if let Err(err) = engine | ||
.compile(&contents) | ||
.map_err(Into::into) | ||
.and_then(|mut ast| { | ||
ast.set_source(filename); | ||
engine.run_ast(&ast) | ||
}) | ||
{ | ||
eprintln!("{:=<1$}", "", filename.len()); | ||
eprintln!("{filename}"); | ||
eprintln!("{:=<1$}", "", filename.len()); | ||
eprintln!(); | ||
|
||
eprint_error(&contents, *err); | ||
} | ||
} else { | ||
eprintln!("You will either have to supply a filename, or you can use the --script option"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "rhai-rusp-lib" | ||
version = "0.95.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
rhai = { workspace = true } | ||
rusp-lib = { workspace = true } | ||
anyhow = { workspace = true } | ||
quick-protobuf = { workspace = true } | ||
serde = { workspace = true } | ||
serde_derive = { workspace = true } | ||
serde_json = { workspace = true } |
Oops, something went wrong.