mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
ed3492fd7b
Number is implemented using rust_decimal::Decimal which uses a 96 bit mantissa. TODO: a) Support u64, i64 variants b) Determine desired semantics for floating-point c) Determine desired big integer length d) Explore other big int/big float crates Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
44 lines
1.3 KiB
Rust
44 lines
1.3 KiB
Rust
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
use crate::ast::{Expr, Ref};
|
|
use crate::builtins;
|
|
use crate::builtins::utils::{ensure_args_count, ensure_string};
|
|
use crate::lexer::Span;
|
|
use crate::value::Value;
|
|
|
|
use semver::Version;
|
|
|
|
use std::cmp::Ordering;
|
|
use std::collections::HashMap;
|
|
|
|
use anyhow::{Ok, Result};
|
|
|
|
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
|
m.insert("semver.compare", (compare, 2));
|
|
m.insert("semver.is_valid", (is_valid, 1));
|
|
}
|
|
|
|
fn compare(span: &Span, params: &[Ref<Expr>], args: &[Value]) -> Result<Value> {
|
|
let name = "semver.compare";
|
|
ensure_args_count(span, name, params, args, 2)?;
|
|
|
|
let v1 = ensure_string(name, ¶ms[0], &args[0])?;
|
|
let v2 = ensure_string(name, ¶ms[1], &args[1])?;
|
|
let version1 = Version::parse(&v1)?;
|
|
let version2 = Version::parse(&v2)?;
|
|
let result = match version1.cmp_precedence(&version2) {
|
|
Ordering::Less => -1,
|
|
Ordering::Equal => 0,
|
|
Ordering::Greater => 1,
|
|
};
|
|
Ok(Value::from(result as i64))
|
|
}
|
|
|
|
fn is_valid(span: &Span, params: &[Ref<Expr>], args: &[Value]) -> Result<Value> {
|
|
let name = "semver.is_valid";
|
|
ensure_args_count(span, name, params, args, 1)?;
|
|
let v = ensure_string(name, ¶ms[0], &args[0])?;
|
|
Ok(Value::Bool(Version::parse(&v).is_ok()))
|
|
}
|