Provide ability to get JSON representation of policy AST (#266)

closes #265

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2024-06-08 21:58:30 -04:00
committed by GitHub
parent 25902bab57
commit df98c8d168
15 changed files with 191 additions and 6 deletions

View File

@@ -170,8 +170,40 @@ fn rego_parse(file: String) -> Result<()> {
Ok(())
}
#[allow(unused_variables)]
fn rego_ast(file: String) -> Result<()> {
#[cfg(feature = "ast")]
{
// Create engine.
let mut engine = regorus::Engine::new();
// Create source.
#[cfg(feature = "std")]
engine.add_policy_from_file(file)?;
#[cfg(not(feature = "std"))]
engine.add_policy(file.clone(), read_file(&file)?)?;
let ast = engine.get_ast_as_json()?;
println!("{ast}");
Ok(())
}
#[cfg(not(feature = "ast"))]
{
bail!("`ast` feature must be enabled");
}
}
#[derive(clap::Subcommand)]
enum RegorusCommand {
/// Parse a Rego policy and dump AST.
Ast {
/// Rego policy file.
file: String,
},
/// Evaluate a Rego Query.
Eval {
/// Directories containing Rego files.
@@ -254,5 +286,6 @@ fn main() -> Result<()> {
),
RegorusCommand::Lex { file, verbose } => rego_lex(file, verbose),
RegorusCommand::Parse { file } => rego_parse(file),
RegorusCommand::Ast { file } => rego_ast(file),
}
}