git.delta.rocks / jrsonnet / refs/commits / f2fefaf94f02

difftreelog

source

bindings/jsonnet/src/val_extract.rs1.1 KiBsourcehistory
1//! Extract values from VM23use std::{4	ffi::CString,5	os::raw::{c_char, c_double, c_int},6};78use jrsonnet_evaluator::{State, Val};910/// If the value is a string, return it as UTF8 otherwise return NULL.11#[no_mangle]12pub extern "C" fn jsonnet_json_extract_string(_vm: &State, v: &Val) -> *mut c_char {13	match v {14		Val::Str(s) => CString::new(s as &str).unwrap().into_raw(),15		_ => std::ptr::null_mut(),16	}17}1819/// If the value is a number, return 1 and store the number in out, otherwise return 0.20#[no_mangle]21pub extern "C" fn jsonnet_json_extract_number(_vm: &State, v: &Val, out: &mut c_double) -> c_int {22	match v {23		Val::Num(n) => {24			*out = *n;25			126		}27		_ => 0,28	}29}3031/// Return 0 if the value is false, 1 if it is true, and 2 if it is not a bool.32#[no_mangle]33pub extern "C" fn jsonnet_json_extract_bool(_vm: &State, v: &Val) -> c_int {34	match v {35		Val::Bool(false) => 0,36		Val::Bool(true) => 1,37		_ => 2,38	}39}4041/// Return 1 if the value is null, else 0.42#[no_mangle]43pub extern "C" fn jsonnet_json_extract_null(_vm: &State, v: &Val) -> c_int {44	match v {45		Val::Null => 1,46		_ => 0,47	}48}