Skip to content

Commit c62f181

Browse files
authored
Make handling for optional arguments in SplitRecursively more robust. (#135)
1 parent cccc4ec commit c62f181

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/base/value.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,13 @@ impl<VS> Value<VS> {
487487
}
488488
}
489489

490+
pub fn optional(&self) -> Option<&Self> {
491+
match self {
492+
Value::Null => None,
493+
_ => Some(self),
494+
}
495+
}
496+
490497
pub fn as_bytes(&self) -> Result<&Arc<[u8]>> {
491498
match self {
492499
Value::Basic(BasicValue::Bytes(v)) => Ok(v),

src/ops/factory_bases.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,12 @@ pub struct ResolvedOpArg {
2525
}
2626

2727
pub trait ResolvedOpArgExt: Sized {
28-
type ValueType;
29-
type ValueRef<'a>;
30-
3128
fn expect_type(self, expected_type: &ValueType) -> Result<Self>;
32-
fn value<'a>(&self, args: &'a Vec<value::Value>) -> Result<Self::ValueRef<'a>>;
33-
fn take_value(&self, args: &mut Vec<value::Value>) -> Result<Self::ValueType>;
29+
fn value<'a>(&self, args: &'a Vec<value::Value>) -> Result<&'a value::Value>;
30+
fn take_value(&self, args: &mut Vec<value::Value>) -> Result<value::Value>;
3431
}
3532

3633
impl ResolvedOpArgExt for ResolvedOpArg {
37-
type ValueType = value::Value;
38-
type ValueRef<'a> = &'a value::Value;
39-
4034
fn expect_type(self, expected_type: &ValueType) -> Result<Self> {
4135
if &self.typ.typ != expected_type {
4236
api_bail!(
@@ -75,19 +69,24 @@ impl ResolvedOpArgExt for ResolvedOpArg {
7569
}
7670

7771
impl ResolvedOpArgExt for Option<ResolvedOpArg> {
78-
type ValueType = Option<value::Value>;
79-
type ValueRef<'a> = Option<&'a value::Value>;
80-
8172
fn expect_type(self, expected_type: &ValueType) -> Result<Self> {
8273
self.map(|arg| arg.expect_type(expected_type)).transpose()
8374
}
8475

85-
fn value<'a>(&self, args: &'a Vec<value::Value>) -> Result<Option<&'a value::Value>> {
86-
self.as_ref().map(|arg| arg.value(args)).transpose()
76+
fn value<'a>(&self, args: &'a Vec<value::Value>) -> Result<&'a value::Value> {
77+
Ok(self
78+
.as_ref()
79+
.map(|arg| arg.value(args))
80+
.transpose()?
81+
.unwrap_or(&value::Value::Null))
8782
}
8883

89-
fn take_value(&self, args: &mut Vec<value::Value>) -> Result<Option<value::Value>> {
90-
self.as_ref().map(|arg| arg.take_value(args)).transpose()
84+
fn take_value(&self, args: &mut Vec<value::Value>) -> Result<value::Value> {
85+
Ok(self
86+
.as_ref()
87+
.map(|arg| arg.take_value(args))
88+
.transpose()?
89+
.unwrap_or(value::Value::Null))
9190
}
9291
}
9392

src/ops/functions/split_recursively.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ impl SimpleFunctionExecutor for Executor {
413413
let lang_config = {
414414
let language = self.args.language.value(&input)?;
415415
language
416+
.optional()
416417
.map(|v| anyhow::Ok(v.as_str()?.as_ref()))
417418
.transpose()?
418419
.and_then(|lang| TREE_SITTER_LANGUAGE_BY_LANG.get(lang))
@@ -426,6 +427,7 @@ impl SimpleFunctionExecutor for Executor {
426427
.args
427428
.chunk_overlap
428429
.value(&input)?
430+
.optional()
429431
.map(|v| v.as_int64())
430432
.transpose()?
431433
.unwrap_or(0) as usize,

0 commit comments

Comments
 (0)