difftreelog
feat(evaluator) `IndexableMap::slice` helper
in: master
Previously `slice` method was only in `ArrayVal`, and for strings it needed to be implemented manually
1 file changed
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth12 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,12 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,13 },13 },14 throw, ObjValue, Result, State, Unbound, WeakObjValue,14 throw,15 typed::BoundedUsize,16 ObjValue, Result, State, Unbound, WeakObjValue,15};17};1618459 Str(IStr),461 Str(IStr),460 Arr(ArrValue),462 Arr(ArrValue),461}463}464impl IndexableVal {465 pub fn slice(466 self,467 index: Option<BoundedUsize<0, { i32::MAX as usize }>>,468 end: Option<BoundedUsize<0, { i32::MAX as usize }>>,469 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,470 ) -> Result<Self> {471 match &self {472 IndexableVal::Str(s) => {473 let index = index.as_deref().copied().unwrap_or(0);474 let end = end.as_deref().copied().unwrap_or(usize::MAX);475 let step = step.as_deref().copied().unwrap_or(1);476477 if index >= end {478 return Ok(Self::Str("".into()));479 }480481 Ok(Self::Str(482 (s.chars()483 .skip(index)484 .take(end - index)485 .step_by(step)486 .collect::<String>())487 .into(),488 ))489 }490 IndexableVal::Arr(arr) => {491 let index = index.as_deref().copied().unwrap_or(0);492 let end = end.as_deref().copied().unwrap_or(usize::MAX).min(arr.len());493 let step = step.as_deref().copied().unwrap_or(1);494495 if index >= end {496 return Ok(Self::Arr(ArrValue::new_eager()));497 }498499 Ok(Self::Arr(ArrValue::Slice(Box::new(Slice {500 inner: arr.clone(),501 from: index as u32,502 to: end as u32,503 step: step as u32,504 }))))505 }506 }507 }508}462509463#[derive(Debug, Clone, Trace)]510#[derive(Debug, Clone, Trace)]464pub enum Val {511pub enum Val {471 Func(FuncVal),518 Func(FuncVal),472}519}520521impl From<IndexableVal> for Val {522 fn from(v: IndexableVal) -> Self {523 match v {524 IndexableVal::Str(s) => Self::Str(s),525 IndexableVal::Arr(a) => Self::Arr(a),526 }527 }528}473529474#[cfg(target_pointer_width = "64")]530#[cfg(target_pointer_width = "64")]475static_assertions::assert_eq_size!(Val, [u8; 32]);531static_assertions::assert_eq_size!(Val, [u8; 32]);