Struct jsonnet::JsonnetVm[][src]

pub struct JsonnetVm(_);

Jsonnet virtual machine context.

Implementations

impl JsonnetVm[src]

pub fn new() -> Self[src]

Create a new Jsonnet virtual machine.

pub fn max_stack(&mut self, v: u32)[src]

Set the maximum stack depth.

pub fn gc_min_objects(&mut self, v: u32)[src]

Set the number of objects required before a garbage collection cycle is allowed.

pub fn gc_growth_trigger(&mut self, v: f64)[src]

Run the garbage collector after this amount of growth in the number of objects.

pub fn string_output(&mut self, v: bool)[src]

Expect a string as output and don't JSON encode it.

pub fn import_callback<F>(&mut self, cb: F) where
    F: Fn(&JsonnetVm, &Path, &Path) -> Result<(PathBuf, String), String>, 
[src]

Override the callback used to locate imports.

Examples

use std::path::{Path,PathBuf};
use std::ffi::OsStr;
use jsonnet::JsonnetVm;

let mut vm = JsonnetVm::new();
vm.import_callback(|_vm, base, rel| {
   if rel.file_stem() == Some(OsStr::new("bar")) {
      let newbase = base.into();
      let contents = "2 + 3".to_owned();
      Ok((newbase, contents))
   } else {
      Err("not found".to_owned())
   }
});

{
   let output = vm.evaluate_snippet("myimport", "import 'x/bar.jsonnet'").unwrap();
   assert_eq!(output.to_string(), "5\n");
}

{
   let result = vm.evaluate_snippet("myimport", "import 'x/foo.jsonnet'");
   assert!(result.is_err());
}

pub fn native_callback<F>(&mut self, name: &str, cb: F, params: &[&str]) where
    F: Fn(&'a JsonnetVm, &[JsonVal<'a>]) -> Result<JsonValue<'a>, String>, 
[src]

Register a native extension.

This will appear in Jsonnet as a function type and can be accessed from std.native("foo").

Examples

use jsonnet::{JsonnetVm, JsonVal, JsonValue};

fn myadd<'a>(vm: &'a JsonnetVm, args: &[JsonVal<'a>]) -> Result<JsonValue<'a>, String> {
   let a = args[0].as_num().ok_or("Expected a number")?;
   let b = args[1].as_num().ok_or("Expected a number")?;
   Ok(JsonValue::from_num(vm, a + b))
}

let mut vm = JsonnetVm::new();
vm.native_callback("myadd", myadd, &["a", "b"]);

{
   let result = vm.evaluate_snippet("nativetest",
      "std.native('myadd')(2, 3)");
   assert_eq!(result.unwrap().as_str(), "5\n");
}

{
   let result = vm.evaluate_snippet("nativefail",
      "std.native('myadd')('foo', 'bar')");
   assert!(result.is_err());
}

vm.native_callback("upcase", |vm, args| {
      let s = args[0].as_str().ok_or("Expected a string")?;
      Ok(JsonValue::from_str(vm, &s.to_uppercase()))
   }, &["s"]);
{
   let result = vm.evaluate_snippet("nativeclosure",
      "std.native('upcase')('foO')");
   assert_eq!(result.unwrap().as_str(), "\"FOO\"\n");
}

Panics

Panics if name or params contain any embedded nul characters.

pub fn ext_var(&mut self, key: &str, val: &str)[src]

Bind a Jsonnet external var to the given string.

Panics

Panics if key or val contain embedded nul characters.

pub fn ext_code(&mut self, key: &str, code: &str)[src]

Bind a Jsonnet external var to the given code.

Panics

Panics if key or code contain embedded nul characters.

pub fn tla_var(&mut self, key: &str, val: &str)[src]

Bind a string top-level argument for a top-level parameter.

Panics

Panics if key or val contain embedded nul characters.

pub fn tla_code(&mut self, key: &str, code: &str)[src]

Bind a code top-level argument for a top-level parameter.

Panics

Panics if key or code contain embedded nul characters.

pub fn fmt_indent(&mut self, n: u32)[src]

Indentation level when reformatting (number of spaces).

pub fn fmt_max_blank_lines(&mut self, n: u32)[src]

Maximum number of blank lines when reformatting.

pub fn fmt_string(&mut self, fmt: FmtString)[src]

Preferred style for string literals ("" or '').

pub fn fmt_comment(&mut self, fmt: FmtComment)[src]

Preferred style for line comments (# or //).

pub fn fmt_pad_arrays(&mut self, pad: bool)[src]

Whether to add an extra space on the inside of arrays.

pub fn fmt_pad_objects(&mut self, pad: bool)[src]

Whether to add an extra space on the inside of objects.

pub fn fmt_pretty_field_names(&mut self, sugar: bool)[src]

Use syntax sugar where possible with field names.

pub fn fmt_sort_import(&mut self, sort: bool)[src]

Sort top-level imports in alphabetical order

pub fn fmt_debug_desugaring(&mut self, reformat: bool)[src]

Reformat the Jsonnet input after desugaring.

pub fn fmt_file<'a, P>(
    &'a mut self,
    filename: P
) -> Result<JsonnetString<'a>, Error<'a>> where
    P: AsRef<OsStr>, 
[src]

Reformat a file containing Jsonnet code, return a Jsonnet string.

Panics

Panics if filename contains embedded nul characters.

pub fn fmt_snippet<'a, P>(
    &'a mut self,
    filename: P,
    snippet: &str
) -> Result<JsonnetString<'a>, Error<'a>> where
    P: AsRef<OsStr>, 
[src]

Reformat a string containing Jsonnet code, return a Jsonnet string.

Panics

Panics if filename or snippet contain embedded nul characters.

pub fn max_trace(&mut self, limit: Option<u32>)[src]

Set the number of lines of stack trace to display (None for unlimited).

pub fn jpath_add<P>(&mut self, path: P) where
    P: AsRef<OsStr>, 
[src]

Add to the default import callback's library search path.

Search order is last to first, so more recently appended paths take precedence.

Panics

Panics if path contains embedded nul characters.

pub fn evaluate_file<'a, P>(
    &'a mut self,
    filename: P
) -> Result<JsonnetString<'a>, Error<'a>> where
    P: AsRef<OsStr>, 
[src]

Evaluate a file containing Jsonnet code, returning a JSON string.

Errors

Returns any jsonnet error during evaluation.

Panics

Panics if filename contains embedded nul characters.

pub fn evaluate_snippet<'a, P>(
    &'a mut self,
    filename: P,
    snippet: &str
) -> Result<JsonnetString<'a>, Error<'a>> where
    P: AsRef<OsStr>, 
[src]

Evaluate a string containing Jsonnet code, returning a JSON string.

The filename argument is only used in error messages.

Errors

Returns any jsonnet error during evaluation.

Panics

Panics if filename or snippet contain embedded nul characters.

pub fn evaluate_file_multi<'a, P>(
    &'a mut self,
    filename: P
) -> Result<EvalMap<'a>, Error<'a>> where
    P: AsRef<OsStr>, 
[src]

Evaluate a file containing Jsonnet code, returning a number of named JSON files.

Errors

Returns any jsonnet error during evaluation.

Panics

Panics if filename contains embedded nul characters.

pub fn evaluate_snippet_multi<'a, P>(
    &'a mut self,
    filename: P,
    snippet: &str
) -> Result<EvalMap<'a>, Error<'a>> where
    P: AsRef<OsStr>, 
[src]

Evaluate a file containing Jsonnet code, returning a number of named JSON files.

Errors

Returns any jsonnet error during evaluation.

Panics

Panics if filename or snippet contain embedded nul characters.

Examples

use std::collections::HashMap;
use jsonnet::JsonnetVm;

let mut vm = JsonnetVm::new();
let output = vm.evaluate_snippet_multi("multi",
   "{'foo.json': 'foo', 'bar.json': 'bar'}").unwrap();

let map: HashMap<_,_> = output.iter().collect();
assert_eq!(*map.get("bar.json").unwrap(), "\"bar\"\n");

pub fn evaluate_file_stream<'a, P>(
    &'a mut self,
    filename: P
) -> Result<EvalList<'a>, Error<'a>> where
    P: AsRef<OsStr>, 
[src]

Evaluate a file containing Jsonnet code, returning a number of JSON files.

Errors

Returns any jsonnet error during evaluation.

Panics

Panics if filename contains embedded nul characters.

pub fn evaluate_snippet_stream<'a, P>(
    &'a mut self,
    filename: P,
    snippet: &str
) -> Result<EvalList<'a>, Error<'a>> where
    P: AsRef<OsStr>, 
[src]

Evaluate a string containing Jsonnet code, returning a number of JSON files.

Errors

Returns any jsonnet error during evaluation.

Panics

Panics if filename or snippet contain embedded nul characters.

Examples

use jsonnet::JsonnetVm;

let mut vm = JsonnetVm::new();
let output = vm.evaluate_snippet_stream("stream",
   "['foo', 'bar']").unwrap();

let list: Vec<_> = output.iter().collect();
assert_eq!(list, vec!["\"foo\"\n", "\"bar\"\n"]);

Trait Implementations

impl Drop for JsonnetVm[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.