diff --git a/src/base/value.rs b/src/base/value.rs index 21bf12a86..c05973b1b 100644 --- a/src/base/value.rs +++ b/src/base/value.rs @@ -487,6 +487,13 @@ impl Value { } } + pub fn optional(&self) -> Option<&Self> { + match self { + Value::Null => None, + _ => Some(self), + } + } + pub fn as_bytes(&self) -> Result<&Arc<[u8]>> { match self { Value::Basic(BasicValue::Bytes(v)) => Ok(v), diff --git a/src/ops/factory_bases.rs b/src/ops/factory_bases.rs index c8dda8abd..c5e601ccf 100644 --- a/src/ops/factory_bases.rs +++ b/src/ops/factory_bases.rs @@ -25,18 +25,12 @@ pub struct ResolvedOpArg { } pub trait ResolvedOpArgExt: Sized { - type ValueType; - type ValueRef<'a>; - fn expect_type(self, expected_type: &ValueType) -> Result; - fn value<'a>(&self, args: &'a Vec) -> Result>; - fn take_value(&self, args: &mut Vec) -> Result; + fn value<'a>(&self, args: &'a Vec) -> Result<&'a value::Value>; + fn take_value(&self, args: &mut Vec) -> Result; } impl ResolvedOpArgExt for ResolvedOpArg { - type ValueType = value::Value; - type ValueRef<'a> = &'a value::Value; - fn expect_type(self, expected_type: &ValueType) -> Result { if &self.typ.typ != expected_type { api_bail!( @@ -75,19 +69,24 @@ impl ResolvedOpArgExt for ResolvedOpArg { } impl ResolvedOpArgExt for Option { - type ValueType = Option; - type ValueRef<'a> = Option<&'a value::Value>; - fn expect_type(self, expected_type: &ValueType) -> Result { self.map(|arg| arg.expect_type(expected_type)).transpose() } - fn value<'a>(&self, args: &'a Vec) -> Result> { - self.as_ref().map(|arg| arg.value(args)).transpose() + fn value<'a>(&self, args: &'a Vec) -> Result<&'a value::Value> { + Ok(self + .as_ref() + .map(|arg| arg.value(args)) + .transpose()? + .unwrap_or(&value::Value::Null)) } - fn take_value(&self, args: &mut Vec) -> Result> { - self.as_ref().map(|arg| arg.take_value(args)).transpose() + fn take_value(&self, args: &mut Vec) -> Result { + Ok(self + .as_ref() + .map(|arg| arg.take_value(args)) + .transpose()? + .unwrap_or(value::Value::Null)) } } diff --git a/src/ops/functions/split_recursively.rs b/src/ops/functions/split_recursively.rs index ca305508e..8288c8646 100644 --- a/src/ops/functions/split_recursively.rs +++ b/src/ops/functions/split_recursively.rs @@ -413,6 +413,7 @@ impl SimpleFunctionExecutor for Executor { let lang_config = { let language = self.args.language.value(&input)?; language + .optional() .map(|v| anyhow::Ok(v.as_str()?.as_ref())) .transpose()? .and_then(|lang| TREE_SITTER_LANGUAGE_BY_LANG.get(lang)) @@ -426,6 +427,7 @@ impl SimpleFunctionExecutor for Executor { .args .chunk_overlap .value(&input)? + .optional() .map(|v| v.as_int64()) .transpose()? .unwrap_or(0) as usize,