no_std support (#232)

- Disable default features in dependencies
- Use anyhow::Error::msg to map errors. Note: anyhow will itself be removed later.
- lazy_static/spin_no_std used in no_std environments
- ensure_no_std binary is built to target  thumbv7m-none-eabi to ensure that
  there are no std dependencies.  thumbv7m-none-eabi target has no std support.
- The opa-no-std feature enables only those Regorus features that work with no_std.
- Enable tests with no_std
- Update sizes of regorus binary in  README.md
- Ensure that regorus example can be built with only std
- Ensure that regorus example can be built with no_std

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2024-05-13 09:42:35 -04:00
committed by GitHub
parent 01fc234a33
commit e86b590f91
25 changed files with 343 additions and 173 deletions

View File

@@ -1,7 +1,37 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use anyhow::{bail, Result};
use anyhow::{anyhow, bail, Result};
#[allow(dead_code)]
fn read_file(path: &String) -> Result<String> {
std::fs::read_to_string(path).map_err(|_| anyhow!("could not read {path}"))
}
#[allow(unused_variables)]
fn read_value_from_yaml_file(path: &String) -> Result<regorus::Value> {
#[cfg(feature = "yaml")]
return regorus::Value::from_yaml_file(path);
#[cfg(not(feature = "yaml"))]
bail!("regorus has not been built with yaml support");
}
fn read_value_from_json_file(path: &String) -> Result<regorus::Value> {
#[cfg(feature = "std")]
return regorus::Value::from_json_file(path);
#[cfg(not(feature = "std"))]
regorus::Value::from_json_str(&read_file(path)?)
}
fn add_policy_from_file(engine: &mut regorus::Engine, path: String) -> Result<()> {
#[cfg(feature = "std")]
return engine.add_policy_from_file(path);
#[cfg(not(feature = "std"))]
engine.add_policy(path.clone(), read_file(&path)?)
}
fn rego_eval(
bundles: &[String],
@@ -35,7 +65,7 @@ fn rego_eval(
_ => continue,
}
engine.add_policy_from_file(entry.path())?;
add_policy_from_file(&mut engine, entry.path().display().to_string())?;
}
}
@@ -43,15 +73,15 @@ fn rego_eval(
for file in files.iter() {
if file.ends_with(".rego") {
// Read policy file.
engine.add_policy_from_file(file)?;
add_policy_from_file(&mut engine, file.clone())?;
} else {
// Read data file.
let data = if file.ends_with(".json") {
regorus::Value::from_json_file(file)?
read_value_from_json_file(file)?
} else if file.ends_with(".yaml") {
regorus::Value::from_yaml_file(file)?
read_value_from_yaml_file(file)?
} else {
bail!("Unsupported data file `{file}`. Must be rego, json or yaml.")
bail!("Unsupported data file `{file}`. Must be rego, json or yaml.");
};
// Merge given data.
@@ -61,9 +91,9 @@ fn rego_eval(
if let Some(file) = input {
let input = if file.ends_with(".json") {
regorus::Value::from_json_file(&file)?
read_value_from_json_file(&file)?
} else if file.ends_with(".yaml") {
regorus::Value::from_yaml_file(&file)?
read_value_from_yaml_file(&file)?
} else {
bail!("Unsupported input file `{file}`. Must be json or yaml.")
};
@@ -95,8 +125,12 @@ fn rego_lex(file: String, verbose: bool) -> Result<()> {
use regorus::unstable::*;
// Create source.
#[cfg(feature = "std")]
let source = Source::from_file(file)?;
#[cfg(not(feature = "std"))]
let source = Source::from_contents(file.clone(), read_file(&file)?)?;
// Create lexer.
let mut lexer = Lexer::new(&source);
@@ -122,8 +156,12 @@ fn rego_parse(file: String) -> Result<()> {
use regorus::unstable::*;
// Create source.
#[cfg(feature = "std")]
let source = Source::from_file(file)?;
#[cfg(not(feature = "std"))]
let source = Source::from_contents(file.clone(), read_file(&file)?)?;
// Create a parser and parse the source.
let mut parser = Parser::new(&source)?;
let ast = parser.parse()?;