mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
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:
committed by
GitHub
parent
25902bab57
commit
df98c8d168
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user