Skip to content
Open
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
28 changes: 14 additions & 14 deletions rust/cocoindex/src/base/duration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::f64;

use anyhow::{Result, anyhow, bail};
use crate::prelude::*;
use chrono::Duration;

/// Parses a string of number-unit pairs into a vector of (number, unit),
Expand Down Expand Up @@ -28,20 +28,20 @@ fn parse_components(
}
}
if num_str.is_empty() {
bail!("Expected number in: {}", original_input);
client_bail!("Expected number in: {}", original_input);
}
let num = num_str
.parse::<f64>()
.map_err(|_| anyhow!("Invalid number '{}' in: {}", num_str, original_input))?;
.map_err(|_| client_error!("Invalid number '{}' in: {}", num_str, original_input))?;
if let Some(&unit) = iter.peek() {
if allowed_units.contains(&unit) {
result.push((num, unit));
iter.next();
} else {
bail!("Invalid unit '{}' in: {}", unit, original_input);
client_bail!("Invalid unit '{}' in: {}", unit, original_input);
}
} else {
bail!(
client_bail!(
"Missing unit after number '{}' in: {}",
num_str,
original_input
Expand All @@ -60,7 +60,7 @@ fn parse_iso8601_duration(s: &str, original_input: &str) -> Result<Duration> {
};

if !s_after_sign.starts_with('P') {
bail!("Duration must start with 'P' in: {}", original_input);
client_bail!("Duration must start with 'P' in: {}", original_input);
}
let s_after_p = &s_after_sign[1..];

Expand All @@ -77,7 +77,7 @@ fn parse_iso8601_duration(s: &str, original_input: &str) -> Result<Duration> {
let time_components = if let Some(time_str) = time_part {
let comps = parse_components(time_str, &['H', 'M', 'S'], original_input)?;
if comps.is_empty() {
bail!(
client_bail!(
"Time part present but no time components in: {}",
original_input
);
Expand All @@ -88,7 +88,7 @@ fn parse_iso8601_duration(s: &str, original_input: &str) -> Result<Duration> {
};

if date_components.is_empty() && time_components.is_empty() {
bail!("No components in duration: {}", original_input);
client_bail!("No components in duration: {}", original_input);
}

// Accumulate date duration
Expand Down Expand Up @@ -138,7 +138,7 @@ fn parse_iso8601_duration(s: &str, original_input: &str) -> Result<Duration> {
fn parse_human_readable_duration(s: &str, original_input: &str) -> Result<Duration> {
let parts: Vec<&str> = s.split_whitespace().collect();
if parts.is_empty() || parts.len() % 2 != 0 {
bail!(
client_bail!(
"Invalid human-readable duration format in: {}",
original_input
);
Expand All @@ -147,9 +147,9 @@ fn parse_human_readable_duration(s: &str, original_input: &str) -> Result<Durati
let durations: Result<Vec<Duration>> = parts
.chunks(2)
.map(|chunk| {
let num: i64 = chunk[0]
.parse()
.map_err(|_| anyhow!("Invalid number '{}' in: {}", chunk[0], original_input))?;
let num: i64 = chunk[0].parse().map_err(|_| {
client_error!("Invalid number '{}' in: {}", chunk[0], original_input)
})?;

match chunk[1].to_lowercase().as_str() {
"day" | "days" => Ok(Duration::days(num)),
Expand All @@ -158,7 +158,7 @@ fn parse_human_readable_duration(s: &str, original_input: &str) -> Result<Durati
"second" | "seconds" => Ok(Duration::seconds(num)),
"millisecond" | "milliseconds" => Ok(Duration::milliseconds(num)),
"microsecond" | "microseconds" => Ok(Duration::microseconds(num)),
_ => bail!("Invalid unit '{}' in: {}", chunk[1], original_input),
_ => client_bail!("Invalid unit '{}' in: {}", chunk[1], original_input),
}
})
.collect();
Expand All @@ -171,7 +171,7 @@ pub fn parse_duration(s: &str) -> Result<Duration> {
let original_input = s;
let s = s.trim();
if s.is_empty() {
bail!("Empty duration string");
client_bail!("Empty duration string");
}

let is_likely_iso8601 = match s.as_bytes() {
Expand Down
2 changes: 1 addition & 1 deletion rust/cocoindex/src/base/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl ValueExtractor {
.remove(object_wrapper_field_name)
.unwrap_or(serde_json::Value::Null),
_ => {
bail!("Field `{}` not found", object_wrapper_field_name)
client_bail!("Field `{}` not found", object_wrapper_field_name)
}
}
} else {
Expand Down
63 changes: 31 additions & 32 deletions rust/cocoindex/src/base/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,56 +279,56 @@ impl KeyPart {
pub fn bytes_value(&self) -> Result<&Bytes> {
match self {
KeyPart::Bytes(v) => Ok(v),
_ => anyhow::bail!("expected bytes value, but got {}", self.kind_str()),
_ => client_bail!("expected bytes value, but got {}", self.kind_str()),
}
}

pub fn str_value(&self) -> Result<&Arc<str>> {
match self {
KeyPart::Str(v) => Ok(v),
_ => anyhow::bail!("expected str value, but got {}", self.kind_str()),
_ => client_bail!("expected str value, but got {}", self.kind_str()),
}
}

pub fn bool_value(&self) -> Result<bool> {
match self {
KeyPart::Bool(v) => Ok(*v),
_ => anyhow::bail!("expected bool value, but got {}", self.kind_str()),
_ => client_bail!("expected bool value, but got {}", self.kind_str()),
}
}

pub fn int64_value(&self) -> Result<i64> {
match self {
KeyPart::Int64(v) => Ok(*v),
_ => anyhow::bail!("expected int64 value, but got {}", self.kind_str()),
_ => client_bail!("expected int64 value, but got {}", self.kind_str()),
}
}

pub fn range_value(&self) -> Result<RangeValue> {
match self {
KeyPart::Range(v) => Ok(*v),
_ => anyhow::bail!("expected range value, but got {}", self.kind_str()),
_ => client_bail!("expected range value, but got {}", self.kind_str()),
}
}

pub fn uuid_value(&self) -> Result<uuid::Uuid> {
match self {
KeyPart::Uuid(v) => Ok(*v),
_ => anyhow::bail!("expected uuid value, but got {}", self.kind_str()),
_ => client_bail!("expected uuid value, but got {}", self.kind_str()),
}
}

pub fn date_value(&self) -> Result<chrono::NaiveDate> {
match self {
KeyPart::Date(v) => Ok(*v),
_ => anyhow::bail!("expected date value, but got {}", self.kind_str()),
_ => client_bail!("expected date value, but got {}", self.kind_str()),
}
}

pub fn struct_value(&self) -> Result<&Vec<KeyPart>> {
match self {
KeyPart::Struct(v) => Ok(v),
_ => anyhow::bail!("expected struct value, but got {}", self.kind_str()),
_ => client_bail!("expected struct value, but got {}", self.kind_str()),
}
}

Expand Down Expand Up @@ -438,7 +438,7 @@ impl KeyValue {
serde_json::Value::Array(arr) => std::iter::zip(arr.into_iter(), schema)
.map(|(v, s)| Value::<ScopeValue>::from_json(v, &s.value_type.typ)?.into_key())
.collect::<Result<Box<[_]>>>()?,
_ => anyhow::bail!("expected array value, but got {}", value),
_ => client_bail!("expected array value, but got {}", value),
}
};
Ok(Self(field_values))
Expand Down Expand Up @@ -848,7 +848,7 @@ impl<VS> Value<VS> {
.collect::<Result<Vec<_>>>()?,
),
Value::Null | Value::UTable(_) | Value::KTable(_) | Value::LTable(_) => {
anyhow::bail!("invalid key value type")
client_bail!("invalid key value type")
}
};
Ok(result)
Expand All @@ -864,7 +864,7 @@ impl<VS> Value<VS> {
.collect::<Result<Vec<_>>>()?,
),
Value::Null | Value::UTable(_) | Value::KTable(_) | Value::LTable(_) => {
anyhow::bail!("invalid key value type")
client_bail!("invalid key value type")
}
};
Ok(result)
Expand All @@ -891,70 +891,70 @@ impl<VS> Value<VS> {
pub fn as_bytes(&self) -> Result<&Bytes> {
match self {
Value::Basic(BasicValue::Bytes(v)) => Ok(v),
_ => anyhow::bail!("expected bytes value, but got {}", self.kind()),
_ => client_bail!("expected bytes value, but got {}", self.kind()),
}
}

pub fn as_str(&self) -> Result<&Arc<str>> {
match self {
Value::Basic(BasicValue::Str(v)) => Ok(v),
_ => anyhow::bail!("expected str value, but got {}", self.kind()),
_ => client_bail!("expected str value, but got {}", self.kind()),
}
}

pub fn as_bool(&self) -> Result<bool> {
match self {
Value::Basic(BasicValue::Bool(v)) => Ok(*v),
_ => anyhow::bail!("expected bool value, but got {}", self.kind()),
_ => client_bail!("expected bool value, but got {}", self.kind()),
}
}

pub fn as_int64(&self) -> Result<i64> {
match self {
Value::Basic(BasicValue::Int64(v)) => Ok(*v),
_ => anyhow::bail!("expected int64 value, but got {}", self.kind()),
_ => client_bail!("expected int64 value, but got {}", self.kind()),
}
}

pub fn as_float32(&self) -> Result<f32> {
match self {
Value::Basic(BasicValue::Float32(v)) => Ok(*v),
_ => anyhow::bail!("expected float32 value, but got {}", self.kind()),
_ => client_bail!("expected float32 value, but got {}", self.kind()),
}
}

pub fn as_float64(&self) -> Result<f64> {
match self {
Value::Basic(BasicValue::Float64(v)) => Ok(*v),
_ => anyhow::bail!("expected float64 value, but got {}", self.kind()),
_ => client_bail!("expected float64 value, but got {}", self.kind()),
}
}

pub fn as_range(&self) -> Result<RangeValue> {
match self {
Value::Basic(BasicValue::Range(v)) => Ok(*v),
_ => anyhow::bail!("expected range value, but got {}", self.kind()),
_ => client_bail!("expected range value, but got {}", self.kind()),
}
}

pub fn as_json(&self) -> Result<&Arc<serde_json::Value>> {
match self {
Value::Basic(BasicValue::Json(v)) => Ok(v),
_ => anyhow::bail!("expected json value, but got {}", self.kind()),
_ => client_bail!("expected json value, but got {}", self.kind()),
}
}

pub fn as_vector(&self) -> Result<&Arc<[BasicValue]>> {
match self {
Value::Basic(BasicValue::Vector(v)) => Ok(v),
_ => anyhow::bail!("expected vector value, but got {}", self.kind()),
_ => client_bail!("expected vector value, but got {}", self.kind()),
}
}

pub fn as_struct(&self) -> Result<&FieldValues<VS>> {
match self {
Value::Struct(v) => Ok(v),
_ => anyhow::bail!("expected struct value, but got {}", self.kind()),
_ => client_bail!("expected struct value, but got {}", self.kind()),
}
}
}
Expand Down Expand Up @@ -1138,16 +1138,15 @@ impl BasicValue {
(serde_json::Value::Bool(v), BasicValueType::Bool) => BasicValue::Bool(v),
(serde_json::Value::Number(v), BasicValueType::Int64) => BasicValue::Int64(
v.as_i64()
.ok_or_else(|| anyhow::anyhow!("invalid int64 value {v}"))?,
.ok_or_else(|| client_error!("invalid int64 value {v}"))?,
),
(serde_json::Value::Number(v), BasicValueType::Float32) => BasicValue::Float32(
v.as_f64()
.ok_or_else(|| anyhow::anyhow!("invalid fp32 value {v}"))?
as f32,
.ok_or_else(|| client_error!("invalid fp32 value {v}"))? as f32,
),
(serde_json::Value::Number(v), BasicValueType::Float64) => BasicValue::Float64(
v.as_f64()
.ok_or_else(|| anyhow::anyhow!("invalid fp64 value {v}"))?,
.ok_or_else(|| client_error!("invalid fp64 value {v}"))?,
),
(v, BasicValueType::Range) => BasicValue::Range(utils::deser::from_json_value(v)?),
(serde_json::Value::String(v), BasicValueType::Uuid) => BasicValue::Uuid(v.parse()?),
Expand Down Expand Up @@ -1193,11 +1192,11 @@ impl BasicValue {
(v, BasicValueType::Union(typ)) => {
let arr = match v {
serde_json::Value::Array(arr) => arr,
_ => anyhow::bail!("Invalid JSON value for union, expect array"),
_ => client_bail!("Invalid JSON value for union, expect array"),
};

if arr.len() != 2 {
anyhow::bail!(
client_bail!(
"Invalid union tuple: expect 2 values, received {}",
arr.len()
);
Expand All @@ -1217,15 +1216,15 @@ impl BasicValue {
let cur_type = typ
.types
.get(tag_id)
.ok_or_else(|| anyhow::anyhow!("No type in `tag_id` \"{tag_id}\" found"))?;
.ok_or_else(|| client_error!("No type in `tag_id` \"{tag_id}\" found"))?;

BasicValue::UnionVariant {
tag_id,
value: Box::new(BasicValue::from_json(value, cur_type)?),
}
}
(v, t) => {
anyhow::bail!("Value and type not matched.\nTarget type {t:?}\nJSON value: {v}\n")
client_bail!("Value and type not matched.\nTarget type {t:?}\nJSON value: {v}\n")
}
};
Ok(result)
Expand Down Expand Up @@ -1297,13 +1296,13 @@ where
v.into_iter()
.map(|v| {
if s.row.fields.len() < num_key_parts {
anyhow::bail!("Invalid KTable schema: expect at least {} fields, got {}", num_key_parts, s.row.fields.len());
client_bail!("Invalid KTable schema: expect at least {} fields, got {}", num_key_parts, s.row.fields.len());
}
let mut fields_iter = s.row.fields.iter();
match v {
serde_json::Value::Array(v) => {
if v.len() != fields_iter.len() {
anyhow::bail!("Invalid KTable value: expect {} values, received {}", fields_iter.len(), v.len());
client_bail!("Invalid KTable value: expect {} values, received {}", fields_iter.len(), v.len());
}

let mut field_vals_iter = v.into_iter();
Expand Down Expand Up @@ -1365,7 +1364,7 @@ where
}
}
(v, t) => {
anyhow::bail!("Value and type not matched.\nTarget type {t:?}\nJSON value: {v}\n")
client_bail!("Value and type not matched.\nTarget type {t:?}\nJSON value: {v}\n")
}
};
Ok(result)
Expand Down
8 changes: 4 additions & 4 deletions rust/cocoindex/src/builder/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl StructSchemaBuilder {
let field_idx = self.fields.len() as u32;
match self.field_name_idx.entry(field.name.clone()) {
std::collections::hash_map::Entry::Occupied(_) => {
bail!("Field name already exists: {}", field.name);
client_bail!("Field name already exists: {}", field.name);
}
std::collections::hash_map::Entry::Vacant(entry) => {
entry.insert(field_idx);
Expand Down Expand Up @@ -465,7 +465,7 @@ impl DataScopeBuilder {
let mut def_fp = base_def_fp;

if field_path.is_empty() {
bail!("Field path is empty");
client_bail!("Field path is empty");
}

let mut i = 0;
Expand Down Expand Up @@ -1096,7 +1096,7 @@ impl AnalyzerContext {
.fields
.iter()
.position(|field| &field.name == f)
.ok_or_else(|| anyhow!("field not found: {}", f))
.ok_or_else(|| client_error!("field not found: {}", f))
})
.collect::<Result<Vec<_>>>()?;

Expand Down Expand Up @@ -1396,7 +1396,7 @@ pub async fn analyze_flow(
targets: targets_analyzed_ss
.into_iter()
.enumerate()
.map(|(idx, v)| v.ok_or_else(|| anyhow!("target op `{}` not found", idx)))
.map(|(idx, v)| v.ok_or_else(|| internal_error!("target op `{}` not found", idx)))
.collect::<Result<Vec<_>>>()?,
declarations: declarations_analyzed_ss,
};
Expand Down
Loading
Loading