Struct rsjsonnet_front::Session
source · pub struct Session { /* private fields */ }Implementations§
source§impl Session
impl Session
pub fn new() -> Self
sourcepub fn program_mut(&mut self) -> &mut Program
pub fn program_mut(&mut self) -> &mut Program
Returns a mutable reference to the underlying Program.
sourcepub fn set_max_trace(&mut self, max_trace: usize)
pub fn set_max_trace(&mut self, max_trace: usize)
Sets the maximum number of stack trace items to print.
pub fn add_search_path(&mut self, path: PathBuf)
sourcepub fn add_native_func<const N: usize, F>(
&mut self,
name: &str,
params: &[&str; N],
func: F
)
pub fn add_native_func<const N: usize, F>( &mut self, name: &str, params: &[&str; N], func: F )
Adds a native function.
§Example
let mut session = rsjsonnet_front::Session::new();
session.add_native_func("MyFunc", &["arg"], |_, [arg]| {
let Some(arg) = arg.to_string() else {
return Err("expected a string".into());
};
// Count lowercase vowels.
let r = arg
.chars()
.filter(|chr| matches!(chr, 'a' | 'e' | 'i' | 'o' | 'u'))
.count();
Ok(rsjsonnet_lang::program::Value::number(r as f64))
});
let source = br#"local f = std.native("MyFunc"); f("hello world")"#;
let thunk = session
.load_virt_file("<example>", source.to_vec())
.unwrap();
let result = session.eval_value(&thunk).unwrap();
assert_eq!(result.as_number(), Some(3.0));sourcepub fn load_virt_file(
&mut self,
repr_path: &str,
data: Vec<u8>
) -> Option<Thunk>
pub fn load_virt_file( &mut self, repr_path: &str, data: Vec<u8> ) -> Option<Thunk>
Loads a file with the provided data.
repr_path is used to represent the file in error messages and for
std.thisFile.
In case of failure, the error is printed to stderr and None is
returned.
sourcepub fn load_real_file(&mut self, path: &Path) -> Option<Thunk>
pub fn load_real_file(&mut self, path: &Path) -> Option<Thunk>
Loads a file from the filesystem.
In case of failure, the error is printed to stderr and None is
returned.
sourcepub fn eval_value(&mut self, thunk: &Thunk) -> Option<Value>
pub fn eval_value(&mut self, thunk: &Thunk) -> Option<Value>
Evaluates a thunk.
In case of failure, the error is printed to stderr and None is
returned.
sourcepub fn eval_call(
&mut self,
func: &Thunk,
pos_args: &[Thunk],
named_args: &[(InternedStr, Thunk)]
) -> Option<Value>
pub fn eval_call( &mut self, func: &Thunk, pos_args: &[Thunk], named_args: &[(InternedStr, Thunk)] ) -> Option<Value>
Evaluates a function call.
In case of failure, the error is printed to stderr and None is
returned.
sourcepub fn manifest_json(
&mut self,
value: &Value,
multiline: bool
) -> Option<String>
pub fn manifest_json( &mut self, value: &Value, multiline: bool ) -> Option<String>
Marshals a value as JSON.
In case of failure, the error is printed to stderr and None is
returned.
pub fn print_error(&self, msg: &str)
pub fn print_note(&self, msg: &str)
sourcepub fn push_custom_stack_trace_item(&mut self, text: String)
pub fn push_custom_stack_trace_item(&mut self, text: String)
Pushes a custom item that will be included at the end of stack straces.
sourcepub fn pop_custom_stack_trace_item(&mut self)
pub fn pop_custom_stack_trace_item(&mut self)
Removes the last item pushed with
Session::push_custom_stack_trace_item.