difftreelog
refactor trivial code review suggestions
in: master
8 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth25#[no_mangle]25#[no_mangle]26pub extern "C" fn _start() {}26pub extern "C" fn _start() {}272728/// Return the version string of the Jsonnet interpreter. Conforms to semantic versioning28/// Return the version string of the Jsonnet interpreter.29/// http://semver.org/ If this does not match LIB_JSONNET_VERSION then there is a mismatch between29/// Conforms to [semantic versioning](http://semver.org/).30/// If this does not match `LIB_JSONNET_VERSION`30/// header and compiled library.31/// then there is a mismatch between header and compiled library.31#[no_mangle]32#[no_mangle]32pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {33pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {33 b"v0.16.0\0"34 b"v0.16.0\0"67 }68 }68}69}697070/// Create a new Jsonnet virtual machine.71/// Creates a new Jsonnet virtual machine.71#[no_mangle]72#[no_mangle]72pub extern "C" fn jsonnet_make() -> *mut State {73pub extern "C" fn jsonnet_make() -> *mut State {73 let state = State::default();74 let state = State::default();79 Box::into_raw(Box::new(state))80 Box::into_raw(Box::new(state))80}81}818282/// Complement of `jsonnet_vm_make`83/// Complement of [`jsonnet_vm_make`].83#[no_mangle]84#[no_mangle]84#[allow(clippy::boxed_local)]85#[allow(clippy::boxed_local)]85pub extern "C" fn jsonnet_destroy(vm: Box<State>) {86pub extern "C" fn jsonnet_destroy(vm: Box<State>) {118 }119 }119}120}120121121/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will122/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will122/// only return NULL if sz was zero.123/// only return NULL if sz was zero.123///124///124/// # Safety125/// # Safetybindings/jsonnet/src/native.rsdiffbeforeafterboth12};12};13use jrsonnet_gcmodule::Cc;13use jrsonnet_gcmodule::Cc;141415/// The returned JsonnetJsonValue* should be allocated with jsonnet_realloc. It will be cleaned up15/// The returned `JsonnetJsonValue*` should be allocated with `jsonnet_realloc`. It will be cleaned up16/// along with the objects rooted at argv by libjsonnet when no-longer needed. Return a string upon16/// along with the objects rooted at `argv` by `libjsonnet` when no-longer needed. Return a string upon17/// failure, which will appear in Jsonnet as an error. The argv pointer is an array whose size17/// failure, which will appear in Jsonnet as an error. The `argv` pointer is an array whose size18/// matches the array of parameters supplied when the native callback was originally registered.18/// matches the array of parameters supplied when the native callback was originally registered.19///19///20/// - `ctx` User pointer, given in jsonnet_native_callback.20/// - `ctx` User pointer, given in jsonnet_native_callback.bindings/jsonnet/src/val_extract.rsdiffbeforeafterboth778use jrsonnet_evaluator::{State, Val};8use jrsonnet_evaluator::{State, Val};9910/// If the value is a string, return it as UTF8 otherwise return NULL.10/// If the value is a string, return it as UTF-8, otherwise return `NULL`.11#[no_mangle]11#[no_mangle]12pub extern "C" fn jsonnet_json_extract_string(_vm: &State, v: &Val) -> *mut c_char {12pub extern "C" fn jsonnet_json_extract_string(_vm: &State, v: &Val) -> *mut c_char {13 match v {13 match v {16 }16 }17}17}181819/// If the value is a number, return 1 and store the number in out, otherwise return 0.19/// If the value is a number, return `1` and store the number in out, otherwise return `0`.20#[no_mangle]20#[no_mangle]21pub extern "C" fn jsonnet_json_extract_number(_vm: &State, v: &Val, out: &mut c_double) -> c_int {21pub extern "C" fn jsonnet_json_extract_number(_vm: &State, v: &Val, out: &mut c_double) -> c_int {22 match v {22 match v {28 }28 }29}29}303031/// Return 0 if the value is false, 1 if it is true, and 2 if it is not a bool.31/// Return `0` if the value is `false`, `1` if it is `true`, and `2` if it is not a `bool`.32#[no_mangle]32#[no_mangle]33pub extern "C" fn jsonnet_json_extract_bool(_vm: &State, v: &Val) -> c_int {33pub extern "C" fn jsonnet_json_extract_bool(_vm: &State, v: &Val) -> c_int {34 match v {34 match v {38 }38 }39}39}404041/// Return 1 if the value is null, else 0.41/// Return `1` if the value is `null`, otherwise return `0`.42#[no_mangle]42#[no_mangle]43pub extern "C" fn jsonnet_json_extract_null(_vm: &State, v: &Val) -> c_int {43pub extern "C" fn jsonnet_json_extract_null(_vm: &State, v: &Val) -> c_int {44 match v {44 match v {bindings/jsonnet/src/val_make.rsdiffbeforeafterboth8use jrsonnet_evaluator::{val::ArrValue, ObjValue, State, Val};8use jrsonnet_evaluator::{val::ArrValue, ObjValue, State, Val};9use jrsonnet_gcmodule::Cc;9use jrsonnet_gcmodule::Cc;101011/// Convert the given UTF8 string to a JsonnetJsonValue.11/// Convert the given `UTF-8` string to a `JsonnetJsonValue`.12///12///13/// # Safety13/// # Safety14///14///20 Box::into_raw(Box::new(Val::Str(val.into())))20 Box::into_raw(Box::new(Val::Str(val.into())))21}21}222223/// Convert the given double to a JsonnetJsonValue.23/// Convert the given double to a `JsonnetJsonValue`.24#[no_mangle]24#[no_mangle]25pub extern "C" fn jsonnet_json_make_number(_vm: &State, v: c_double) -> *mut Val {25pub extern "C" fn jsonnet_json_make_number(_vm: &State, v: c_double) -> *mut Val {26 Box::into_raw(Box::new(Val::Num(v)))26 Box::into_raw(Box::new(Val::Num(v)))27}27}282829/// Convert the given bool (1 or 0) to a JsonnetJsonValue.29/// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.30#[no_mangle]30#[no_mangle]31pub extern "C" fn jsonnet_json_make_bool(_vm: &State, v: c_int) -> *mut Val {31pub extern "C" fn jsonnet_json_make_bool(_vm: &State, v: c_int) -> *mut Val {32 assert!(v == 0 || v == 1, "bad boolean value");32 assert!(v == 0 || v == 1, "bad boolean value");33 Box::into_raw(Box::new(Val::Bool(v == 1)))33 Box::into_raw(Box::new(Val::Bool(v == 1)))34}34}353536/// Make a JsonnetJsonValue representing null.36/// Make a `JsonnetJsonValue` representing `null`.37#[no_mangle]37#[no_mangle]38pub extern "C" fn jsonnet_json_make_null(_vm: &State) -> *mut Val {38pub extern "C" fn jsonnet_json_make_null(_vm: &State) -> *mut Val {39 Box::into_raw(Box::new(Val::Null))39 Box::into_raw(Box::new(Val::Null))40}40}414142/// Make a JsonnetJsonValue representing an array.42/// Make a `JsonnetJsonValue` representing an array.43///43///44/// Assign elements with jsonnet_json_array_append.44/// Assign elements with [`jsonnet_json_array_append`].45#[no_mangle]45#[no_mangle]46pub extern "C" fn jsonnet_json_make_array(_vm: &State) -> *mut Val {46pub extern "C" fn jsonnet_json_make_array(_vm: &State) -> *mut Val {47 Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))47 Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))48}48}494950/// Make a JsonnetJsonValue representing an object.50/// Make a `JsonnetJsonValue` representing an object.51#[no_mangle]51#[no_mangle]52pub extern "C" fn jsonnet_json_make_object(_vm: &State) -> *mut Val {52pub extern "C" fn jsonnet_json_make_object(_vm: &State) -> *mut Val {53 Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))53 Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth7use jrsonnet_evaluator::{val::ArrValue, State, Thunk, Val};7use jrsonnet_evaluator::{val::ArrValue, State, Thunk, Val};8use jrsonnet_gcmodule::Cc;8use jrsonnet_gcmodule::Cc;9910/// Add value to the end of the array arr10/// Adds value to the end of the array `arr`.11///11///12/// # Safety12/// # Safety13///13///29 }29 }30}30}313132/// Add the field to the object, bound to value.32/// Adds the field to the object, bound to value.33///33///34/// This shadows any previous binding of the field.34/// This shadows any previous binding of the field.35///35///36/// # Safety36/// # Safety37///37///38/// `obj` should be pointer to object value allocated by make_object, or returned by other library call38/// `obj` should be a valid pointer to object value allocated by `make_object`, or returned by other library call39/// `name` should be \0-terminated string39/// `name` should be \0-terminated string40#[no_mangle]40#[no_mangle]41pub unsafe extern "C" fn jsonnet_json_object_append(41pub unsafe extern "C" fn jsonnet_json_object_append(crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth105 fn as_any(&self) -> &dyn Any;105 fn as_any(&self) -> &dyn Any;106}106}107107108/// Context initializer, which adds noth108/// Context initializer which adds nothing.109pub struct DummyContextInitializer;109pub struct DummyContextInitializer;110impl ContextInitializer for DummyContextInitializer {110impl ContextInitializer for DummyContextInitializer {111 fn initialize(&self, _state: State, _for_file: Source) -> Context {111 fn initialize(&self, _state: State, _for_file: Source) -> Context {crates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth66}66}676768fn frexp(s: f64) -> (f64, i16) {68fn frexp(s: f64) -> (f64, i16) {69 if 0.0 == s {69 if s == 0.0 {70 (s, 0)70 (s, 0)71 } else {71 } else {72 let lg = s.abs().log2();72 let lg = s.abs().log2();crates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth41 (Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,41 (Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,42 (Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}42 (Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}43 (Val::Str(_) | Val::Num(_), _) => {43 (Val::Str(_) | Val::Num(_), _) => {44 throw_runtime!("sort elements should have same types")44 throw_runtime!("sort elements should have the same types")45 }45 }46 _ => throw_runtime!("sort key should be string or number"),46 _ => throw_runtime!("sort key should either be a string or a number"),47 }47 }48 }48 }49 Ok(sort_type)49 Ok(sort_type)