From 0096939bf6edd9fd87cd555820c741b0da5508f2 Mon Sep 17 00:00:00 2001 From: sterni Date: Mon, 19 Sep 2022 10:14:05 +0200 Subject: feat(tvix/eval): implement builtins.splitVersion This was fairly easy, thanks to the work already done by Thomas Frank. However, the implementation is suboptimal because we parse number parts only to convert them back to strings afterwards. I've chosen to tackle this problem in the future, since having an (inefficient) implementation of splitVersion will be helpful for debugging the slight discrepancies between C++ Nix and Tvix in the case of compareVersions. Change-Id: Id6ed8eeb77663ff650c8c53ea952875b1fb7ea84 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6688 Autosubmit: sterni Reviewed-by: grfn Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'tvix/eval') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 1ea23d49e632..02457340dcd1 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -18,7 +18,7 @@ use crate::{ use crate::arithmetic_op; -use self::versions::VersionPartsIter; +use self::versions::{VersionPart, VersionPartsIter}; pub mod versions; @@ -213,6 +213,21 @@ fn pure_builtins() -> Vec { let a = args.pop().unwrap(); arithmetic_op!(a, b, *) }), + Builtin::new("splitVersion", &[true], |args, _| { + let s = args[0].to_str()?; + let s = VersionPartsIter::new(s.as_str()); + + let parts = s + .map(|s| { + Value::String(match s { + // TODO(sterni): we should avoid converting back and forth here + VersionPart::Number(n) => format!("{n}").into(), + VersionPart::Word(w) => w.into(), + }) + }) + .collect::>(); + Ok(Value::List(NixList::construct(parts.len(), parts))) + }), Builtin::new("sub", &[true, true], |mut args, _| { let b = args.pop().unwrap(); let a = args.pop().unwrap(); -- cgit 1.4.1