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

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::Val;910use crate::VM;1112/// If the value is a string, return it as UTF-8, otherwise return `NULL`.13#[no_mangle]14pub extern "C" fn jsonnet_json_extract_string(_vm: &VM, v: &Val) -> *mut c_char {15	match v {16		Val::Str(s) => CString::new(s as &str).unwrap().into_raw(),17		_ => std::ptr::null_mut(),18	}19}2021/// If the value is a number, return `1` and store the number in out, otherwise return `0`.22#[no_mangle]23pub extern "C" fn jsonnet_json_extract_number(_vm: &VM, v: &Val, out: &mut c_double) -> c_int {24	match v {25		Val::Num(n) => {26			*out = *n;27			128		}29		_ => 0,30	}31}3233/// Return `0` if the value is `false`, `1` if it is `true`, and `2` if it is not a `bool`.34#[no_mangle]35pub extern "C" fn jsonnet_json_extract_bool(_vm: &VM, v: &Val) -> c_int {36	match v {37		Val::Bool(false) => 0,38		Val::Bool(true) => 1,39		_ => 2,40	}41}4243/// Return `1` if the value is `null`, otherwise return `0`.44#[no_mangle]45pub extern "C" fn jsonnet_json_extract_null(_vm: &VM, v: &Val) -> c_int {46	match v {47		Val::Null => 1,48		_ => 0,49	}50}