Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/base/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,13 @@ impl<VS> Value<VS> {
}
}

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),
Expand Down
29 changes: 14 additions & 15 deletions src/ops/factory_bases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@ pub struct ResolvedOpArg {
}

pub trait ResolvedOpArgExt: Sized {
type ValueType;
type ValueRef<'a>;

fn expect_type(self, expected_type: &ValueType) -> Result<Self>;
fn value<'a>(&self, args: &'a Vec<value::Value>) -> Result<Self::ValueRef<'a>>;
fn take_value(&self, args: &mut Vec<value::Value>) -> Result<Self::ValueType>;
fn value<'a>(&self, args: &'a Vec<value::Value>) -> Result<&'a value::Value>;
fn take_value(&self, args: &mut Vec<value::Value>) -> Result<value::Value>;
}

impl ResolvedOpArgExt for ResolvedOpArg {
type ValueType = value::Value;
type ValueRef<'a> = &'a value::Value;

fn expect_type(self, expected_type: &ValueType) -> Result<Self> {
if &self.typ.typ != expected_type {
api_bail!(
Expand Down Expand Up @@ -75,19 +69,24 @@ impl ResolvedOpArgExt for ResolvedOpArg {
}

impl ResolvedOpArgExt for Option<ResolvedOpArg> {
type ValueType = Option<value::Value>;
type ValueRef<'a> = Option<&'a value::Value>;

fn expect_type(self, expected_type: &ValueType) -> Result<Self> {
self.map(|arg| arg.expect_type(expected_type)).transpose()
}

fn value<'a>(&self, args: &'a Vec<value::Value>) -> Result<Option<&'a value::Value>> {
self.as_ref().map(|arg| arg.value(args)).transpose()
fn value<'a>(&self, args: &'a Vec<value::Value>) -> 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<value::Value>) -> Result<Option<value::Value>> {
self.as_ref().map(|arg| arg.take_value(args)).transpose()
fn take_value(&self, args: &mut Vec<value::Value>) -> Result<value::Value> {
Ok(self
.as_ref()
.map(|arg| arg.take_value(args))
.transpose()?
.unwrap_or(value::Value::Null))
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/ops/functions/split_recursively.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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,
Expand Down