git.delta.rocks / jrsonnet / refs/commits / 59f19ba582ad

difftreelog

refactor drop runtime_error! usage

pospolqmYaroslav Bolyukin2026-05-05parent: #cf33b6e.patch.diff
in: master

4 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
3use jrsonnet_evaluator::{3use jrsonnet_evaluator::{
4 Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val, bail, error,4 Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val, bail, error,
5 function::{NativeFn, builtin},5 function::{NativeFn, builtin},
6 runtime_error,
7 typed::{BoundedUsize, Either2, FromUntyped},6 typed::{BoundedUsize, Either2, FromUntyped},
8 val::{ArrValue, IndexableVal, equals},7 val::{ArrValue, IndexableVal, equals},
9};8};
40 Ok(match what {39 Ok(match what {
41 Either2::A(s) => Val::string(s.repeat(count as usize)),40 Either2::A(s) => Val::string(s.repeat(count as usize)),
42 Either2::B(arr) => Val::Arr(41 Either2::B(arr) => Val::Arr(
43 ArrValue::repeated(arr, count)42 ArrValue::repeated(arr, count).ok_or_else(|| error!("repeated length overflow"))?,
44 .ok_or_else(|| runtime_error!("repeated length overflow"))?,
45 ),43 ),
46 })44 })
47}45}
modifiedcrates/jrsonnet-stdlib/src/encoding.rsdiffbeforeafterboth
1use base64::{Engine, engine::general_purpose::STANDARD};1use base64::{Engine, engine::general_purpose::STANDARD};
2use jrsonnet_evaluator::{2use jrsonnet_evaluator::{
3 IBytes, IStr, Result, bail,3 IBytes, IStr, Result, bail, error,
4 function::builtin,4 function::builtin,
5 runtime_error,
6 typed::{Either, Either2},5 typed::{Either, Either2},
7};6};
87
35pub fn builtin_base64_decode_bytes(str: IStr) -> Result<IBytes> {34pub fn builtin_base64_decode_bytes(str: IStr) -> Result<IBytes> {
36 Ok(STANDARD35 Ok(STANDARD
37 .decode(str.as_bytes())36 .decode(str.as_bytes())
38 .map_err(|e| runtime_error!("invalid base64: {e}"))?37 .map_err(|e| error!("invalid base64: {e}"))?
39 .as_slice()38 .as_slice()
40 .into())39 .into())
41}40}
44pub fn builtin_base64_decode(str: IStr, #[default(false)] lossy: bool) -> Result<String> {43pub fn builtin_base64_decode(str: IStr, #[default(false)] lossy: bool) -> Result<String> {
45 let bytes = STANDARD44 let bytes = STANDARD
46 .decode(str.as_bytes())45 .decode(str.as_bytes())
47 .map_err(|e| runtime_error!("invalid base64: {e}"))?;46 .map_err(|e| error!("invalid base64: {e}"))?;
48 if lossy {47 if lossy {
49 Ok(String::from_utf8_lossy(&bytes).to_string())48 Ok(String::from_utf8_lossy(&bytes).to_string())
50 } else {49 } else {
51 String::from_utf8(bytes).map_err(|e| runtime_error!("bad utf8: {e}"))50 String::from_utf8(bytes).map_err(|e| error!("bad utf8: {e}"))
52 }51 }
53}52}
5453
modifiedcrates/jrsonnet-stdlib/src/parse.rsdiffbeforeafterboth
1use jrsonnet_evaluator::{IStr, Result, Val, function::builtin, runtime_error};1use jrsonnet_evaluator::{IStr, Result, Val, error, function::builtin};
2use serde_saphyr::options;2use serde_saphyr::options;
33
4#[builtin]4#[builtin]
5pub fn builtin_parse_json(str: IStr) -> Result<Val> {5pub fn builtin_parse_json(str: IStr) -> Result<Val> {
6 let value: Val =6 let value: Val = serde_json::from_str(&str).map_err(|e| error!("failed to parse json: {e}"))?;
7 serde_json::from_str(&str).map_err(|e| runtime_error!("failed to parse json: {e}"))?;
8 Ok(value)7 Ok(value)
9}8}
109
21 budget: None,20 budget: None,
22 },21 },
23 )22 )
24 .map_err(|e| runtime_error!("failed to parse yaml: {e}"))?;23 .map_err(|e| error!("failed to parse yaml: {e}"))?;
2524
26 // saphyr and other yaml implementations disagree on how to handle an empty document in multi-document stream.25 // saphyr and other yaml implementations disagree on how to handle an empty document in multi-document stream.
27 // Saphyr only considers document started after anything is emitted after the document delimiter26 // Saphyr only considers document started after anything is emitted after the document delimiter
modifiedcrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth
206#[builtin]206#[builtin]
207pub fn builtin_bigint(v: Either![f64, IStr]) -> Result<Val> {207pub fn builtin_bigint(v: Either![f64, IStr]) -> Result<Val> {
208 use Either2::*;208 use Either2::*;
209 use jrsonnet_evaluator::runtime_error;209 use jrsonnet_evaluator::error;
210 Ok(match v {210 Ok(match v {
211 A(a) => {211 A(a) => Val::BigInt(Box::new(
212 Val::BigInt(Box::new(a.to_string().parse().map_err(|e| {212 a.to_string()
213 .parse()
213 runtime_error!("number is not convertible to bigint: {e}")214 .map_err(|e| error!("number is not convertible to bigint: {e}"))?,
214 })?))215 )),
215 }
216 B(b) => Val::BigInt(Box::new(216 B(b) => Val::BigInt(Box::new(
217 b.as_str()217 b.as_str().parse().map_err(|e| error!("bad bigint: {e}"))?,
218 .parse()
219 .map_err(|e| runtime_error!("bad bigint: {e}"))?,
220 )),218 )),
221 })219 })
222}220}