Skip to content

Commit

Permalink
Simpler error propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jan 23, 2024
1 parent 928717e commit 29dddd2
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions crates/samples/components/json_validator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,19 @@ extern "system" fn CloseJsonValidator(handle: usize) {
fn create_validator(schema: *const u8, schema_len: usize, handle: *mut usize) -> Result<()> {
let schema = json_from_raw_parts(schema, schema_len)?;

match JSONSchema::compile(&schema) {
Ok(compiled) => {
if handle.is_null() {
return Err(E_POINTER.into());
}

// The handle is not null so we can safely dereference it here.
unsafe {
*handle = Box::into_raw(Box::new(compiled)) as usize;
}

Ok(())
}
Err(error) => Err(Error::new(E_INVALIDARG, error.to_string().into())),
let compiled = JSONSchema::compile(&schema)
.map_err(|error| Error::new(E_INVALIDARG, error.to_string().into()))?;

if handle.is_null() {
return Err(E_POINTER.into());
}

// The handle is not null so we can safely dereference it here.
unsafe {
*handle = Box::into_raw(Box::new(compiled)) as usize;
}

Ok(())
}

// Implementation of the `ValidateJson` function so we can use `Result` for simplicity.
Expand Down Expand Up @@ -84,14 +82,10 @@ fn json_from_raw_parts(value: *const u8, value_len: usize) -> Result<serde_json:

let value = unsafe { std::slice::from_raw_parts(value, value_len) };

let Ok(value) = std::str::from_utf8(value) else {
return Err(ERROR_NO_UNICODE_TRANSLATION.into());
};
let value =
std::str::from_utf8(value).map_err(|_| Error::from(ERROR_NO_UNICODE_TRANSLATION))?;

match serde_json::from_str(value) {
Ok(value) => Ok(value),
Err(error) => Err(Error::new(E_INVALIDARG, format!("{error}").into())),
}
serde_json::from_str(value).map_err(|error| Error::new(E_INVALIDARG, format!("{error}").into()))
}

#[test]
Expand Down

0 comments on commit 29dddd2

Please sign in to comment.