1use std::{cell::RefCell, rc::Rc};23use jrsonnet_evaluator::{4 bail,5 error::{ErrorKind::*, Result},6 function::{builtin, ArgLike, CallLocation, FuncVal},7 manifest::JsonFormat,8 typed::{Either2, Either4},9 val::{equals, ArrValue},10 Context, Either, IStr, ObjValue, Thunk, Val,11};1213use crate::{extvar_source, Settings};1415#[builtin]16pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {17 use Either4::*;18 match x {19 A(x) => x.chars().count(),20 B(x) => x.len(),21 C(x) => x.len(),22 D(f) => f.params_len(),23 }24}2526#[builtin]27pub fn builtin_get(28 o: ObjValue,29 f: IStr,30 default: Option<Thunk<Val>>,31 #[default(true)] inc_hidden: bool,32) -> Result<Val> {33 let do_default = move || {34 let Some(default) = default else {35 return Ok(Val::Null);36 };37 default.evaluate()38 };39 40 if !inc_hidden && !o.has_field_ex(f.clone(), false) {41 return do_default();42 }43 let Some(v) = o.get(f)? else {44 return do_default();45 };46 Ok(v)47}4849#[builtin(fields(50 settings: Rc<RefCell<Settings>>,51))]52pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {53 let ctx = ctx.state().create_default_context(extvar_source(&x, ""));54 this.settings55 .borrow()56 .ext_vars57 .get(&x)58 .cloned()59 .ok_or_else(|| UndefinedExternalVariable(x))?60 .evaluate_arg(ctx, true)?61 .evaluate()62}6364#[builtin(fields(65 settings: Rc<RefCell<Settings>>,66))]67pub fn builtin_native(this: &builtin_native, x: IStr) -> Val {68 this.settings69 .borrow()70 .ext_natives71 .get(&x)72 .cloned()73 .map_or(Val::Null, Val::Func)74}7576#[builtin(fields(77 settings: Rc<RefCell<Settings>>,78))]79pub fn builtin_trace(80 this: &builtin_trace,81 loc: CallLocation,82 str: Val,83 rest: Option<Thunk<Val>>,84) -> Result<Val> {85 this.settings.borrow().trace_printer.print_trace(86 loc,87 match &str {88 Val::Str(s) => s.clone().into_flat(),89 Val::Func(f) => format!("{f:?}").into(),90 v => v.manifest(JsonFormat::debug())?.into(),91 },92 );93 rest.map_or_else(|| Ok(str), |rest| rest.evaluate())94}9596#[allow(clippy::comparison_chain)]97#[builtin]98pub fn builtin_starts_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {99 Ok(match (a, b) {100 (Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),101 (Either2::B(a), Either2::B(b)) => {102 if b.len() > a.len() {103 return Ok(false);104 } else if b.len() == a.len() {105 return equals(&Val::Arr(a), &Val::Arr(b));106 }107 for (a, b) in a.iter().take(b.len()).zip(b.iter()) {108 let a = a?;109 let b = b?;110 if !equals(&a, &b)? {111 return Ok(false);112 }113 }114 true115 }116 _ => bail!("both arguments should be of the same type"),117 })118}119120#[allow(clippy::comparison_chain)]121#[builtin]122pub fn builtin_ends_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {123 Ok(match (a, b) {124 (Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),125 (Either2::B(a), Either2::B(b)) => {126 if b.len() > a.len() {127 return Ok(false);128 } else if b.len() == a.len() {129 return equals(&Val::Arr(a), &Val::Arr(b));130 }131 let a_len = a.len();132 for (a, b) in a.iter().skip(a_len - b.len()).zip(b.iter()) {133 let a = a?;134 let b = b?;135 if !equals(&a, &b)? {136 return Ok(false);137 }138 }139 true140 }141 _ => bail!("both arguments should be of the same type"),142 })143}