Skip to content

Commit 233acd2

Browse files
committed
to_unity function
1 parent b9c4783 commit 233acd2

3 files changed

Lines changed: 87 additions & 3 deletions

File tree

cozo-core/src/data/expr.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,26 @@ impl Expr {
326326
#[diagnostic(code(eval::unbound))]
327327
struct TupleTooShortError(String, usize, usize, #[label] SourceSpan);
328328

329-
Ok(bindings.as_ref()
329+
Ok(bindings
330+
.as_ref()
330331
.get(*i)
331332
.ok_or_else(|| {
332-
TupleTooShortError(var.name.to_string(), *i, bindings.as_ref().len(), var.span)
333+
TupleTooShortError(
334+
var.name.to_string(),
335+
*i,
336+
bindings.as_ref().len(),
337+
var.span,
338+
)
333339
})?
334340
.clone())
335341
}
336342
},
337343
Expr::Const { val, .. } => Ok(val.clone()),
338344
Expr::Apply { op, args, .. } => {
339-
let args: Box<[DataValue]> = args.iter().map(|v| v.eval(bindings.as_ref())).try_collect()?;
345+
let args: Box<[DataValue]> = args
346+
.iter()
347+
.map(|v| v.eval(bindings.as_ref()))
348+
.try_collect()?;
340349
Ok((op.inner)(&args)
341350
.map_err(|err| EvalRaisedError(self.span(), err.to_string()))?)
342351
}
@@ -701,6 +710,7 @@ pub(crate) fn get_op(name: &str) -> Option<&'static Op> {
701710
"difference" => &OP_DIFFERENCE,
702711
"to_uuid" => &OP_TO_UUID,
703712
"to_bool" => &OP_TO_BOOL,
713+
"to_unity" => &OP_TO_UNITY,
704714
"rand_uuid_v1" => &OP_RAND_UUID_V1,
705715
"rand_uuid_v4" => &OP_RAND_UUID_V4,
706716
"uuid_timestamp" => &OP_UUID_TIMESTAMP,

cozo-core/src/data/functions.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,10 +1277,31 @@ pub(crate) fn op_to_bool(args: &[DataValue]) -> Result<DataValue> {
12771277
}))
12781278
}
12791279

1280+
1281+
define_op!(OP_TO_UNITY, 1, false);
1282+
pub(crate) fn op_to_unity(args: &[DataValue]) -> Result<DataValue> {
1283+
Ok(DataValue::from(match &args[0] {
1284+
DataValue::Null => 0,
1285+
DataValue::Bool(b) => if *b {1} else {0},
1286+
DataValue::Num(n) => if n.get_float() != 0. {1} else {0},
1287+
DataValue::Str(s) => if s.is_empty() {0} else { 1},
1288+
DataValue::Bytes(b) => if b.is_empty() {0} else { 1},
1289+
DataValue::Uuid(u) => if u.0.is_nil() {0} else { 1 },
1290+
DataValue::Regex(r) => if r.0.as_str().is_empty() {0 } else { 1},
1291+
DataValue::List(l) => if l.is_empty() {0} else {1},
1292+
DataValue::Set(s) => if s.is_empty() {0} else {1},
1293+
DataValue::Guard => 0,
1294+
DataValue::Bot => 0,
1295+
}))
1296+
}
1297+
1298+
12801299
define_op!(OP_TO_FLOAT, 1, false);
12811300
pub(crate) fn op_to_float(args: &[DataValue]) -> Result<DataValue> {
12821301
Ok(match &args[0] {
12831302
DataValue::Num(n) => n.get_float().into(),
1303+
DataValue::Null => DataValue::from(0.0),
1304+
DataValue::Bool(b) => DataValue::from(if *b { 1.0 } else { 0.0 }),
12841305
DataValue::Str(t) => match t as &str {
12851306
"PI" => f64::PI().into(),
12861307
"E" => f64::E().into(),

cozo-core/src/data/tests/functions.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,61 @@ fn test_to_string() {
12171217
);
12181218
}
12191219

1220+
#[test]
1221+
fn test_to_unity() {
1222+
assert_eq!(op_to_unity(&[DataValue::Null]).unwrap(), DataValue::from(0));
1223+
assert_eq!(
1224+
op_to_unity(&[DataValue::Bool(false)]).unwrap(),
1225+
DataValue::from(0)
1226+
);
1227+
assert_eq!(
1228+
op_to_unity(&[DataValue::Bool(true)]).unwrap(),
1229+
DataValue::from(1)
1230+
);
1231+
assert_eq!(
1232+
op_to_unity(&[DataValue::from(10)]).unwrap(),
1233+
DataValue::from(1)
1234+
);
1235+
assert_eq!(
1236+
op_to_unity(&[DataValue::from(1.0)]).unwrap(),
1237+
DataValue::from(1)
1238+
);
1239+
assert_eq!(
1240+
op_to_unity(&[DataValue::from(f64::NAN)]).unwrap(),
1241+
DataValue::from(1)
1242+
);
1243+
assert_eq!(
1244+
op_to_unity(&[DataValue::Str("0".into())]).unwrap(),
1245+
DataValue::from(1)
1246+
);
1247+
assert_eq!(
1248+
op_to_unity(&[DataValue::Str("".into())]).unwrap(),
1249+
DataValue::from(0)
1250+
);
1251+
assert_eq!(
1252+
op_to_unity(&[DataValue::List(vec![])]).unwrap(),
1253+
DataValue::from(0)
1254+
);
1255+
assert_eq!(
1256+
op_to_unity(&[DataValue::List(vec![DataValue::Null])]).unwrap(),
1257+
DataValue::from(1)
1258+
);
1259+
}
1260+
12201261
#[test]
12211262
fn test_to_float() {
1263+
assert_eq!(
1264+
op_to_float(&[DataValue::Null]).unwrap(),
1265+
DataValue::from(0.0)
1266+
);
1267+
assert_eq!(
1268+
op_to_float(&[DataValue::Bool(false)]).unwrap(),
1269+
DataValue::from(0.0)
1270+
);
1271+
assert_eq!(
1272+
op_to_float(&[DataValue::Bool(true)]).unwrap(),
1273+
DataValue::from(1.0)
1274+
);
12221275
assert_eq!(
12231276
op_to_float(&[DataValue::from(1)]).unwrap(),
12241277
DataValue::from(1.0)

0 commit comments

Comments
 (0)