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.rsdiffbeforeafterboth1use crate::error::LocError;2use crate::function::CallLocation;3use crate::gc::{GcHashMap, GcHashSet, TraceBox};4use crate::operator::evaluate_add_op;5use crate::push_frame;6use crate::{7 cc_ptr_eq, error::Error::*, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal,8 Result, Val,9};10use gcmodule::{Cc, Trace, Weak};11use jrsonnet_interner::IStr;12use jrsonnet_parser::{ExprLocation, Visibility};13use rustc_hash::FxHashMap;14use std::cell::RefCell;15use std::fmt::Debug;16use std::hash::{Hash, Hasher};1718#[derive(Debug, Trace)]19pub struct ObjMember {20 pub add: bool,21 pub visibility: Visibility,22 pub invoke: LazyBinding,23 pub location: Option<ExprLocation>,24}2526pub trait ObjectAssertion: Trace {27 fn run(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<()>;28}2930// Field => This31type CacheKey = (IStr, WeakObjValue);3233#[derive(Trace)]34enum CacheValue {35 Cached(Val),36 NotFound,37 Pending,38 Errored(LocError),39}4041#[derive(Trace)]42#[force_tracking]43pub struct ObjValueInternals {44 super_obj: Option<ObjValue>,45 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,46 assertions_ran: RefCell<GcHashSet<ObjValue>>,47 this_obj: Option<ObjValue>,48 this_entries: Cc<GcHashMap<IStr, ObjMember>>,49 value_cache: RefCell<GcHashMap<CacheKey, CacheValue>>,50}5152#[derive(Clone, Trace)]53pub struct WeakObjValue(#[skip_trace] pub(crate) Weak<ObjValueInternals>);5455impl PartialEq for WeakObjValue {56 fn eq(&self, other: &Self) -> bool {57 weak_ptr_eq(self.0.clone(), other.0.clone())58 }59}6061impl Eq for WeakObjValue {}62impl Hash for WeakObjValue {63 fn hash<H: Hasher>(&self, hasher: &mut H) {64 hasher.write_usize(weak_raw(self.0.clone()) as usize)65 }66}6768#[derive(Clone, Trace)]69pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);70impl Debug for ObjValue {71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {72 if let Some(super_obj) = self.0.super_obj.as_ref() {73 if f.alternate() {74 write!(f, "{:#?}", super_obj)?;75 } else {76 write!(f, "{:?}", super_obj)?;77 }78 write!(f, " + ")?;79 }80 let mut debug = f.debug_struct("ObjValue");81 for (name, member) in self.0.this_entries.iter() {82 debug.field(name, member);83 }84 debug.finish_non_exhaustive()85 }86}8788impl ObjValue {89 pub fn new(90 super_obj: Option<Self>,91 this_entries: Cc<GcHashMap<IStr, ObjMember>>,92 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,93 ) -> Self {94 Self(Cc::new(ObjValueInternals {95 super_obj,96 assertions,97 assertions_ran: RefCell::new(GcHashSet::new()),98 this_obj: None,99 this_entries,100 value_cache: RefCell::new(GcHashMap::new()),101 }))102 }103 pub fn new_empty() -> Self {104 Self::new(None, Cc::new(GcHashMap::new()), Cc::new(Vec::new()))105 }106 pub fn extend_from(&self, super_obj: Self) -> Self {107 match &self.0.super_obj {108 None => Self::new(109 Some(super_obj),110 self.0.this_entries.clone(),111 self.0.assertions.clone(),112 ),113 Some(v) => Self::new(114 Some(v.extend_from(super_obj)),115 self.0.this_entries.clone(),116 self.0.assertions.clone(),117 ),118 }119 }120 pub fn with_this(&self, this_obj: Self) -> Self {121 Self(Cc::new(ObjValueInternals {122 super_obj: self.0.super_obj.clone(),123 assertions: self.0.assertions.clone(),124 assertions_ran: RefCell::new(GcHashSet::new()),125 this_obj: Some(this_obj),126 this_entries: self.0.this_entries.clone(),127 value_cache: RefCell::new(GcHashMap::new()),128 }))129 }130131 pub fn is_empty(&self) -> bool {132 if !self.0.this_entries.is_empty() {133 return false;134 }135 self.0136 .super_obj137 .as_ref()138 .map(|s| s.is_empty())139 .unwrap_or(true)140 }141142 /// Run callback for every field found in object143 pub(crate) fn enum_fields(&self, handler: &mut impl FnMut(&IStr, &ObjMember) -> bool) -> bool {144 if let Some(s) = &self.0.super_obj {145 if s.enum_fields(handler) {146 return true;147 }148 }149 for (name, member) in self.0.this_entries.iter() {150 if handler(name, member) {151 return true;152 }153 }154 false155 }156157 pub fn fields_visibility(&self) -> FxHashMap<IStr, bool> {158 let mut out = FxHashMap::default();159 self.enum_fields(&mut |name, member| {160 match member.visibility {161 Visibility::Normal => {162 let entry = out.entry(name.to_owned());163 entry.or_insert(true);164 }165 Visibility::Hidden => {166 out.insert(name.to_owned(), false);167 }168 Visibility::Unhide => {169 out.insert(name.to_owned(), true);170 }171 };172 false173 });174 out175 }176 pub fn fields_ex(&self, include_hidden: bool) -> Vec<IStr> {177 let mut fields: Vec<_> = self178 .fields_visibility()179 .into_iter()180 .filter(|(_k, v)| include_hidden || *v)181 .map(|(k, _)| k)182 .collect();183 fields.sort_unstable();184 fields185 }186 pub fn fields(&self) -> Vec<IStr> {187 self.fields_ex(false)188 }189190 pub fn field_visibility(&self, name: IStr) -> Option<Visibility> {191 if let Some(m) = self.0.this_entries.get(&name) {192 Some(match &m.visibility {193 Visibility::Normal => self194 .0195 .super_obj196 .as_ref()197 .and_then(|super_obj| super_obj.field_visibility(name))198 .unwrap_or(Visibility::Normal),199 v => *v,200 })201 } else if let Some(super_obj) = &self.0.super_obj {202 super_obj.field_visibility(name)203 } else {204 None205 }206 }207208 fn has_field_include_hidden(&self, name: IStr) -> bool {209 if self.0.this_entries.contains_key(&name) {210 true211 } else if let Some(super_obj) = &self.0.super_obj {212 super_obj.has_field_include_hidden(name)213 } else {214 false215 }216 }217218 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {219 if include_hidden {220 self.has_field_include_hidden(name)221 } else {222 self.has_field(name)223 }224 }225 pub fn has_field(&self, name: IStr) -> bool {226 self.field_visibility(name)227 .map(|v| v.is_visible())228 .unwrap_or(false)229 }230231 pub fn get(&self, key: IStr) -> Result<Option<Val>> {232 self.run_assertions()?;233 self.get_raw(key, self.0.this_obj.as_ref())234 }235236 pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {237 let mut new = GcHashMap::with_capacity(1);238 new.insert(key, value);239 Self::new(Some(self), Cc::new(new), Cc::new(Vec::new()))240 }241242 fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {243 let real_this = real_this.unwrap_or(self);244 let cache_key = (key.clone(), WeakObjValue(real_this.0.downgrade()));245246 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {247 return Ok(match v {248 CacheValue::Cached(v) => Some(v.clone()),249 CacheValue::NotFound => None,250 CacheValue::Pending => throw!(InfiniteRecursionDetected),251 CacheValue::Errored(e) => return Err(e.clone()),252 });253 }254 self.0255 .value_cache256 .borrow_mut()257 .insert(cache_key.clone(), CacheValue::Pending);258 let value = match (self.0.this_entries.get(&key), &self.0.super_obj) {259 (Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),260 (Some(k), Some(s)) => {261 let our = self.evaluate_this(k, real_this)?;262 if k.add {263 s.get_raw(key, Some(real_this))?264 .map_or(Ok(Some(our.clone())), |v| {265 Ok(Some(evaluate_add_op(&v, &our)?))266 })267 } else {268 Ok(Some(our))269 }270 }271 (None, Some(s)) => s.get_raw(key, Some(real_this)),272 (None, None) => Ok(None),273 };274 let value = match value {275 Ok(v) => v,276 Err(e) => {277 self.0278 .value_cache279 .borrow_mut()280 .insert(cache_key, CacheValue::Errored(e.clone()));281 return Err(e);282 }283 };284 self.0.value_cache.borrow_mut().insert(285 cache_key,286 match &value {287 Some(v) => CacheValue::Cached(v.clone()),288 None => CacheValue::NotFound,289 },290 );291 Ok(value)292 }293 fn evaluate_this(&self, v: &ObjMember, real_this: &Self) -> Result<Val> {294 v.invoke295 .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?296 .evaluate()297 }298299 fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {300 if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {301 for assertion in self.0.assertions.iter() {302 if let Err(e) = assertion.run(Some(real_this.clone()), self.0.super_obj.clone()) {303 self.0.assertions_ran.borrow_mut().remove(real_this);304 return Err(e);305 }306 }307 if let Some(super_obj) = &self.0.super_obj {308 super_obj.run_assertions_raw(real_this)?;309 }310 }311 Ok(())312 }313 pub fn run_assertions(&self) -> Result<()> {314 self.run_assertions_raw(self)315 }316317 pub fn ptr_eq(a: &Self, b: &Self) -> bool {318 cc_ptr_eq(&a.0, &b.0)319 }320}321322impl PartialEq for ObjValue {323 fn eq(&self, other: &Self) -> bool {324 cc_ptr_eq(&self.0, &other.0)325 }326}327328impl Eq for ObjValue {}329impl Hash for ObjValue {330 fn hash<H: Hasher>(&self, hasher: &mut H) {331 hasher.write_usize(&*self.0 as *const _ as usize)332 }333}334335pub struct ObjValueBuilder {336 super_obj: Option<ObjValue>,337 map: GcHashMap<IStr, ObjMember>,338 assertions: Vec<TraceBox<dyn ObjectAssertion>>,339}340impl ObjValueBuilder {341 pub fn new() -> Self {342 Self::with_capacity(0)343 }344 pub fn with_capacity(capacity: usize) -> Self {345 Self {346 super_obj: None,347 map: GcHashMap::with_capacity(capacity),348 assertions: Vec::new(),349 }350 }351 pub fn reserve_asserts(&mut self, capacity: usize) -> &mut Self {352 self.assertions.reserve_exact(capacity);353 self354 }355 pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {356 self.super_obj = Some(super_obj);357 self358 }359360 pub fn assert(&mut self, assertion: TraceBox<dyn ObjectAssertion>) -> &mut Self {361 self.assertions.push(assertion);362 self363 }364 pub fn member(&mut self, name: IStr) -> ObjMemberBuilder {365 ObjMemberBuilder {366 value: self,367 name,368 add: false,369 visibility: Visibility::Normal,370 location: None,371 }372 }373374 pub fn build(self) -> ObjValue {375 ObjValue::new(self.super_obj, Cc::new(self.map), Cc::new(self.assertions))376 }377}378impl Default for ObjValueBuilder {379 fn default() -> Self {380 Self::with_capacity(0)381 }382}383384#[must_use = "value not added unless binding() was called"]385pub struct ObjMemberBuilder<'v> {386 value: &'v mut ObjValueBuilder,387 name: IStr,388 add: bool,389 visibility: Visibility,390 location: Option<ExprLocation>,391}392393#[allow(clippy::missing_const_for_fn)]394impl<'v> ObjMemberBuilder<'v> {395 pub const fn with_add(mut self, add: bool) -> Self {396 self.add = add;397 self398 }399 pub fn add(self) -> Self {400 self.with_add(true)401 }402 pub fn with_visibility(mut self, visibility: Visibility) -> Self {403 self.visibility = visibility;404 self405 }406 pub fn hide(self) -> Self {407 self.with_visibility(Visibility::Hidden)408 }409 pub fn with_location(mut self, location: ExprLocation) -> Self {410 self.location = Some(location);411 self412 }413 pub fn value(self, value: Val) -> Result<()> {414 self.binding(LazyBinding::Bound(LazyVal::new_resolved(value)))415 }416 pub fn bindable(self, bindable: TraceBox<dyn Bindable>) -> Result<()> {417 self.binding(LazyBinding::Bindable(Cc::new(bindable)))418 }419 pub fn binding(self, binding: LazyBinding) -> Result<()> {420 let old = self.value.map.insert(421 self.name.clone(),422 ObjMember {423 add: self.add,424 visibility: self.visibility,425 invoke: binding,426 location: self.location.clone(),427 },428 );429 if old.is_some() {430 push_frame(431 CallLocation(self.location.as_ref()),432 || format!("field <{}> initializtion", self.name.clone()),433 || throw!(DuplicateFieldName(self.name.clone())),434 )?435 }436 Ok(())437 }438}1use std::{2 cell::RefCell,3 fmt::Debug,4 hash::{Hash, Hasher},5};67use gcmodule::{Cc, Trace, Weak};8use jrsonnet_interner::IStr;9use jrsonnet_parser::{ExprLocation, Visibility};10use rustc_hash::FxHashMap;1112use crate::{13 cc_ptr_eq,14 error::{Error::*, LocError},15 function::CallLocation,16 gc::{GcHashMap, GcHashSet, TraceBox},17 operator::evaluate_add_op,18 push_frame, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal, Result, Val,19};2021#[derive(Debug, Trace)]22pub struct ObjMember {23 pub add: bool,24 pub visibility: Visibility,25 pub invoke: LazyBinding,26 pub location: Option<ExprLocation>,27}2829pub trait ObjectAssertion: Trace {30 fn run(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<()>;31}3233// Field => This34type CacheKey = (IStr, WeakObjValue);3536#[derive(Trace)]37enum CacheValue {38 Cached(Val),39 NotFound,40 Pending,41 Errored(LocError),42}4344#[derive(Trace)]45#[force_tracking]46pub struct ObjValueInternals {47 super_obj: Option<ObjValue>,48 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,49 assertions_ran: RefCell<GcHashSet<ObjValue>>,50 this_obj: Option<ObjValue>,51 this_entries: Cc<GcHashMap<IStr, ObjMember>>,52 value_cache: RefCell<GcHashMap<CacheKey, CacheValue>>,53}5455#[derive(Clone, Trace)]56pub struct WeakObjValue(#[skip_trace] pub(crate) Weak<ObjValueInternals>);5758impl PartialEq for WeakObjValue {59 fn eq(&self, other: &Self) -> bool {60 weak_ptr_eq(self.0.clone(), other.0.clone())61 }62}6364impl Eq for WeakObjValue {}65impl Hash for WeakObjValue {66 fn hash<H: Hasher>(&self, hasher: &mut H) {67 hasher.write_usize(weak_raw(self.0.clone()) as usize)68 }69}7071#[derive(Clone, Trace)]72pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);73impl Debug for ObjValue {74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {75 if let Some(super_obj) = self.0.super_obj.as_ref() {76 if f.alternate() {77 write!(f, "{:#?}", super_obj)?;78 } else {79 write!(f, "{:?}", super_obj)?;80 }81 write!(f, " + ")?;82 }83 let mut debug = f.debug_struct("ObjValue");84 for (name, member) in self.0.this_entries.iter() {85 debug.field(name, member);86 }87 debug.finish_non_exhaustive()88 }89}9091impl ObjValue {92 pub fn new(93 super_obj: Option<Self>,94 this_entries: Cc<GcHashMap<IStr, ObjMember>>,95 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,96 ) -> Self {97 Self(Cc::new(ObjValueInternals {98 super_obj,99 assertions,100 assertions_ran: RefCell::new(GcHashSet::new()),101 this_obj: None,102 this_entries,103 value_cache: RefCell::new(GcHashMap::new()),104 }))105 }106 pub fn new_empty() -> Self {107 Self::new(None, Cc::new(GcHashMap::new()), Cc::new(Vec::new()))108 }109 pub fn extend_from(&self, super_obj: Self) -> Self {110 match &self.0.super_obj {111 None => Self::new(112 Some(super_obj),113 self.0.this_entries.clone(),114 self.0.assertions.clone(),115 ),116 Some(v) => Self::new(117 Some(v.extend_from(super_obj)),118 self.0.this_entries.clone(),119 self.0.assertions.clone(),120 ),121 }122 }123 pub fn with_this(&self, this_obj: Self) -> Self {124 Self(Cc::new(ObjValueInternals {125 super_obj: self.0.super_obj.clone(),126 assertions: self.0.assertions.clone(),127 assertions_ran: RefCell::new(GcHashSet::new()),128 this_obj: Some(this_obj),129 this_entries: self.0.this_entries.clone(),130 value_cache: RefCell::new(GcHashMap::new()),131 }))132 }133134 pub fn is_empty(&self) -> bool {135 if !self.0.this_entries.is_empty() {136 return false;137 }138 self.0139 .super_obj140 .as_ref()141 .map(|s| s.is_empty())142 .unwrap_or(true)143 }144145 /// Run callback for every field found in object146 pub(crate) fn enum_fields(&self, handler: &mut impl FnMut(&IStr, &ObjMember) -> bool) -> bool {147 if let Some(s) = &self.0.super_obj {148 if s.enum_fields(handler) {149 return true;150 }151 }152 for (name, member) in self.0.this_entries.iter() {153 if handler(name, member) {154 return true;155 }156 }157 false158 }159160 pub fn fields_visibility(&self) -> FxHashMap<IStr, bool> {161 let mut out = FxHashMap::default();162 self.enum_fields(&mut |name, member| {163 match member.visibility {164 Visibility::Normal => {165 let entry = out.entry(name.to_owned());166 entry.or_insert(true);167 }168 Visibility::Hidden => {169 out.insert(name.to_owned(), false);170 }171 Visibility::Unhide => {172 out.insert(name.to_owned(), true);173 }174 };175 false176 });177 out178 }179 pub fn fields_ex(&self, include_hidden: bool) -> Vec<IStr> {180 let mut fields: Vec<_> = self181 .fields_visibility()182 .into_iter()183 .filter(|(_k, v)| include_hidden || *v)184 .map(|(k, _)| k)185 .collect();186 fields.sort_unstable();187 fields188 }189 pub fn fields(&self) -> Vec<IStr> {190 self.fields_ex(false)191 }192193 pub fn field_visibility(&self, name: IStr) -> Option<Visibility> {194 if let Some(m) = self.0.this_entries.get(&name) {195 Some(match &m.visibility {196 Visibility::Normal => self197 .0198 .super_obj199 .as_ref()200 .and_then(|super_obj| super_obj.field_visibility(name))201 .unwrap_or(Visibility::Normal),202 v => *v,203 })204 } else if let Some(super_obj) = &self.0.super_obj {205 super_obj.field_visibility(name)206 } else {207 None208 }209 }210211 fn has_field_include_hidden(&self, name: IStr) -> bool {212 if self.0.this_entries.contains_key(&name) {213 true214 } else if let Some(super_obj) = &self.0.super_obj {215 super_obj.has_field_include_hidden(name)216 } else {217 false218 }219 }220221 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {222 if include_hidden {223 self.has_field_include_hidden(name)224 } else {225 self.has_field(name)226 }227 }228 pub fn has_field(&self, name: IStr) -> bool {229 self.field_visibility(name)230 .map(|v| v.is_visible())231 .unwrap_or(false)232 }233234 pub fn get(&self, key: IStr) -> Result<Option<Val>> {235 self.run_assertions()?;236 self.get_raw(key, self.0.this_obj.as_ref())237 }238239 pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {240 let mut new = GcHashMap::with_capacity(1);241 new.insert(key, value);242 Self::new(Some(self), Cc::new(new), Cc::new(Vec::new()))243 }244245 fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {246 let real_this = real_this.unwrap_or(self);247 let cache_key = (key.clone(), WeakObjValue(real_this.0.downgrade()));248249 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {250 return Ok(match v {251 CacheValue::Cached(v) => Some(v.clone()),252 CacheValue::NotFound => None,253 CacheValue::Pending => throw!(InfiniteRecursionDetected),254 CacheValue::Errored(e) => return Err(e.clone()),255 });256 }257 self.0258 .value_cache259 .borrow_mut()260 .insert(cache_key.clone(), CacheValue::Pending);261 let value = match (self.0.this_entries.get(&key), &self.0.super_obj) {262 (Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),263 (Some(k), Some(s)) => {264 let our = self.evaluate_this(k, real_this)?;265 if k.add {266 s.get_raw(key, Some(real_this))?267 .map_or(Ok(Some(our.clone())), |v| {268 Ok(Some(evaluate_add_op(&v, &our)?))269 })270 } else {271 Ok(Some(our))272 }273 }274 (None, Some(s)) => s.get_raw(key, Some(real_this)),275 (None, None) => Ok(None),276 };277 let value = match value {278 Ok(v) => v,279 Err(e) => {280 self.0281 .value_cache282 .borrow_mut()283 .insert(cache_key, CacheValue::Errored(e.clone()));284 return Err(e);285 }286 };287 self.0.value_cache.borrow_mut().insert(288 cache_key,289 match &value {290 Some(v) => CacheValue::Cached(v.clone()),291 None => CacheValue::NotFound,292 },293 );294 Ok(value)295 }296 fn evaluate_this(&self, v: &ObjMember, real_this: &Self) -> Result<Val> {297 v.invoke298 .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?299 .evaluate()300 }301302 fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {303 if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {304 for assertion in self.0.assertions.iter() {305 if let Err(e) = assertion.run(Some(real_this.clone()), self.0.super_obj.clone()) {306 self.0.assertions_ran.borrow_mut().remove(real_this);307 return Err(e);308 }309 }310 if let Some(super_obj) = &self.0.super_obj {311 super_obj.run_assertions_raw(real_this)?;312 }313 }314 Ok(())315 }316 pub fn run_assertions(&self) -> Result<()> {317 self.run_assertions_raw(self)318 }319320 pub fn ptr_eq(a: &Self, b: &Self) -> bool {321 cc_ptr_eq(&a.0, &b.0)322 }323}324325impl PartialEq for ObjValue {326 fn eq(&self, other: &Self) -> bool {327 cc_ptr_eq(&self.0, &other.0)328 }329}330331impl Eq for ObjValue {}332impl Hash for ObjValue {333 fn hash<H: Hasher>(&self, hasher: &mut H) {334 hasher.write_usize(&*self.0 as *const _ as usize)335 }336}337338pub struct ObjValueBuilder {339 super_obj: Option<ObjValue>,340 map: GcHashMap<IStr, ObjMember>,341 assertions: Vec<TraceBox<dyn ObjectAssertion>>,342}343impl ObjValueBuilder {344 pub fn new() -> Self {345 Self::with_capacity(0)346 }347 pub fn with_capacity(capacity: usize) -> Self {348 Self {349 super_obj: None,350 map: GcHashMap::with_capacity(capacity),351 assertions: Vec::new(),352 }353 }354 pub fn reserve_asserts(&mut self, capacity: usize) -> &mut Self {355 self.assertions.reserve_exact(capacity);356 self357 }358 pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {359 self.super_obj = Some(super_obj);360 self361 }362363 pub fn assert(&mut self, assertion: TraceBox<dyn ObjectAssertion>) -> &mut Self {364 self.assertions.push(assertion);365 self366 }367 pub fn member(&mut self, name: IStr) -> ObjMemberBuilder {368 ObjMemberBuilder {369 value: self,370 name,371 add: false,372 visibility: Visibility::Normal,373 location: None,374 }375 }376377 pub fn build(self) -> ObjValue {378 ObjValue::new(self.super_obj, Cc::new(self.map), Cc::new(self.assertions))379 }380}381impl Default for ObjValueBuilder {382 fn default() -> Self {383 Self::with_capacity(0)384 }385}386387#[must_use = "value not added unless binding() was called"]388pub struct ObjMemberBuilder<'v> {389 value: &'v mut ObjValueBuilder,390 name: IStr,391 add: bool,392 visibility: Visibility,393 location: Option<ExprLocation>,394}395396#[allow(clippy::missing_const_for_fn)]397impl<'v> ObjMemberBuilder<'v> {398 pub const fn with_add(mut self, add: bool) -> Self {399 self.add = add;400 self401 }402 pub fn add(self) -> Self {403 self.with_add(true)404 }405 pub fn with_visibility(mut self, visibility: Visibility) -> Self {406 self.visibility = visibility;407 self408 }409 pub fn hide(self) -> Self {410 self.with_visibility(Visibility::Hidden)411 }412 pub fn with_location(mut self, location: ExprLocation) -> Self {413 self.location = Some(location);414 self415 }416 pub fn value(self, value: Val) -> Result<()> {417 self.binding(LazyBinding::Bound(LazyVal::new_resolved(value)))418 }419 pub fn bindable(self, bindable: TraceBox<dyn Bindable>) -> Result<()> {420 self.binding(LazyBinding::Bindable(Cc::new(bindable)))421 }422 pub fn binding(self, binding: LazyBinding) -> Result<()> {423 let old = self.value.map.insert(424 self.name.clone(),425 ObjMember {426 add: self.add,427 visibility: self.visibility,428 invoke: binding,429 location: self.location.clone(),430 },431 );432 if old.is_some() {433 push_frame(434 CallLocation(self.location.as_ref()),435 || format!("field <{}> initializtion", self.name.clone()),436 || throw!(DuplicateFieldName(self.name.clone())),437 )?438 }439 Ok(())440 }441}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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,3 +1,10 @@
+use std::{cell::RefCell, fmt::Debug, rc::Rc};
+
+use gcmodule::{Cc, Trace};
+use jrsonnet_interner::IStr;
+use jrsonnet_parser::{LocExpr, ParamsDesc};
+use jrsonnet_types::ValType;
+
use crate::{
builtin::manifest::{
manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,
@@ -12,11 +19,6 @@
gc::TraceBox,
throw, Context, ObjValue, Result,
};
-use gcmodule::{Cc, Trace};
-use jrsonnet_interner::IStr;
-use jrsonnet_parser::{LocExpr, ParamsDesc};
-use jrsonnet_types::ValType;
-use std::{cell::RefCell, fmt::Debug, rc::Rc};
pub trait LazyValValue: Trace {
fn get(self: Box<Self>) -> Result<Val>;
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>)) => {{