1#![allow(clippy::result_large_err)]
14#![warn(missing_docs)]
18
19mod compile;
20pub mod script;
21mod spec_build;
22
23pub use compile::{CompileError, compile};
24pub use ruso_runtime::{
25 BytecodeProgram, EvidenceKind, ExtractSource, QualifiedMatch, Severity, encode_bytecode,
26};
27pub use script::ast::{self, Program, Stmt};
28pub use script::{ParseError, parse};
29
30use std::path::Path;
31
32#[derive(Debug, thiserror::Error)]
35pub enum LoadError {
36 #[error("failed to read {}: {source}", path.display())]
38 Io {
39 path: std::path::PathBuf,
41 source: std::io::Error,
43 },
44 #[error("failed to parse {}: {source}", path.display())]
46 Parse {
47 path: std::path::PathBuf,
49 source: ParseError,
51 },
52}
53
54pub fn load_program(path: &Path) -> Result<Program, LoadError> {
56 let source = std::fs::read_to_string(path).map_err(|err| LoadError::Io {
57 path: path.to_path_buf(),
58 source: err,
59 })?;
60 parse(&source).map_err(|err| LoadError::Parse {
61 path: path.to_path_buf(),
62 source: err,
63 })
64}
65
66pub fn compile_program(program: &Program) -> Result<BytecodeProgram, CompileError> {
68 compile(program)
69}
70
71pub fn compile_to_bytes(program: &Program) -> Result<Vec<u8>, CompileError> {
73 Ok(encode_bytecode(&compile_program(program)?))
74}
75
76pub async fn run(
78 program: &Program,
79 config: ruso_runtime::ExecutorConfig,
80) -> Result<ruso_runtime::ExecutionResult, ruso_runtime::RuntimeError> {
81 let bytecode =
82 compile_program(program).map_err(|e| ruso_runtime::RuntimeError::Other(e.to_string()))?;
83 ruso_runtime::Executor::from_bytecode(config, bytecode)?
84 .run()
85 .await
86}
87
88pub async fn run_bytecode(
90 bytecode: &BytecodeProgram,
91 config: ruso_runtime::ExecutorConfig,
92) -> Result<ruso_runtime::ExecutionResult, ruso_runtime::RuntimeError> {
93 ruso_runtime::Executor::from_bytecode(config, bytecode.clone())?
94 .run()
95 .await
96}
97
98pub async fn run_program(
104 bytecode: std::sync::Arc<BytecodeProgram>,
105 config: ruso_runtime::ExecutorConfig,
106) -> Result<ruso_runtime::ExecutionResult, ruso_runtime::RuntimeError> {
107 ruso_runtime::Executor::from_program(config, bytecode)?
108 .run()
109 .await
110}
111
112pub async fn run_bytes(
114 bytes: &[u8],
115 config: ruso_runtime::ExecutorConfig,
116) -> Result<ruso_runtime::ExecutionResult, ruso_runtime::RuntimeError> {
117 ruso_runtime::Executor::from_bytes(config, bytes)?
118 .run()
119 .await
120}
121
122#[cfg(test)]
123mod tests {
124 use std::path::PathBuf;
125
126 use ruso_runtime::{bytes_to_hex, decode_bytecode, hex_to_bytes};
127
128 use super::*;
129 use crate::script::ast::Stmt;
130
131 #[test]
132 fn load_program_valid_example() {
133 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/http_status_ok.rsl");
134 let program = load_program(&path).expect("example script should parse");
135 assert!(!program.statements.is_empty());
136 }
137
138 #[test]
139 fn bytecode_roundtrip_http_example() {
140 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/http_status_ok.rsl");
141 let program = load_program(&path).unwrap();
142 let original = compile_program(&program).unwrap();
143 let bytes = encode_bytecode(&original);
144 let restored = decode_bytecode(&bytes).unwrap();
145 assert_eq!(original.code, restored.code);
146 assert_eq!(original.strings, restored.strings);
147 assert_eq!(original.matchers, restored.matchers);
148 }
149
150 #[test]
151 fn hex_bytes_roundtrip() {
152 let program = Program {
153 statements: vec![Stmt::Name("Hex test".into()), Stmt::Severity(Severity::Low)],
154 };
155 let bytes = compile_to_bytes(&program).unwrap();
156 let hex = bytes_to_hex(&bytes);
157 let restored = hex_to_bytes(&hex).expect("hex decode");
158 let decoded = decode_bytecode(&restored).expect("bytecode decode");
159 assert_eq!(decoded.spec.metadata.name.as_deref(), Some("Hex test"));
160 }
161}