Struct rsjsonnet_front::Session

source ·
pub struct Session { /* private fields */ }

Implementations§

source§

impl Session

source

pub fn new() -> Self

source

pub fn program(&self) -> &Program

Returns a reference to the underlying Program.

source

pub fn program_mut(&mut self) -> &mut Program

Returns a mutable reference to the underlying Program.

source

pub fn set_max_trace(&mut self, max_trace: usize)

Sets the maximum number of stack trace items to print.

source

pub fn add_search_path(&mut self, path: PathBuf)

source

pub fn add_native_func<const N: usize, F>( &mut self, name: &str, params: &[&str; N], func: F, )
where F: FnMut(&mut Program, &[Value; N]) -> Result<Value, String> + 'static,

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));
source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn print_error(&self, msg: &str)

source

pub fn print_note(&self, msg: &str)

source

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.

source

pub fn pop_custom_stack_trace_item(&mut self)

Removes the last item pushed with Session::push_custom_stack_trace_item.

Trait Implementations§

source§

impl Default for Session

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for Session

§

impl !RefUnwindSafe for Session

§

impl !Send for Session

§

impl !Sync for Session

§

impl Unpin for Session

§

impl !UnwindSafe for Session

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.