difftreelog
style enforce import style
in: master
38 files changed
.rustfmt.tomldiffbeforeafterboth--- a/.rustfmt.toml
+++ b/.rustfmt.toml
@@ -1 +1,3 @@
hard_tabs = true
+imports_granularity = "crate"
+group_imports = "stdexternalcrate"
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -1,9 +1,5 @@
//! Import resolution manipulation utilities
-use jrsonnet_evaluator::{
- error::{Error::*, Result},
- throw, EvaluationState, ImportResolver,
-};
use std::{
any::Any,
cell::RefCell,
@@ -17,6 +13,11 @@
rc::Rc,
};
+use jrsonnet_evaluator::{
+ error::{Error::*, Result},
+ throw, EvaluationState, ImportResolver,
+};
+
pub type JsonnetImportCallback = unsafe extern "C" fn(
ctx: *mut c_void,
base: *const c_char,
bindings/jsonnet/src/interop.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/interop.rs
+++ b/bindings/jsonnet/src/interop.rs
@@ -1,12 +1,14 @@
//! Jrsonnet specific additional binding helpers
-use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
-use jrsonnet_evaluator::{EvaluationState, Val};
use std::{
ffi::c_void,
os::raw::{c_char, c_int},
};
+use jrsonnet_evaluator::{EvaluationState, Val};
+
+use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
+
extern "C" {
pub fn _jrsonnet_static_import_callback(
ctx: *mut c_void,
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -8,8 +8,6 @@
pub mod val_modify;
pub mod vars_tlas;
-use import::NativeImportResolver;
-use jrsonnet_evaluator::{EvaluationState, IStr, ManifestFormat, Val};
use std::{
alloc::Layout,
ffi::{CStr, CString},
@@ -17,6 +15,9 @@
path::PathBuf,
};
+use import::NativeImportResolver;
+use jrsonnet_evaluator::{EvaluationState, IStr, ManifestFormat, Val};
+
/// WASM stub
#[cfg(target_arch = "wasm32")]
#[no_mangle]
bindings/jsonnet/src/native.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -1,3 +1,11 @@
+use std::{
+ convert::TryFrom,
+ ffi::{c_void, CStr},
+ os::raw::{c_char, c_int},
+ path::Path,
+ rc::Rc,
+};
+
use gcmodule::Cc;
use jrsonnet_evaluator::{
error::{Error, LocError},
@@ -5,13 +13,6 @@
gc::TraceBox,
native::{NativeCallback, NativeCallbackHandler},
EvaluationState, IStr, Val,
-};
-use std::{
- convert::TryFrom,
- ffi::{c_void, CStr},
- os::raw::{c_char, c_int},
- path::Path,
- rc::Rc,
};
type JsonnetNativeCallback = unsafe extern "C" fn(
bindings/jsonnet/src/val_extract.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_extract.rs
+++ b/bindings/jsonnet/src/val_extract.rs
@@ -1,12 +1,12 @@
//! Extract values from VM
-use jrsonnet_evaluator::{EvaluationState, Val};
-
use std::{
ffi::CString,
os::raw::{c_char, c_double, c_int},
};
+use jrsonnet_evaluator::{EvaluationState, Val};
+
#[no_mangle]
pub extern "C" fn jsonnet_json_extract_string(_vm: &EvaluationState, v: &Val) -> *mut c_char {
match v {
bindings/jsonnet/src/val_make.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -1,12 +1,13 @@
//! Create values in VM
-use gcmodule::Cc;
-use jrsonnet_evaluator::{ArrValue, EvaluationState, ObjValue, Val};
use std::{
ffi::CStr,
os::raw::{c_char, c_double, c_int},
};
+use gcmodule::Cc;
+use jrsonnet_evaluator::{val::ArrValue, EvaluationState, ObjValue, Val};
+
/// # Safety
///
/// This function is safe, if received v is a pointer to normal C string
bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -2,10 +2,11 @@
//! Only tested with variables, which haven't altered by code before appearing here
//! In jrsonnet every value is immutable, and this code is probally broken
+use std::{ffi::CStr, os::raw::c_char};
+
use gcmodule::Cc;
-use jrsonnet_evaluator::{ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};
+use jrsonnet_evaluator::{val::ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};
use jrsonnet_parser::Visibility;
-use std::{ffi::CStr, os::raw::c_char};
/// # Safety
///
bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -1,8 +1,9 @@
//! Manipulate external variables and top level arguments
-use jrsonnet_evaluator::EvaluationState;
use std::{ffi::CStr, os::raw::c_char};
+use jrsonnet_evaluator::EvaluationState;
+
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn jsonnet_ext_var(
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,13 +1,13 @@
+use std::{
+ fs::{create_dir_all, File},
+ io::{Read, Write},
+ path::PathBuf,
+};
+
use clap::{AppSettings, IntoApp, Parser};
use clap_complete::Shell;
use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
use jrsonnet_evaluator::{error::LocError, EvaluationState};
-use std::{
- fs::{create_dir_all, File},
- io::Read,
- io::Write,
- path::PathBuf,
-};
#[cfg(feature = "mimalloc")]
#[global_allocator]
crates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/ext.rs
+++ b/crates/jrsonnet-cli/src/ext.rs
@@ -1,8 +1,10 @@
-use crate::ConfigureState;
+use std::{fs::read_to_string, str::FromStr};
+
use clap::Parser;
use jrsonnet_evaluator::{error::Result, EvaluationState};
-use std::{fs::read_to_string, str::FromStr};
+use crate::ConfigureState;
+
#[derive(Clone)]
pub struct ExtStr {
pub name: String,
crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -3,14 +3,14 @@
mod tla;
mod trace;
+use std::{env, path::PathBuf};
+
+use clap::Parser;
pub use ext::*;
+use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};
pub use manifest::*;
pub use tla::*;
pub use trace::*;
-
-use clap::Parser;
-use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};
-use std::{env, path::PathBuf};
pub trait ConfigureState {
fn configure(&self, state: &EvaluationState) -> Result<()>;
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -1,8 +1,10 @@
-use crate::ConfigureState;
+use std::{path::PathBuf, str::FromStr};
+
use clap::Parser;
use jrsonnet_evaluator::{error::Result, EvaluationState, ManifestFormat};
-use std::{path::PathBuf, str::FromStr};
+use crate::ConfigureState;
+
pub enum ManifestFormatName {
/// Expect string as output, and write them directly
String,
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,7 +1,8 @@
-use crate::{ConfigureState, ExtFile, ExtStr};
use clap::Parser;
use jrsonnet_evaluator::{error::Result, EvaluationState};
+use crate::{ConfigureState, ExtFile, ExtStr};
+
#[derive(Parser)]
#[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]
pub struct TLAOpts {
crates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -1,12 +1,14 @@
-use crate::ConfigureState;
+use std::str::FromStr;
+
use clap::Parser;
use jrsonnet_evaluator::{
error::Result,
trace::{CompactFormat, ExplainingFormat, PathResolver},
EvaluationState,
};
-use std::str::FromStr;
+use crate::ConfigureState;
+
#[derive(PartialEq)]
pub enum TraceFormatName {
Compact,
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -1,6 +1,3 @@
-use bincode::serialize;
-use jrsonnet_parser::{parse, ParserSettings};
-use jrsonnet_stdlib::STDLIB_STR;
use std::{
env,
fs::File,
@@ -8,6 +5,10 @@
path::{Path, PathBuf},
};
+use bincode::serialize;
+use jrsonnet_parser::{parse, ParserSettings};
+use jrsonnet_stdlib::STDLIB_STR;
+
fn main() {
let parsed = parse(
STDLIB_STR,
crates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -1,13 +1,15 @@
//! faster std.format impl
#![allow(clippy::too_many_arguments)]
-use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
+use std::convert::TryFrom;
+
use gcmodule::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_types::ValType;
-use std::convert::TryFrom;
use thiserror::Error;
+use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
+
#[derive(Debug, Clone, Error, Trace)]
pub enum FormatError {
#[error("truncated format code")]
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -1,7 +1,7 @@
-use crate::error::Error::*;
-use crate::error::Result;
-use crate::push_description_frame;
-use crate::{throw, Val};
+use crate::{
+ error::{Error::*, Result},
+ push_description_frame, throw, Val,
+};
#[derive(PartialEq, Clone, Copy)]
pub enum ManifestType {
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,23 +1,25 @@
-use crate::function::{CallLocation, StaticBuiltin};
-use crate::typed::{Any, Bytes, PositiveF64, VecVal, M1};
-use crate::{
- builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
- equals,
- error::{Error::*, Result},
- operator::evaluate_mod_op,
- primitive_equals, push_frame, throw,
- typed::{Either2, Either4},
- with_state, ArrValue, FuncVal, IndexableVal, Val,
+use std::{
+ collections::HashMap,
+ convert::{TryFrom, TryInto},
};
-use crate::{Either, ObjValue};
+
use format::{format_arr, format_obj};
use gcmodule::Cc;
use jrsonnet_interner::IStr;
use serde::Deserialize;
use serde_yaml::DeserializingQuirks;
-use std::collections::HashMap;
-use std::convert::{TryFrom, TryInto};
+use crate::{
+ builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
+ error::{Error::*, Result},
+ function::{CallLocation, StaticBuiltin},
+ operator::evaluate_mod_op,
+ push_frame, throw,
+ typed::{Any, BoundedUsize, Bytes, Either2, Either4, PositiveF64, VecVal, M1},
+ val::{equals, primitive_equals, ArrValue, FuncVal, IndexableVal, Slice},
+ with_state, Either, ObjValue, Val,
+};
+
pub mod stdlib;
pub use stdlib::*;
@@ -58,12 +60,12 @@
}
Ok(Val::Str(
- (s.chars()
- .skip(index)
- .take(end - index)
- .step_by(step)
- .collect::<String>())
- .into(),
+ (s.chars()
+ .skip(index)
+ .take(end - index)
+ .step_by(step)
+ .collect::<String>())
+ .into(),
))
}
IndexableVal::Arr(arr) => {
crates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -1,10 +1,12 @@
+use gcmodule::{Cc, Trace};
+
use crate::{
error::{Error, LocError, Result},
throw,
typed::Any,
- FuncVal, Val,
+ val::FuncVal,
+ Val,
};
-use gcmodule::{Cc, Trace};
#[derive(Debug, Clone, thiserror::Error, Trace)]
pub enum SortError {
crates/jrsonnet-evaluator/src/builtin/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
@@ -1,6 +1,7 @@
-use jrsonnet_parser::{LocExpr, ParserSettings};
use std::path::PathBuf;
+use jrsonnet_parser::{LocExpr, ParserSettings};
+
thread_local! {
/// To avoid parsing again when issued from the same thread
#[allow(unreachable_code)]
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -1,12 +1,12 @@
-use crate::cc_ptr_eq;
-use crate::gc::GcHashMap;
+use std::fmt::Debug;
+
+use gcmodule::{Cc, Trace};
+use jrsonnet_interner::IStr;
+
use crate::{
- error::Error::*, map::LayeredHashMap, FutureWrapper, LazyBinding, LazyVal, ObjValue, Result,
- Val,
+ cc_ptr_eq, error::Error::*, gc::GcHashMap, map::LayeredHashMap, FutureWrapper, LazyBinding,
+ LazyVal, ObjValue, Result, Val,
};
-use gcmodule::{Cc, Trace};
-use jrsonnet_interner::IStr;
-use std::fmt::Debug;
#[derive(Clone, Trace)]
pub struct ContextCreator(pub Context, pub FutureWrapper<GcHashMap<IStr, LazyBinding>>);
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -1,16 +1,18 @@
-use crate::{
- builtin::{format::FormatError, sort::SortError},
- typed::TypeLocError,
+use std::{
+ path::{Path, PathBuf},
+ rc::Rc,
};
+
use gcmodule::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
use jrsonnet_types::ValType;
-use std::{
- path::{Path, PathBuf},
- rc::Rc,
+use thiserror::Error;
+
+use crate::{
+ builtin::{format::FormatError, sort::SortError},
+ typed::TypeLocError,
};
-use thiserror::Error;
#[derive(Error, Debug, Clone, Trace)]
pub enum Error {
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -1,15 +1,5 @@
use std::convert::TryFrom;
-use crate::{
- builtin::{std_slice, BUILTINS},
- error::Error::*,
- evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
- function::CallLocation,
- gc::TraceBox,
- push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
- FutureWrapper, GcHashMap, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder,
- ObjectAssertion, Result, Val,
-};
use gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
@@ -17,6 +7,19 @@
LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
};
use jrsonnet_types::ValType;
+
+use crate::{
+ builtin::{std_slice, BUILTINS},
+ error::Error::*,
+ evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
+ function::CallLocation,
+ gc::TraceBox,
+ push_frame, throw,
+ typed::BoundedUsize,
+ val::{ArrValue, FuncDesc, FuncVal, LazyValValue},
+ with_state, Bindable, Context, ContextCreator, FutureWrapper, GcHashMap, LazyBinding, LazyVal,
+ ObjValue, ObjValueBuilder, ObjectAssertion, Result, Val,
+};
pub mod operator;
pub fn evaluate_binding_in_future(
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -1,10 +1,11 @@
use std::convert::TryInto;
-use crate::builtin::std_format;
-use crate::{equals, evaluate, Context, Val};
-use crate::{error::Error::*, throw, Result};
use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
+use crate::{
+ builtin::std_format, error::Error::*, evaluate, throw, val::equals, Context, Result, Val,
+};
+
pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
use UnaryOpType::*;
use Val::*;
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,16 +1,19 @@
+use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
+
+use gcmodule::Trace;
+use jrsonnet_interner::IStr;
+pub use jrsonnet_macros::builtin;
+use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
+
use crate::{
error::{Error::*, LocError},
evaluate, evaluate_named,
gc::TraceBox,
throw,
typed::Typed,
- Context, FutureWrapper, GcHashMap, LazyVal, LazyValValue, Result, Val,
+ val::LazyValValue,
+ Context, FutureWrapper, GcHashMap, LazyVal, Result, Val,
};
-use gcmodule::Trace;
-use jrsonnet_interner::IStr;
-pub use jrsonnet_macros::builtin;
-use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
-use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
#[derive(Clone, Copy)]
pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,17 +1,20 @@
-use crate::{
- error::{Error::*, Result},
- throw,
-};
-use fs::File;
-use jrsonnet_interner::IStr;
-use std::fs;
use std::{
any::Any,
+ convert::TryFrom,
+ fs,
+ io::Read,
path::{Path, PathBuf},
rc::Rc,
};
-use std::{convert::TryFrom, io::Read};
+use fs::File;
+use jrsonnet_interner::IStr;
+
+use crate::{
+ error::{Error::*, Result},
+ throw,
+};
+
/// Implements file resolution logic for `import` and `importStr`
pub trait ImportResolver {
/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -1,9 +1,11 @@
+use std::convert::{TryFrom, TryInto};
+
+use serde_json::{Map, Number, Value};
+
use crate::{
error::{Error::*, LocError, Result},
throw, ObjValueBuilder, Val,
};
-use serde_json::{Map, Number, Value};
-use std::convert::{TryFrom, TryInto};
impl TryFrom<&Val> for Value {
type Error = LocError;
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -13,6 +13,7 @@
pub mod error;
mod evaluate;
pub mod function;
+pub mod gc;
mod import;
mod integrations;
mod map;
@@ -20,9 +21,15 @@
mod obj;
pub mod trace;
pub mod typed;
-mod val;
+pub mod val;
-pub use jrsonnet_parser as parser;
+use std::{
+ cell::{Ref, RefCell, RefMut},
+ collections::HashMap,
+ fmt::Debug,
+ path::{Path, PathBuf},
+ rc::Rc,
+};
pub use ctx::*;
pub use dynamic::*;
@@ -33,18 +40,11 @@
use gcmodule::{Cc, Trace, Weak};
pub use import::*;
pub use jrsonnet_interner::IStr;
+pub use jrsonnet_parser as parser;
use jrsonnet_parser::*;
pub use obj::*;
-use std::{
- cell::{Ref, RefCell, RefMut},
- collections::HashMap,
- fmt::Debug,
- path::{Path, PathBuf},
- rc::Rc,
-};
use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};
-pub use val::*;
-pub mod gc;
+pub use val::{LazyVal, ManifestFormat, Val};
pub trait Bindable: Trace + 'static {
fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;
@@ -693,18 +693,24 @@
#[cfg(test)]
pub mod tests {
- use super::Val;
- use crate::{
- error::Error::*, function::BuiltinParam, gc::TraceBox, native::NativeCallbackHandler,
- primitive_equals, EvaluationState,
- };
- use gcmodule::{Cc, Trace};
- use jrsonnet_parser::*;
use std::{
path::{Path, PathBuf},
rc::Rc,
};
+ use gcmodule::{Cc, Trace};
+ use jrsonnet_parser::*;
+
+ use super::Val;
+ use crate::{
+ error::Error::*,
+ function::{BuiltinParam, CallLocation},
+ gc::TraceBox,
+ native::NativeCallbackHandler,
+ val::primitive_equals,
+ EvaluationState,
+ };
+
#[test]
#[should_panic]
fn eval_state_stacktrace() {
@@ -712,11 +718,15 @@
state.run_in_state(|| {
state
.push(
- Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),
+ CallLocation::new(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),
|| "outer".to_owned(),
|| {
state.push(
- Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),
+ CallLocation::new(&ExprLocation(
+ PathBuf::from("test2.jsonnet").into(),
+ 30,
+ 40,
+ )),
|| "inner".to_owned(),
|| Err(RuntimeError("".into()).into()),
)?;
@@ -1290,10 +1300,11 @@
}
mod derive_typed {
+ use std::path::PathBuf;
+
use crate::{typed::Typed, EvaluationState};
- use std::path::PathBuf;
- #[derive(Typed, PartialEq, Debug)]
+ #[derive(PartialEq, Debug, Typed)]
struct MyTyped {
a: u32,
#[typed(rename = "b")]
crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,13 +1,16 @@
#![allow(clippy::type_complexity)]
-use crate::function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam, CallLocation};
-use crate::gc::TraceBox;
-use crate::Context;
-use crate::{error::Result, Val};
+use std::{path::Path, rc::Rc};
+
use gcmodule::Trace;
-use std::path::Path;
-use std::rc::Rc;
+use crate::{
+ error::Result,
+ function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam, CallLocation},
+ gc::TraceBox,
+ Context, Val,
+};
+
#[derive(Trace)]
pub struct NativeCallback {
pub(crate) params: Vec<BuiltinParam>,
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,20 +1,23 @@
-use crate::error::LocError;
-use crate::function::CallLocation;
-use crate::gc::{GcHashMap, GcHashSet, TraceBox};
-use crate::operator::evaluate_add_op;
-use crate::push_frame;
-use crate::{
- cc_ptr_eq, error::Error::*, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal,
- Result, Val,
+use std::{
+ cell::RefCell,
+ fmt::Debug,
+ hash::{Hash, Hasher},
};
+
use gcmodule::{Cc, Trace, Weak};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ExprLocation, Visibility};
use rustc_hash::FxHashMap;
-use std::cell::RefCell;
-use std::fmt::Debug;
-use std::hash::{Hash, Hasher};
+use crate::{
+ cc_ptr_eq,
+ error::{Error::*, LocError},
+ function::CallLocation,
+ gc::{GcHashMap, GcHashSet, TraceBox},
+ operator::evaluate_add_op,
+ push_frame, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal, Result, Val,
+};
+
#[derive(Debug, Trace)]
pub struct ObjMember {
pub add: bool,
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -1,8 +1,10 @@
mod location;
+use std::path::{Path, PathBuf};
+
+pub use location::*;
+
use crate::{error::Error, EvaluationState, LocError};
-pub use location::*;
-use std::path::{Path, PathBuf};
/// The way paths should be displayed
pub enum PathResolver {
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -2,14 +2,14 @@
mod conversions;
pub use conversions::*;
+use gcmodule::Trace;
+pub use jrsonnet_types::{ComplexValType, ValType};
+use thiserror::Error;
use crate::{
error::{Error, LocError, Result},
push_description_frame, Val,
};
-use gcmodule::Trace;
-pub use jrsonnet_types::{ComplexValType, ValType};
-use thiserror::Error;
#[derive(Debug, Error, Clone, Trace)]
pub enum TypeError {
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth1use crate::{2 builtin::manifest::{3 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,4 },5 cc_ptr_eq,6 error::{Error::*, LocError},7 evaluate,8 function::{9 parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,10 StaticBuiltin,11 },12 gc::TraceBox,13 throw, Context, ObjValue, Result,14};15use gcmodule::{Cc, Trace};16use jrsonnet_interner::IStr;17use jrsonnet_parser::{LocExpr, ParamsDesc};18use jrsonnet_types::ValType;19use std::{cell::RefCell, fmt::Debug, rc::Rc};2021pub trait LazyValValue: Trace {22 fn get(self: Box<Self>) -> Result<Val>;23}2425#[derive(Trace)]26enum LazyValInternals {27 Computed(Val),28 Errored(LocError),29 Waiting(TraceBox<dyn LazyValValue>),30 Pending,31}3233#[derive(Clone, Trace)]34pub struct LazyVal(Cc<RefCell<LazyValInternals>>);35impl LazyVal {36 pub fn new(f: TraceBox<dyn LazyValValue>) -> Self {37 Self(Cc::new(RefCell::new(LazyValInternals::Waiting(f))))38 }39 pub fn new_resolved(val: Val) -> Self {40 Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))41 }42 pub fn force(&self) -> Result<()> {43 self.evaluate()?;44 Ok(())45 }46 pub fn evaluate(&self) -> Result<Val> {47 match &*self.0.borrow() {48 LazyValInternals::Computed(v) => return Ok(v.clone()),49 LazyValInternals::Errored(e) => return Err(e.clone()),50 LazyValInternals::Pending => return Err(InfiniteRecursionDetected.into()),51 _ => (),52 };53 let value = if let LazyValInternals::Waiting(value) =54 std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)55 {56 value57 } else {58 unreachable!()59 };60 let new_value = match value.0.get() {61 Ok(v) => v,62 Err(e) => {63 *self.0.borrow_mut() = LazyValInternals::Errored(e.clone());64 return Err(e);65 }66 };67 *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());68 Ok(new_value)69 }70}7172impl Debug for LazyVal {73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {74 write!(f, "Lazy")75 }76}77impl PartialEq for LazyVal {78 fn eq(&self, other: &Self) -> bool {79 cc_ptr_eq(&self.0, &other.0)80 }81}8283#[derive(Debug, PartialEq, Trace)]84pub struct FuncDesc {85 pub name: IStr,86 pub ctx: Context,87 pub params: ParamsDesc,88 pub body: LocExpr,89}90impl FuncDesc {91 /// Create body context, but fill arguments without defaults with lazy error92 pub fn default_body_context(&self) -> Context {93 parse_default_function_call(self.ctx.clone(), &self.params)94 }9596 /// Create context, with which body code will run97 pub fn call_body_context(98 &self,99 call_ctx: Context,100 args: &dyn ArgsLike,101 tailstrict: bool,102 ) -> Result<Context> {103 parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)104 }105}106107#[derive(Trace, Clone)]108pub enum FuncVal {109 /// Plain function implemented in jsonnet110 Normal(Cc<FuncDesc>),111 /// Standard library function112 StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),113 /// User-provided function114 Builtin(Cc<TraceBox<dyn Builtin>>),115}116117impl Debug for FuncVal {118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {119 match self {120 Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),121 Self::StaticBuiltin(arg0) => {122 f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()123 }124 Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),125 }126 }127}128129impl FuncVal {130 pub fn args_len(&self) -> usize {131 match self {132 Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),133 Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),134 Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),135 }136 }137 pub fn name(&self) -> IStr {138 match self {139 Self::Normal(normal) => normal.name.clone(),140 Self::StaticBuiltin(builtin) => builtin.name().into(),141 Self::Builtin(builtin) => builtin.name().into(),142 }143 }144 pub fn evaluate(145 &self,146 call_ctx: Context,147 loc: CallLocation,148 args: &dyn ArgsLike,149 tailstrict: bool,150 ) -> Result<Val> {151 match self {152 Self::Normal(func) => {153 let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;154 evaluate(body_ctx, &func.body)155 }156 Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),157 Self::Builtin(b) => b.call(call_ctx, loc, args),158 }159 }160 pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {161 self.evaluate(Context::default(), CallLocation::native(), args, true)162 }163}164165#[derive(Clone)]166pub enum ManifestFormat {167 YamlStream(Box<ManifestFormat>),168 Yaml(usize),169 Json(usize),170 ToString,171 String,172}173174#[derive(Debug, Clone, Trace)]175pub struct Slice {176 pub(crate) inner: ArrValue,177 pub(crate) from: u32,178 pub(crate) to: u32,179 pub(crate) step: u32,180}181impl Slice {182 fn from(&self) -> usize {183 self.from as usize184 }185 fn to(&self) -> usize {186 self.to as usize187 }188 fn step(&self) -> usize {189 self.step as usize190 }191 fn len(&self) -> usize {192 // TODO: use div_ceil193 let diff = self.to() - self.from();194 let rem = diff % self.step();195 let div = diff / self.step();196197 if rem != 0 {198 div + 1199 } else {200 div201 }202 }203}204205#[derive(Debug, Clone, Trace)]206#[force_tracking]207pub enum ArrValue {208 Bytes(#[skip_trace] Rc<[u8]>),209 Lazy(Cc<Vec<LazyVal>>),210 Eager(Cc<Vec<Val>>),211 Extended(Box<(Self, Self)>),212 Range(i32, i32),213 Slice(Box<Slice>),214 Reversed(Box<Self>),215}216impl ArrValue {217 pub fn new_eager() -> Self {218 Self::Eager(Cc::new(Vec::new()))219 }220 pub fn new_range(a: i32, b: i32) -> Self {221 assert!(a <= b);222 Self::Range(a, b)223 }224225 pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {226 let len = self.len();227 let from = from.unwrap_or(0);228 let to = to.unwrap_or(len).min(len);229 let step = step.unwrap_or(1);230 assert!(from < to);231 assert!(step > 0);232233 Self::Slice(Box::new(Slice {234 inner: self,235 from: from as u32,236 to: to as u32,237 step: step as u32,238 }))239 }240241 pub fn len(&self) -> usize {242 match self {243 Self::Bytes(i) => i.len(),244 Self::Lazy(l) => l.len(),245 Self::Eager(e) => e.len(),246 Self::Extended(v) => v.0.len() + v.1.len(),247 Self::Range(a, b) => a.abs_diff(*b) as usize,248 Self::Reversed(i) => i.len(),249 Self::Slice(s) => s.len(),250 }251 }252253 pub fn is_empty(&self) -> bool {254 self.len() == 0255 }256257 pub fn get(&self, index: usize) -> Result<Option<Val>> {258 match self {259 Self::Bytes(i) => i260 .get(index)261 .map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),262 Self::Lazy(vec) => {263 if let Some(v) = vec.get(index) {264 Ok(Some(v.evaluate()?))265 } else {266 Ok(None)267 }268 }269 Self::Eager(vec) => Ok(vec.get(index).cloned()),270 Self::Extended(v) => {271 let a_len = v.0.len();272 if a_len > index {273 v.0.get(index)274 } else {275 v.1.get(index - a_len)276 }277 }278 Self::Range(a, _) => {279 if index >= self.len() {280 return Ok(None);281 }282 Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))283 }284 Self::Reversed(v) => {285 let len = v.len();286 if index >= len {287 return Ok(None);288 }289 v.get(len - index - 1)290 }291 Self::Slice(s) => {292 let index = s.from() + index * s.step();293 if index >= s.to() {294 return Ok(None);295 }296 s.inner.get(index as usize)297 }298 }299 }300301 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {302 match self {303 Self::Bytes(i) => i304 .get(index)305 .map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),306 Self::Lazy(vec) => vec.get(index).cloned(),307 Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),308 Self::Extended(v) => {309 let a_len = v.0.len();310 if a_len > index {311 v.0.get_lazy(index)312 } else {313 v.1.get_lazy(index - a_len)314 }315 }316 Self::Range(a, _) => {317 if index >= self.len() {318 return None;319 }320 Some(LazyVal::new_resolved(Val::Num(321 ((*a as isize) + index as isize) as f64,322 )))323 }324 Self::Reversed(v) => {325 let len = v.len();326 if index >= len {327 return None;328 }329 v.get_lazy(len - index - 1)330 }331 Self::Slice(s) => {332 let index = s.from() + index * s.step();333 if index >= s.to() {334 return None;335 }336 s.inner.get_lazy(index as usize)337 }338 }339 }340341 pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {342 Ok(match self {343 Self::Bytes(i) => {344 let mut out = Vec::with_capacity(i.len());345 for v in i.iter() {346 out.push(Val::Num(*v as f64));347 }348 Cc::new(out)349 }350 Self::Lazy(vec) => {351 let mut out = Vec::with_capacity(vec.len());352 for item in vec.iter() {353 out.push(item.evaluate()?);354 }355 Cc::new(out)356 }357 Self::Eager(vec) => vec.clone(),358 Self::Extended(_v) => {359 let mut out = Vec::with_capacity(self.len());360 for item in self.iter() {361 out.push(item?);362 }363 Cc::new(out)364 }365 Self::Range(a, b) => {366 let mut out = Vec::with_capacity(self.len());367 for i in *a..*b {368 out.push(Val::Num(i as f64));369 }370 Cc::new(out)371 }372 Self::Reversed(r) => {373 let mut r = r.evaluated()?;374 Cc::update_with(&mut r, |v| v.reverse());375 r376 }377 Self::Slice(v) => {378 let mut out = Vec::with_capacity(v.inner.len());379 for v in v380 .inner381 .iter_lazy()382 .skip(v.from())383 .take(v.to() - v.from())384 .step_by(v.step())385 {386 out.push(v.evaluate()?)387 }388 Cc::new(out)389 }390 })391 }392393 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {394 (0..self.len()).map(move |idx| match self {395 Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),396 Self::Lazy(l) => l[idx].evaluate(),397 Self::Eager(e) => Ok(e[idx].clone()),398 Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),399 Self::Range(..) => self.get(idx).map(|e| e.unwrap()),400 Self::Reversed(..) => self.get(idx).map(|e| e.unwrap()),401 Self::Slice(..) => self.get(idx).map(|e| e.unwrap()),402 })403 }404405 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {406 (0..self.len()).map(move |idx| match self {407 Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),408 Self::Lazy(l) => l[idx].clone(),409 Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),410 Self::Extended(_) => self.get_lazy(idx).unwrap(),411 Self::Range(..) => self.get_lazy(idx).unwrap(),412 Self::Reversed(..) => self.get_lazy(idx).unwrap(),413 Self::Slice(..) => self.get_lazy(idx).unwrap(),414 })415 }416417 pub fn reversed(self) -> Self {418 Self::Reversed(Box::new(self))419 }420421 pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {422 let mut out = Vec::with_capacity(self.len());423424 for value in self.iter() {425 out.push(mapper(value?)?);426 }427428 Ok(Self::Eager(Cc::new(out)))429 }430431 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {432 let mut out = Vec::with_capacity(self.len());433434 for value in self.iter() {435 let value = value?;436 if filter(&value)? {437 out.push(value);438 }439 }440441 Ok(Self::Eager(Cc::new(out)))442 }443444 pub fn ptr_eq(a: &Self, b: &Self) -> bool {445 match (a, b) {446 (Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),447 (Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),448 _ => false,449 }450 }451}452453impl From<Vec<LazyVal>> for ArrValue {454 fn from(v: Vec<LazyVal>) -> Self {455 Self::Lazy(Cc::new(v))456 }457}458459impl From<Vec<Val>> for ArrValue {460 fn from(v: Vec<Val>) -> Self {461 Self::Eager(Cc::new(v))462 }463}464465pub enum IndexableVal {466 Str(IStr),467 Arr(ArrValue),468}469470#[derive(Debug, Clone, Trace)]471pub enum Val {472 Bool(bool),473 Null,474 Str(IStr),475 Num(f64),476 Arr(ArrValue),477 Obj(ObjValue),478 Func(FuncVal),479}480481impl Val {482 pub const fn as_bool(&self) -> Option<bool> {483 match self {484 Val::Bool(v) => Some(*v),485 _ => None,486 }487 }488 pub const fn as_null(&self) -> Option<()> {489 match self {490 Val::Null => Some(()),491 _ => None,492 }493 }494 pub fn as_str(&self) -> Option<IStr> {495 match self {496 Val::Str(s) => Some(s.clone()),497 _ => None,498 }499 }500 pub const fn as_num(&self) -> Option<f64> {501 match self {502 Val::Num(n) => Some(*n),503 _ => None,504 }505 }506 pub fn as_arr(&self) -> Option<ArrValue> {507 match self {508 Val::Arr(a) => Some(a.clone()),509 _ => None,510 }511 }512 pub fn as_obj(&self) -> Option<ObjValue> {513 match self {514 Val::Obj(o) => Some(o.clone()),515 _ => None,516 }517 }518 pub fn as_func(&self) -> Option<FuncVal> {519 match self {520 Val::Func(f) => Some(f.clone()),521 _ => None,522 }523 }524525 /// Creates `Val::Num` after checking for numeric overflow.526 /// As numbers are `f64`, we can just check for their finity.527 pub fn new_checked_num(num: f64) -> Result<Self> {528 if num.is_finite() {529 Ok(Self::Num(num))530 } else {531 throw!(RuntimeError("overflow".into()))532 }533 }534535 pub const fn value_type(&self) -> ValType {536 match self {537 Self::Str(..) => ValType::Str,538 Self::Num(..) => ValType::Num,539 Self::Arr(..) => ValType::Arr,540 Self::Obj(..) => ValType::Obj,541 Self::Bool(_) => ValType::Bool,542 Self::Null => ValType::Null,543 Self::Func(..) => ValType::Func,544 }545 }546547 pub fn to_string(&self) -> Result<IStr> {548 Ok(match self {549 Self::Bool(true) => "true".into(),550 Self::Bool(false) => "false".into(),551 Self::Null => "null".into(),552 Self::Str(s) => s.clone(),553 v => manifest_json_ex(554 v,555 &ManifestJsonOptions {556 padding: "",557 mtype: ManifestType::ToString,558 newline: "\n",559 key_val_sep: ": ",560 },561 )?562 .into(),563 })564 }565566 /// Expects value to be object, outputs (key, manifested value) pairs567 pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {568 let obj = match self {569 Self::Obj(obj) => obj,570 _ => throw!(MultiManifestOutputIsNotAObject),571 };572 let keys = obj.fields();573 let mut out = Vec::with_capacity(keys.len());574 for key in keys {575 let value = obj576 .get(key.clone())?577 .expect("item in object")578 .manifest(ty)?;579 out.push((key, value));580 }581 Ok(out)582 }583584 /// Expects value to be array, outputs manifested values585 pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {586 let arr = match self {587 Self::Arr(a) => a,588 _ => throw!(StreamManifestOutputIsNotAArray),589 };590 let mut out = Vec::with_capacity(arr.len());591 for i in arr.iter() {592 out.push(i?.manifest(ty)?);593 }594 Ok(out)595 }596597 pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {598 Ok(match ty {599 ManifestFormat::YamlStream(format) => {600 let arr = match self {601 Self::Arr(a) => a,602 _ => throw!(StreamManifestOutputIsNotAArray),603 };604 let mut out = String::new();605606 match format as &ManifestFormat {607 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),608 ManifestFormat::String => throw!(StreamManifestCannotNestString),609 _ => {}610 };611612 if !arr.is_empty() {613 for v in arr.iter() {614 out.push_str("---\n");615 out.push_str(&v?.manifest(format)?);616 out.push('\n');617 }618 out.push_str("...");619 }620621 out.into()622 }623 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,624 ManifestFormat::Json(padding) => self.to_json(*padding)?,625 ManifestFormat::ToString => self.to_string()?,626 ManifestFormat::String => match self {627 Self::Str(s) => s.clone(),628 _ => throw!(StringManifestOutputIsNotAString),629 },630 })631 }632633 /// For manifestification634 pub fn to_json(&self, padding: usize) -> Result<IStr> {635 manifest_json_ex(636 self,637 &ManifestJsonOptions {638 padding: &" ".repeat(padding),639 mtype: if padding == 0 {640 ManifestType::Minify641 } else {642 ManifestType::Manifest643 },644 newline: "\n",645 key_val_sep: ": ",646 },647 )648 .map(|s| s.into())649 }650651 /// Calls `std.manifestJson`652 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {653 manifest_json_ex(654 self,655 &ManifestJsonOptions {656 padding: &" ".repeat(padding),657 mtype: ManifestType::Std,658 newline: "\n",659 key_val_sep: ": ",660 },661 )662 .map(|s| s.into())663 }664665 pub fn to_yaml(&self, padding: usize) -> Result<IStr> {666 let padding = &" ".repeat(padding);667 manifest_yaml_ex(668 self,669 &ManifestYamlOptions {670 padding,671 arr_element_padding: padding,672 quote_keys: false,673 },674 )675 .map(|s| s.into())676 }677 pub fn into_indexable(self) -> Result<IndexableVal> {678 Ok(match self {679 Val::Str(s) => IndexableVal::Str(s),680 Val::Arr(arr) => IndexableVal::Arr(arr),681 _ => throw!(ValueIsNotIndexable(self.value_type())),682 })683 }684}685686const fn is_function_like(val: &Val) -> bool {687 matches!(val, Val::Func(_))688}689690/// Native implementation of `std.primitiveEquals`691pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {692 Ok(match (val_a, val_b) {693 (Val::Bool(a), Val::Bool(b)) => a == b,694 (Val::Null, Val::Null) => true,695 (Val::Str(a), Val::Str(b)) => a == b,696 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,697 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(698 "primitiveEquals operates on primitive types, got array".into(),699 )),700 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(701 "primitiveEquals operates on primitive types, got object".into(),702 )),703 (a, b) if is_function_like(a) && is_function_like(b) => {704 throw!(RuntimeError("cannot test equality of functions".into()))705 }706 (_, _) => false,707 })708}709710/// Native implementation of `std.equals`711pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {712 if val_a.value_type() != val_b.value_type() {713 return Ok(false);714 }715 match (val_a, val_b) {716 (Val::Arr(a), Val::Arr(b)) => {717 if ArrValue::ptr_eq(a, b) {718 return Ok(true);719 }720 if a.len() != b.len() {721 return Ok(false);722 }723 for (a, b) in a.iter().zip(b.iter()) {724 if !equals(&a?, &b?)? {725 return Ok(false);726 }727 }728 Ok(true)729 }730 (Val::Obj(a), Val::Obj(b)) => {731 if ObjValue::ptr_eq(a, b) {732 return Ok(true);733 }734 let fields = a.fields();735 if fields != b.fields() {736 return Ok(false);737 }738 for field in fields {739 if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {740 return Ok(false);741 }742 }743 Ok(true)744 }745 (a, b) => Ok(primitive_equals(a, b)?),746 }747}1use std::{cell::RefCell, fmt::Debug, rc::Rc};23use gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{LocExpr, ParamsDesc};6use jrsonnet_types::ValType;78use crate::{9 builtin::manifest::{10 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,11 },12 cc_ptr_eq,13 error::{Error::*, LocError},14 evaluate,15 function::{16 parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,17 StaticBuiltin,18 },19 gc::TraceBox,20 throw, Context, ObjValue, Result,21};2223pub trait LazyValValue: Trace {24 fn get(self: Box<Self>) -> Result<Val>;25}2627#[derive(Trace)]28enum LazyValInternals {29 Computed(Val),30 Errored(LocError),31 Waiting(TraceBox<dyn LazyValValue>),32 Pending,33}3435#[derive(Clone, Trace)]36pub struct LazyVal(Cc<RefCell<LazyValInternals>>);37impl LazyVal {38 pub fn new(f: TraceBox<dyn LazyValValue>) -> Self {39 Self(Cc::new(RefCell::new(LazyValInternals::Waiting(f))))40 }41 pub fn new_resolved(val: Val) -> Self {42 Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))43 }44 pub fn force(&self) -> Result<()> {45 self.evaluate()?;46 Ok(())47 }48 pub fn evaluate(&self) -> Result<Val> {49 match &*self.0.borrow() {50 LazyValInternals::Computed(v) => return Ok(v.clone()),51 LazyValInternals::Errored(e) => return Err(e.clone()),52 LazyValInternals::Pending => return Err(InfiniteRecursionDetected.into()),53 _ => (),54 };55 let value = if let LazyValInternals::Waiting(value) =56 std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)57 {58 value59 } else {60 unreachable!()61 };62 let new_value = match value.0.get() {63 Ok(v) => v,64 Err(e) => {65 *self.0.borrow_mut() = LazyValInternals::Errored(e.clone());66 return Err(e);67 }68 };69 *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());70 Ok(new_value)71 }72}7374impl Debug for LazyVal {75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {76 write!(f, "Lazy")77 }78}79impl PartialEq for LazyVal {80 fn eq(&self, other: &Self) -> bool {81 cc_ptr_eq(&self.0, &other.0)82 }83}8485#[derive(Debug, PartialEq, Trace)]86pub struct FuncDesc {87 pub name: IStr,88 pub ctx: Context,89 pub params: ParamsDesc,90 pub body: LocExpr,91}92impl FuncDesc {93 /// Create body context, but fill arguments without defaults with lazy error94 pub fn default_body_context(&self) -> Context {95 parse_default_function_call(self.ctx.clone(), &self.params)96 }9798 /// Create context, with which body code will run99 pub fn call_body_context(100 &self,101 call_ctx: Context,102 args: &dyn ArgsLike,103 tailstrict: bool,104 ) -> Result<Context> {105 parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)106 }107}108109#[derive(Trace, Clone)]110pub enum FuncVal {111 /// Plain function implemented in jsonnet112 Normal(Cc<FuncDesc>),113 /// Standard library function114 StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),115 /// User-provided function116 Builtin(Cc<TraceBox<dyn Builtin>>),117}118119impl Debug for FuncVal {120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {121 match self {122 Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),123 Self::StaticBuiltin(arg0) => {124 f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()125 }126 Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),127 }128 }129}130131impl FuncVal {132 pub fn args_len(&self) -> usize {133 match self {134 Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),135 Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),136 Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),137 }138 }139 pub fn name(&self) -> IStr {140 match self {141 Self::Normal(normal) => normal.name.clone(),142 Self::StaticBuiltin(builtin) => builtin.name().into(),143 Self::Builtin(builtin) => builtin.name().into(),144 }145 }146 pub fn evaluate(147 &self,148 call_ctx: Context,149 loc: CallLocation,150 args: &dyn ArgsLike,151 tailstrict: bool,152 ) -> Result<Val> {153 match self {154 Self::Normal(func) => {155 let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;156 evaluate(body_ctx, &func.body)157 }158 Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),159 Self::Builtin(b) => b.call(call_ctx, loc, args),160 }161 }162 pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {163 self.evaluate(Context::default(), CallLocation::native(), args, true)164 }165}166167#[derive(Clone)]168pub enum ManifestFormat {169 YamlStream(Box<ManifestFormat>),170 Yaml(usize),171 Json(usize),172 ToString,173 String,174}175176#[derive(Debug, Clone, Trace)]177pub struct Slice {178 pub(crate) inner: ArrValue,179 pub(crate) from: u32,180 pub(crate) to: u32,181 pub(crate) step: u32,182}183impl Slice {184 fn from(&self) -> usize {185 self.from as usize186 }187 fn to(&self) -> usize {188 self.to as usize189 }190 fn step(&self) -> usize {191 self.step as usize192 }193 fn len(&self) -> usize {194 // TODO: use div_ceil195 let diff = self.to() - self.from();196 let rem = diff % self.step();197 let div = diff / self.step();198199 if rem != 0 {200 div + 1201 } else {202 div203 }204 }205}206207#[derive(Debug, Clone, Trace)]208#[force_tracking]209pub enum ArrValue {210 Bytes(#[skip_trace] Rc<[u8]>),211 Lazy(Cc<Vec<LazyVal>>),212 Eager(Cc<Vec<Val>>),213 Extended(Box<(Self, Self)>),214 Range(i32, i32),215 Slice(Box<Slice>),216 Reversed(Box<Self>),217}218impl ArrValue {219 pub fn new_eager() -> Self {220 Self::Eager(Cc::new(Vec::new()))221 }222 pub fn new_range(a: i32, b: i32) -> Self {223 assert!(a <= b);224 Self::Range(a, b)225 }226227 pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {228 let len = self.len();229 let from = from.unwrap_or(0);230 let to = to.unwrap_or(len).min(len);231 let step = step.unwrap_or(1);232 assert!(from < to);233 assert!(step > 0);234235 Self::Slice(Box::new(Slice {236 inner: self,237 from: from as u32,238 to: to as u32,239 step: step as u32,240 }))241 }242243 pub fn len(&self) -> usize {244 match self {245 Self::Bytes(i) => i.len(),246 Self::Lazy(l) => l.len(),247 Self::Eager(e) => e.len(),248 Self::Extended(v) => v.0.len() + v.1.len(),249 Self::Range(a, b) => a.abs_diff(*b) as usize,250 Self::Reversed(i) => i.len(),251 Self::Slice(s) => s.len(),252 }253 }254255 pub fn is_empty(&self) -> bool {256 self.len() == 0257 }258259 pub fn get(&self, index: usize) -> Result<Option<Val>> {260 match self {261 Self::Bytes(i) => i262 .get(index)263 .map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),264 Self::Lazy(vec) => {265 if let Some(v) = vec.get(index) {266 Ok(Some(v.evaluate()?))267 } else {268 Ok(None)269 }270 }271 Self::Eager(vec) => Ok(vec.get(index).cloned()),272 Self::Extended(v) => {273 let a_len = v.0.len();274 if a_len > index {275 v.0.get(index)276 } else {277 v.1.get(index - a_len)278 }279 }280 Self::Range(a, _) => {281 if index >= self.len() {282 return Ok(None);283 }284 Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))285 }286 Self::Reversed(v) => {287 let len = v.len();288 if index >= len {289 return Ok(None);290 }291 v.get(len - index - 1)292 }293 Self::Slice(s) => {294 let index = s.from() + index * s.step();295 if index >= s.to() {296 return Ok(None);297 }298 s.inner.get(index as usize)299 }300 }301 }302303 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {304 match self {305 Self::Bytes(i) => i306 .get(index)307 .map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),308 Self::Lazy(vec) => vec.get(index).cloned(),309 Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),310 Self::Extended(v) => {311 let a_len = v.0.len();312 if a_len > index {313 v.0.get_lazy(index)314 } else {315 v.1.get_lazy(index - a_len)316 }317 }318 Self::Range(a, _) => {319 if index >= self.len() {320 return None;321 }322 Some(LazyVal::new_resolved(Val::Num(323 ((*a as isize) + index as isize) as f64,324 )))325 }326 Self::Reversed(v) => {327 let len = v.len();328 if index >= len {329 return None;330 }331 v.get_lazy(len - index - 1)332 }333 Self::Slice(s) => {334 let index = s.from() + index * s.step();335 if index >= s.to() {336 return None;337 }338 s.inner.get_lazy(index as usize)339 }340 }341 }342343 pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {344 Ok(match self {345 Self::Bytes(i) => {346 let mut out = Vec::with_capacity(i.len());347 for v in i.iter() {348 out.push(Val::Num(*v as f64));349 }350 Cc::new(out)351 }352 Self::Lazy(vec) => {353 let mut out = Vec::with_capacity(vec.len());354 for item in vec.iter() {355 out.push(item.evaluate()?);356 }357 Cc::new(out)358 }359 Self::Eager(vec) => vec.clone(),360 Self::Extended(_v) => {361 let mut out = Vec::with_capacity(self.len());362 for item in self.iter() {363 out.push(item?);364 }365 Cc::new(out)366 }367 Self::Range(a, b) => {368 let mut out = Vec::with_capacity(self.len());369 for i in *a..*b {370 out.push(Val::Num(i as f64));371 }372 Cc::new(out)373 }374 Self::Reversed(r) => {375 let mut r = r.evaluated()?;376 Cc::update_with(&mut r, |v| v.reverse());377 r378 }379 Self::Slice(v) => {380 let mut out = Vec::with_capacity(v.inner.len());381 for v in v382 .inner383 .iter_lazy()384 .skip(v.from())385 .take(v.to() - v.from())386 .step_by(v.step())387 {388 out.push(v.evaluate()?)389 }390 Cc::new(out)391 }392 })393 }394395 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {396 (0..self.len()).map(move |idx| match self {397 Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),398 Self::Lazy(l) => l[idx].evaluate(),399 Self::Eager(e) => Ok(e[idx].clone()),400 Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),401 Self::Range(..) => self.get(idx).map(|e| e.unwrap()),402 Self::Reversed(..) => self.get(idx).map(|e| e.unwrap()),403 Self::Slice(..) => self.get(idx).map(|e| e.unwrap()),404 })405 }406407 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {408 (0..self.len()).map(move |idx| match self {409 Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),410 Self::Lazy(l) => l[idx].clone(),411 Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),412 Self::Extended(_) => self.get_lazy(idx).unwrap(),413 Self::Range(..) => self.get_lazy(idx).unwrap(),414 Self::Reversed(..) => self.get_lazy(idx).unwrap(),415 Self::Slice(..) => self.get_lazy(idx).unwrap(),416 })417 }418419 pub fn reversed(self) -> Self {420 Self::Reversed(Box::new(self))421 }422423 pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {424 let mut out = Vec::with_capacity(self.len());425426 for value in self.iter() {427 out.push(mapper(value?)?);428 }429430 Ok(Self::Eager(Cc::new(out)))431 }432433 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {434 let mut out = Vec::with_capacity(self.len());435436 for value in self.iter() {437 let value = value?;438 if filter(&value)? {439 out.push(value);440 }441 }442443 Ok(Self::Eager(Cc::new(out)))444 }445446 pub fn ptr_eq(a: &Self, b: &Self) -> bool {447 match (a, b) {448 (Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),449 (Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),450 _ => false,451 }452 }453}454455impl From<Vec<LazyVal>> for ArrValue {456 fn from(v: Vec<LazyVal>) -> Self {457 Self::Lazy(Cc::new(v))458 }459}460461impl From<Vec<Val>> for ArrValue {462 fn from(v: Vec<Val>) -> Self {463 Self::Eager(Cc::new(v))464 }465}466467pub enum IndexableVal {468 Str(IStr),469 Arr(ArrValue),470}471472#[derive(Debug, Clone, Trace)]473pub enum Val {474 Bool(bool),475 Null,476 Str(IStr),477 Num(f64),478 Arr(ArrValue),479 Obj(ObjValue),480 Func(FuncVal),481}482483impl Val {484 pub const fn as_bool(&self) -> Option<bool> {485 match self {486 Val::Bool(v) => Some(*v),487 _ => None,488 }489 }490 pub const fn as_null(&self) -> Option<()> {491 match self {492 Val::Null => Some(()),493 _ => None,494 }495 }496 pub fn as_str(&self) -> Option<IStr> {497 match self {498 Val::Str(s) => Some(s.clone()),499 _ => None,500 }501 }502 pub const fn as_num(&self) -> Option<f64> {503 match self {504 Val::Num(n) => Some(*n),505 _ => None,506 }507 }508 pub fn as_arr(&self) -> Option<ArrValue> {509 match self {510 Val::Arr(a) => Some(a.clone()),511 _ => None,512 }513 }514 pub fn as_obj(&self) -> Option<ObjValue> {515 match self {516 Val::Obj(o) => Some(o.clone()),517 _ => None,518 }519 }520 pub fn as_func(&self) -> Option<FuncVal> {521 match self {522 Val::Func(f) => Some(f.clone()),523 _ => None,524 }525 }526527 /// Creates `Val::Num` after checking for numeric overflow.528 /// As numbers are `f64`, we can just check for their finity.529 pub fn new_checked_num(num: f64) -> Result<Self> {530 if num.is_finite() {531 Ok(Self::Num(num))532 } else {533 throw!(RuntimeError("overflow".into()))534 }535 }536537 pub const fn value_type(&self) -> ValType {538 match self {539 Self::Str(..) => ValType::Str,540 Self::Num(..) => ValType::Num,541 Self::Arr(..) => ValType::Arr,542 Self::Obj(..) => ValType::Obj,543 Self::Bool(_) => ValType::Bool,544 Self::Null => ValType::Null,545 Self::Func(..) => ValType::Func,546 }547 }548549 pub fn to_string(&self) -> Result<IStr> {550 Ok(match self {551 Self::Bool(true) => "true".into(),552 Self::Bool(false) => "false".into(),553 Self::Null => "null".into(),554 Self::Str(s) => s.clone(),555 v => manifest_json_ex(556 v,557 &ManifestJsonOptions {558 padding: "",559 mtype: ManifestType::ToString,560 newline: "\n",561 key_val_sep: ": ",562 },563 )?564 .into(),565 })566 }567568 /// Expects value to be object, outputs (key, manifested value) pairs569 pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {570 let obj = match self {571 Self::Obj(obj) => obj,572 _ => throw!(MultiManifestOutputIsNotAObject),573 };574 let keys = obj.fields();575 let mut out = Vec::with_capacity(keys.len());576 for key in keys {577 let value = obj578 .get(key.clone())?579 .expect("item in object")580 .manifest(ty)?;581 out.push((key, value));582 }583 Ok(out)584 }585586 /// Expects value to be array, outputs manifested values587 pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {588 let arr = match self {589 Self::Arr(a) => a,590 _ => throw!(StreamManifestOutputIsNotAArray),591 };592 let mut out = Vec::with_capacity(arr.len());593 for i in arr.iter() {594 out.push(i?.manifest(ty)?);595 }596 Ok(out)597 }598599 pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {600 Ok(match ty {601 ManifestFormat::YamlStream(format) => {602 let arr = match self {603 Self::Arr(a) => a,604 _ => throw!(StreamManifestOutputIsNotAArray),605 };606 let mut out = String::new();607608 match format as &ManifestFormat {609 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),610 ManifestFormat::String => throw!(StreamManifestCannotNestString),611 _ => {}612 };613614 if !arr.is_empty() {615 for v in arr.iter() {616 out.push_str("---\n");617 out.push_str(&v?.manifest(format)?);618 out.push('\n');619 }620 out.push_str("...");621 }622623 out.into()624 }625 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,626 ManifestFormat::Json(padding) => self.to_json(*padding)?,627 ManifestFormat::ToString => self.to_string()?,628 ManifestFormat::String => match self {629 Self::Str(s) => s.clone(),630 _ => throw!(StringManifestOutputIsNotAString),631 },632 })633 }634635 /// For manifestification636 pub fn to_json(&self, padding: usize) -> Result<IStr> {637 manifest_json_ex(638 self,639 &ManifestJsonOptions {640 padding: &" ".repeat(padding),641 mtype: if padding == 0 {642 ManifestType::Minify643 } else {644 ManifestType::Manifest645 },646 newline: "\n",647 key_val_sep: ": ",648 },649 )650 .map(|s| s.into())651 }652653 /// Calls `std.manifestJson`654 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {655 manifest_json_ex(656 self,657 &ManifestJsonOptions {658 padding: &" ".repeat(padding),659 mtype: ManifestType::Std,660 newline: "\n",661 key_val_sep: ": ",662 },663 )664 .map(|s| s.into())665 }666667 pub fn to_yaml(&self, padding: usize) -> Result<IStr> {668 let padding = &" ".repeat(padding);669 manifest_yaml_ex(670 self,671 &ManifestYamlOptions {672 padding,673 arr_element_padding: padding,674 quote_keys: false,675 },676 )677 .map(|s| s.into())678 }679 pub fn into_indexable(self) -> Result<IndexableVal> {680 Ok(match self {681 Val::Str(s) => IndexableVal::Str(s),682 Val::Arr(arr) => IndexableVal::Arr(arr),683 _ => throw!(ValueIsNotIndexable(self.value_type())),684 })685 }686}687688const fn is_function_like(val: &Val) -> bool {689 matches!(val, Val::Func(_))690}691692/// Native implementation of `std.primitiveEquals`693pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {694 Ok(match (val_a, val_b) {695 (Val::Bool(a), Val::Bool(b)) => a == b,696 (Val::Null, Val::Null) => true,697 (Val::Str(a), Val::Str(b)) => a == b,698 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,699 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(700 "primitiveEquals operates on primitive types, got array".into(),701 )),702 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(703 "primitiveEquals operates on primitive types, got object".into(),704 )),705 (a, b) if is_function_like(a) && is_function_like(b) => {706 throw!(RuntimeError("cannot test equality of functions".into()))707 }708 (_, _) => false,709 })710}711712/// Native implementation of `std.equals`713pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {714 if val_a.value_type() != val_b.value_type() {715 return Ok(false);716 }717 match (val_a, val_b) {718 (Val::Arr(a), Val::Arr(b)) => {719 if ArrValue::ptr_eq(a, b) {720 return Ok(true);721 }722 if a.len() != b.len() {723 return Ok(false);724 }725 for (a, b) in a.iter().zip(b.iter()) {726 if !equals(&a?, &b?)? {727 return Ok(false);728 }729 }730 Ok(true)731 }732 (Val::Obj(a), Val::Obj(b)) => {733 if ObjValue::ptr_eq(a, b) {734 return Ok(true);735 }736 let fields = a.fields();737 if fields != b.fields() {738 return Ok(false);739 }740 for field in fields {741 if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {742 return Ok(false);743 }744 }745 Ok(true)746 }747 (a, b) => Ok(primitive_equals(a, b)?),748 }749}crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -1,6 +1,3 @@
-use gcmodule::Trace;
-use rustc_hash::FxHashMap;
-use serde::{Deserialize, Serialize};
use std::{
borrow::Cow,
cell::RefCell,
@@ -12,6 +9,10 @@
str::Utf8Error,
};
+use gcmodule::Trace;
+use rustc_hash::FxHashMap;
+use serde::{Deserialize, Serialize};
+
#[derive(Clone, PartialOrd, Ord, Eq)]
pub struct IStr(Rc<str>);
impl Trace for IStr {
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -1,7 +1,3 @@
-use gcmodule::Trace;
-use jrsonnet_interner::IStr;
-#[cfg(feature = "serde")]
-use serde::{Deserialize, Serialize};
use std::{
fmt::{Debug, Display},
ops::Deref,
@@ -9,6 +5,11 @@
rc::Rc,
};
+use gcmodule::Trace;
+use jrsonnet_interner::IStr;
+#[cfg(feature = "serde")]
+use serde::{Deserialize, Serialize};
+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Trace)]
pub enum FieldName {
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -1,10 +1,11 @@
#![allow(clippy::redundant_closure_call)]
-use peg::parser;
use std::{
path::{Path, PathBuf},
rc::Rc,
};
+
+use peg::parser;
mod expr;
pub use expr::*;
pub use jrsonnet_interner::IStr;
@@ -317,11 +318,13 @@
#[cfg(test)]
pub mod tests {
- use super::{expr::*, parse};
- use crate::ParserSettings;
use std::path::PathBuf;
+
use BinaryOpType::*;
+ use super::{expr::*, parse};
+ use crate::ParserSettings;
+
macro_rules! parse {
($s:expr) => {
parse(
crates/jrsonnet-types/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-types/src/lib.rs
+++ b/crates/jrsonnet-types/src/lib.rs
@@ -1,8 +1,9 @@
#![allow(clippy::redundant_closure_call)]
-use gcmodule::Trace;
use std::fmt::Display;
+use gcmodule::Trace;
+
#[macro_export]
macro_rules! ty {
((Array<number>)) => {{