pub struct Session<'p> { /* private fields */ }Implementations§
Source§impl<'p> Session<'p>
impl<'p> Session<'p>
pub fn new(arena: &'p Arena) -> Self
Sourcepub fn program_mut(&mut self) -> &mut Program<'p>
pub fn program_mut(&mut self) -> &mut Program<'p>
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 arena = rsjsonnet_lang::arena::Arena::new();
let mut session = rsjsonnet_front::Session::new(&arena);
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<'p>>
pub fn load_virt_file( &mut self, repr_path: &str, data: Vec<u8>, ) -> Option<Thunk<'p>>
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<'p>>
pub fn load_real_file(&mut self, path: &Path) -> Option<Thunk<'p>>
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<'p>) -> Option<Value<'p>>
pub fn eval_value(&mut self, thunk: &Thunk<'p>) -> Option<Value<'p>>
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<'p>,
pos_args: &[Thunk<'p>],
named_args: &[(InternedStr<'p>, Thunk<'p>)],
) -> Option<Value<'p>>
pub fn eval_call( &mut self, func: &Thunk<'p>, pos_args: &[Thunk<'p>], named_args: &[(InternedStr<'p>, Thunk<'p>)], ) -> Option<Value<'p>>
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<'p>,
multiline: bool,
) -> Option<String>
pub fn manifest_json( &mut self, value: &Value<'p>, 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.