git.delta.rocks / jrsonnet / refs/heads / master

difftreelog

source

bindings/jsonnet/src/val_extract.rs1.2 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#[unsafe(no_mangle)]14pub extern "C" fn jsonnet_json_extract_string(_vm: &VM, v: &Val) -> *mut c_char {15	match v {16		Val::Str(s) => {17			let s = s.clone().into_flat();18			CString::new(s.as_str()).unwrap().into_raw()19		}20		_ => std::ptr::null_mut(),21	}22}2324/// If the value is a number, return `1` and store the number in out, otherwise return `0`.25#[unsafe(no_mangle)]26pub extern "C" fn jsonnet_json_extract_number(_vm: &VM, v: &Val, out: &mut c_double) -> c_int {27	match v {28		Val::Num(n) => {29			*out = n.get();30			131		}32		_ => 0,33	}34}3536/// Return `0` if the value is `false`, `1` if it is `true`, and `2` if it is not a `bool`.37#[unsafe(no_mangle)]38pub extern "C" fn jsonnet_json_extract_bool(_vm: &VM, v: &Val) -> c_int {39	match v {40		Val::Bool(false) => 0,41		Val::Bool(true) => 1,42		_ => 2,43	}44}4546/// Return `1` if the value is `null`, otherwise return `0`.47#[unsafe(no_mangle)]48pub extern "C" fn jsonnet_json_extract_null(_vm: &VM, v: &Val) -> c_int {49	match v {50		Val::Null => 1,51		_ => 0,52	}53}