Crates.io | jsonnet-go |
lib.rs | jsonnet-go |
version | 0.1.0 |
source | src |
created_at | 2024-09-22 02:11:56.057748 |
updated_at | 2024-09-22 02:11:56.057748 |
description | Idiomatic rust bindings for go-jsonnet |
homepage | |
repository | https://github.com/swlynch99/jsonnet-go-rs |
max_upload_size | |
id | 1382686 |
size | 96,767 |
Run jsonnet programs in rust!
This crate provides idiomatic Rust bindings for the go-jsonnet library.
Create a JsonnetVm
and then call evaluate_file
or evaluate_snippet
to run
jsonnet code. These will return a string containing the resulting JSON:
use jsonnet_go::JsonnetVm;
fn main() -> jsonnet_go::Result<()> {
let mut vm = JsonnetVm::new();
let jsonnet = "{ field: std.base64('Hello, World!') }";
let json = vm.evaluate_snippet("<inline>", jsonnet)?;
println!("{json}");
Ok(())
}
Alternatively, you could enable the json
feature and use evaluate_json
to
deserialize the returned JSON string:
use jsonnet_go::{JsonnetVm, EvaluateOptions};
use serde::Deserialize;
#[derive(Deserialize)]
struct Output {
field: String
}
fn main() -> jsonnet_go::Result<()> {
let mut vm = JsonnetVm::new();
let jsonnet = "{ field: std.base64('Hello, World!') }";
let options = EvaluateOptions::new("<inline>").snippet(jsonnet);
let output: Output = vm.evaluate_json(options)?;
assert_eq!(output.field, "SGVsbG8sIFdvcmxkIQ==");
Ok(())
}
You will need go 1.12+ installed in order to build go-jsonnet. You can install it via:
On older distros the packaged version of go may not be new enough. In that case you will need to install it from the official website.