Skip to content

Commit

Permalink
Wrap boon errors
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Nov 12, 2024
1 parent 135310f commit 541f27d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ All notable changes to this project will be documented in this file. It uses the
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html
"Semantic Versioning 2.0.0"

## [v0.5.0] — Unreleased

### ⚡ Improvements

* Added the [error module], which defines all the errors returned by
pgxn_meta.
* Changed the errors returned from the [valid module] from boxed
[boon] errors with lifetimes to [error module] errors with no lifetimes.

[v0.3.0]: https://github.com/pgxn/meta/compare/v0.4.0...v0.5.0
[error module]: https://docs.rs/pgxn_meta/0.5.0/pgxn_meta/error/
[valid module]: https://docs.rs/pgxn_meta/0.5.0/pgxn_meta/valid/
[boon]: https://github.com/santhosh-tekuri/boon

## [v0.4.0] — 2024-10-08

The theme of this release is *JSON Web Signatures.*
Expand Down
22 changes: 18 additions & 4 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@ pub enum Error {
#[error("{}", .0.reason)]
License(#[from] spdx::error::ParseError),

/// UnknownSpec errors are returned when the validator cannot determine
/// the version of the meta spec.
/// Validator cannot determine the version of the meta spec.
#[error("Cannot determine meta-spec version")]
UnknownSpec,

/// UnknownSchemaID errors are returned by new() when a schema file has no `$id`
/// property.
/// A schema file has no `$id` property.
#[error("No $id found in schema")]
UnknownSchemaId,

/// JSON Schema compile error.
#[error(transparent)]
#[allow(clippy::enum_variant_names)]
CompileError(#[from] boon::CompileError),

// JSON Schema validation error.
#[error("{0}")]
#[allow(clippy::enum_variant_names)]
ValidationError(String),
}

impl<'s, 'v> From<boon::ValidationError<'s, 'v>> for Error {
fn from(value: boon::ValidationError<'s, 'v>) -> Self {
Self::ValidationError(value.to_string())
}
}
25 changes: 25 additions & 0 deletions src/error/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use serde_json::json;

#[test]
fn spdx() {
Expand All @@ -22,3 +23,27 @@ fn unknown_spec() {
fn unknown_schema_id() {
assert_eq!(Error::UnknownSchemaId.to_string(), "No $id found in schema")
}

#[test]
fn compile() {
let mut c = boon::Compiler::new();
c.add_resource("foo", json!("not a schema")).unwrap();
let mut s = boon::Schemas::new();
let compile_err = c.compile("foo", &mut s).unwrap_err();
assert_eq!(
compile_err.to_string(),
Error::CompileError(compile_err).to_string(),
);
}

#[test]
fn validation() {
let mut c = boon::Compiler::new();
c.add_resource("foo", json!({"type": "object"})).unwrap();
let mut s = boon::Schemas::new();
let idx = c.compile("foo", &mut s).unwrap();
let json = json!([]);
let valid_err = s.validate(&json, idx).unwrap_err();
let err: Error = valid_err.into();
assert_eq!(err.to_string(), err.to_string());
}
28 changes: 5 additions & 23 deletions src/valid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ impl Validator {
/// success and a validation error on failure.
///
/// See the [module docs](crate::valid) for an example.
pub fn validate<'a>(
&'a mut self,
meta: &'a Value,
) -> Result<u8, Box<dyn std::error::Error + 'a>> {
pub fn validate(&mut self, meta: &Value) -> Result<u8, Error> {
self.validate_schema(meta, "distribution.schema.json")
}

Expand All @@ -113,10 +110,7 @@ impl Validator {
/// [JSON Serialization]: https://datatracker.ietf.org/doc/html/rfc7515#section-7.2
/// [RFC 5]: https://github.com/pgxn/rfcs/pull/5
/// [JSON Web Signature]: https://datatracker.ietf.org/doc/html/rfc7515
pub fn validate_release<'a>(
&'a mut self,
meta: &'a Value,
) -> Result<u8, Box<dyn std::error::Error + 'a>> {
pub fn validate_release(&mut self, meta: &Value) -> Result<u8, Error> {
self.validate_schema(meta, "release.schema.json")
}

Expand All @@ -133,28 +127,16 @@ impl Validator {
///
/// [JSON Serialization]: https://datatracker.ietf.org/doc/html/rfc7515#section-7.2
/// [RFC 5]: https://github.com/pgxn/rfcs/pull/5
pub fn validate_payload<'a>(
&'a mut self,
meta: &'a Value,
) -> Result<(), Box<dyn std::error::Error + 'a>> {
pub fn validate_payload(&mut self, meta: &Value) -> Result<(), Error> {
self.validate_version_schema(meta, 2, "payload.schema.json")
}

fn validate_schema<'a>(
&'a mut self,
meta: &'a Value,
schema: &str,
) -> Result<u8, Box<dyn std::error::Error + 'a>> {
fn validate_schema(&mut self, meta: &Value, schema: &str) -> Result<u8, Error> {
let v = util::get_version(meta).ok_or(Error::UnknownSpec)?;
self.validate_version_schema(meta, v, schema).map(|()| v)
}

fn validate_version_schema<'a>(
&'a mut self,
meta: &'a Value,
v: u8,
schema: &str,
) -> Result<(), Box<dyn std::error::Error + 'a>> {
fn validate_version_schema(&mut self, meta: &Value, v: u8, schema: &str) -> Result<(), Error> {
let id = format!("{SCHEMA_BASE}{v}/{schema}");

let compiler = &mut self.compiler;
Expand Down

0 comments on commit 541f27d

Please sign in to comment.