tracing::trace builtin

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2023-03-04 05:27:31 -08:00
committed by Anand Krishnamoorthi
parent 81f6199642
commit 112623c762
8 changed files with 77 additions and 11 deletions
+2 -1
View File
@@ -9,6 +9,7 @@ mod debugging;
pub mod numbers;
pub mod sets;
mod strings;
mod tracing;
pub mod types;
mod utils;
@@ -56,7 +57,7 @@ lazy_static! {
//rego::register(&mut m);
//opa::register(&mut m);
debugging::register(&mut m);
//tracing::register(&mut m);
tracing::register(&mut m);
m
};
+30
View File
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use crate::ast::Expr;
use crate::builtins;
use crate::builtins::utils::{ensure_args_count, ensure_string};
use crate::lexer::Span;
use crate::value::Value;
use std::collections::HashMap;
use anyhow::Result;
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
m.insert("trace", trace);
}
// Symbol analyzer must ensure that vars used by trace are defined before
// the trace statement. Scheduler must ensure the above constraint.
fn trace(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
let name = "trace";
ensure_args_count(span, name, params, args, 1)?;
let msg = ensure_string(name, &params[0], &args[0])?;
// Unlike rego, trace returns a string instead of bool.
// The interpreter accumulates the traces.
// TODO: Stateful bultins can pass in a state that would allow capturing
// the traces in the state.
Ok(Value::String(msg))
}