Skip to content

Commit

Permalink
resolve anyhow compile errors (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavemarshall authored Jan 6, 2025
1 parent d955ae1 commit ba3a128
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions scripts/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ if [ -f Cargo.toml ]; then

# Ensure that all tests pass with extensions
cargo test -r --features rego-extensions
cargo test -r --test aci rego-extensions
cargo test -r --test kata rego-extensions
cargo test -r --test aci --features rego-extensions
cargo test -r --test kata --features rego-extensions

# Ensure that OPA conformance tests don't regress.
cargo test -r --features opa-testutil,serde_json/arbitrary_precision,rego-extensions --test opa -- $(tr '\n' ' ' < tests/opa.passing)
Expand Down
10 changes: 5 additions & 5 deletions src/builtins/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ fn yaml_is_valid(
fn yaml_marshal(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) -> Result<Value> {
let name = "yaml.marshal";
ensure_args_count(span, name, params, args, 1)?;
Ok(Value::String(
serde_yaml::to_string(&args[0])
.with_context(|| span.error("could not serialize to yaml"))?
.into(),
))

let serialized = serde_yaml::to_string(&args[0])
.map_err(|err| span.error(&format!("could not serialize to yaml: {}", err)))?;

Ok(Value::String(serialized.into()))
}

#[cfg(feature = "yaml")]
Expand Down
3 changes: 2 additions & 1 deletion src/builtins/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ fn json_match_schema(
ensure_args_count(span, name, params, args, 2)?;

// The following is expected to succeed.
let document: serde_json::Value = serde_json::from_str(&args[0].to_json_str()?)?;
let document: serde_json::Value = serde_json::from_str(&args[0].to_json_str()?)
.map_err(|err| span.error(&format!("Failed to parse JSON: {}", err)))?;

Ok(Value::from_array(
match compile_json_schema(&params[1], &args[1]) {
Expand Down
6 changes: 5 additions & 1 deletion src/builtins/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ fn sleep(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) -> Re
let dur = time::compat::parse_duration(val.as_ref())
.map_err(|e| params[0].span().error(&format!("{e}")))?;

thread::sleep(dur.to_std()?);
let std_dur = dur
.to_std()
.map_err(|err| anyhow::anyhow!("Failed to convert to std::time::Duration: {err}"))?;

thread::sleep(std_dur);

Ok(Value::Null)
}
6 changes: 4 additions & 2 deletions src/builtins/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ fn parse_ns(span: &Span, params: &[Ref<Expr>], args: &[Value], strict: bool) ->
let layout = ensure_string(name, &params[0], &args[0])?;
let value = ensure_string(name, &params[1], &args[1])?;

let datetime = compat::parse(layout_with_predefined_formats(&layout), &value)?;
let datetime = compat::parse(layout_with_predefined_formats(&layout), &value)
.map_err(|err| anyhow::anyhow!("Failed to parse datetime: {}", err))?;
safe_timestamp_nanos(span, strict, datetime.timestamp_nanos_opt())
}

Expand All @@ -173,7 +174,8 @@ fn parse_rfc3339_ns(

let value = ensure_string(name, &params[0], &args[0])?;

let datetime = DateTime::parse_from_rfc3339(&value)?;
let datetime = DateTime::parse_from_rfc3339(&value)
.map_err(|err| anyhow::anyhow!("Failed to parse datetime: {}", err))?;
safe_timestamp_nanos(span, strict, datetime.timestamp_nanos_opt())
}

Expand Down
2 changes: 1 addition & 1 deletion src/builtins/time/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ struct GoTimeFormatItems<'a> {
mode: GoTimeFormatItemsMode,
}

impl<'a> GoTimeFormatItems<'a> {
impl GoTimeFormatItems<'_> {
fn parse(reminder: &str) -> GoTimeFormatItems {
GoTimeFormatItems {
reminder,
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ where
}

/// Implement clone for a boxed extension using [`Extension::clone_box`].
impl<'a> Clone for Box<dyn 'a + Extension> {
impl Clone for Box<dyn '_ + Extension> {
fn clone(&self) -> Self {
(**self).clone_box()
}
Expand Down Expand Up @@ -405,7 +405,6 @@ pub mod coverage {
/// Lines that are not covered are red.
///
/// <img src="https://github.com/microsoft/regorus/blob/main/docs/coverage.png?raw=true">
pub fn to_string_pretty(&self) -> anyhow::Result<String> {
let mut s = String::default();
s.push_str("COVERAGE REPORT:\n");
Expand Down
4 changes: 3 additions & 1 deletion src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ impl Value {
#[cfg(feature = "yaml")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn from_yaml_str(yaml: &str) -> Result<Value> {
Ok(serde_yaml::from_str(yaml)?)
let value = serde_yaml::from_str(yaml)
.map_err(|err| anyhow::anyhow!("Failed to parse YAML: {}", err))?;
Ok(value)
}

/// Deserialize a value from a file containing YAML.
Expand Down

0 comments on commit ba3a128

Please sign in to comment.