Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factorize errors between APIs #342

Merged
merged 5 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions book/src/applet/prelude/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'a> Command<'a> {
//} ANCHOR_END: impl_parse

//{ ANCHOR: process_signature
fn process(&self) -> Result<(), store::Error> {
fn process(&self) -> Result<(), Error> {
//} ANCHOR_END: process_signature
//{ ANCHOR: process_insert
match self {
Expand Down Expand Up @@ -160,21 +160,21 @@ impl Key {
}

//{ ANCHOR: helpers
fn insert(key: &Key, value: &[u8]) -> Result<(), store::Error> {
fn insert(key: &Key, value: &[u8]) -> Result<(), Error> {
match key {
Key::Exact(key) => store::insert(*key, value),
Key::Range(keys) => store::fragment::insert(keys.clone(), value),
}
}

fn find(key: &Key) -> Result<Option<Box<[u8]>>, store::Error> {
fn find(key: &Key) -> Result<Option<Box<[u8]>>, Error> {
match key {
Key::Exact(key) => store::find(*key),
Key::Range(keys) => store::fragment::find(keys.clone()),
}
}

fn remove(key: &Key) -> Result<(), store::Error> {
fn remove(key: &Key) -> Result<(), Error> {
match key {
Key::Exact(key) => store::remove(*key),
Key::Range(keys) => store::fragment::remove(keys.clone()),
Expand Down
2 changes: 2 additions & 0 deletions crates/api-desc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Minor

- Remove isize conversion functions for enums
- Remove custom error types
- Add `gpio` module
- Add `platform::version()`
- Add `radio::ble` module
Expand Down
20 changes: 1 addition & 19 deletions crates/api-desc/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,6 @@ pub(crate) fn new() -> Item {
/// Cryptographic operations.
};
let name = "crypto".into();
let items = vec![
item! {
/// Describes errors on cryptographic operations.
enum Error {
/// A function pre-condition was broken.
InvalidArgument = 0,

/// An operation is unsupported.
Unsupported = 1,

/// An RNG operation failed.
RngFailure = 2,
}
},
ccm::new(),
ec::new(),
gcm::new(),
hash::new(),
];
let items = vec![ccm::new(), ec::new(), gcm::new(), hash::new()];
Item::Mod(Mod { docs, name, items })
}
6 changes: 2 additions & 4 deletions crates/api-desc/src/crypto/ccm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ pub(crate) fn new() -> Item {
/// Its length must be `len + 4` bytes.
cipher: *mut u8,
} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand Down Expand Up @@ -83,8 +82,7 @@ pub(crate) fn new() -> Item {
/// Its length must be provided in the `len` field.
clear: *mut u8,
} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand Down
12 changes: 4 additions & 8 deletions crates/api-desc/src/crypto/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ pub(crate) fn new() -> Item {
/// The y coordinate in SEC1 encoding.
y: *mut u8,
} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand All @@ -108,8 +107,7 @@ pub(crate) fn new() -> Item {
/// The y coordinate of the output point in SEC1 encoding.
out_y: *mut u8,
} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand All @@ -131,8 +129,7 @@ pub(crate) fn new() -> Item {
/// The s signature component in SEC1 encoding.
s: *mut u8,
} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand All @@ -157,8 +154,7 @@ pub(crate) fn new() -> Item {
/// The s signature component in SEC1 encoding.
s: *const u8,
} -> {
/// 1 if the signature is valid, 0 if invalid, and bitwise complement of
/// [`Error`](crate::crypto::Error) otherwise.
/// One if the signature is valid. Zero if invalid. Negative on error.
res: isize,
}
},
Expand Down
6 changes: 2 additions & 4 deletions crates/api-desc/src/crypto/gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ pub(crate) fn new() -> Item {
/// The authentication tag (see [`super::tag_length()`]).
tag: *mut u8,
} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand Down Expand Up @@ -113,8 +112,7 @@ pub(crate) fn new() -> Item {
/// The clear text.
clear: *mut u8,
} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand Down
15 changes: 5 additions & 10 deletions crates/api-desc/src/crypto/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ pub(crate) fn new() -> Item {
/// The hash algorithm.
algorithm: usize,
} -> {
/// A non-negative identifier on success, bitwise complement of
/// [`Error`](crate::crypto::Error) otherwise.
/// Negative on error. The identifier otherwise.
id: isize,
}
},
Expand Down Expand Up @@ -83,8 +82,7 @@ pub(crate) fn new() -> Item {
/// without computing the digest.
digest: *mut u8,
} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand Down Expand Up @@ -112,8 +110,7 @@ pub(crate) fn new() -> Item {
/// If greater than 64 bytes, the key will be itself hashed.
key_len: usize,
} -> {
/// A non-negative identifier on success, bitwise complement of
/// [`Error`](crate::crypto::Error) otherwise.
/// Negative on error. The identifier otherwise.
id: isize,
}
},
Expand Down Expand Up @@ -149,8 +146,7 @@ pub(crate) fn new() -> Item {
/// without computing the hmac.
hmac: *mut u8,
} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative otherwise.
res: isize,
}
},
Expand Down Expand Up @@ -197,8 +193,7 @@ pub(crate) fn new() -> Item {
okm_len: usize,

} -> {
/// Zero on success, bitwise complement of [`Error`](crate::crypto::Error)
/// otherwise.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand Down
16 changes: 2 additions & 14 deletions crates/api-desc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,6 @@ impl Enum {
impl From<#name> for crate::U32<usize> {
fn from(x: #name) -> Self { (x as u32).into() }
}
impl From<#name> for crate::U32<isize> {
fn from(x: #name) -> Self { (!(x as u32)).into() }
}
}
}

Expand All @@ -221,12 +218,6 @@ impl Enum {
(x as u32).try_into().unwrap()
}
}
impl #name {
pub fn to_result(x: isize) -> Result<usize, Self> {
let y = x as usize;
if x < 0 { Err((!y).into()) } else { Ok(y) }
}
}
}
}

Expand All @@ -241,20 +232,17 @@ impl Enum {
#[repr(u32)]
pub enum #name { #(#variants),* }
impl TryFrom<u32> for #name {
type Error = ();
type Error = wasefire_error::Error;

fn try_from(x: u32) -> Result<Self, Self::Error> {
if x < #num_variants as u32 {
// SAFETY: See `tests::enum_values_are_valid()`.
Ok(unsafe { core::mem::transmute(x) })
} else {
Err(())
Err(wasefire_error::Error::internal(0))
}
}
}
impl From<#name> for isize {
fn from(x: #name) -> Self { !(x as usize) as isize }
}
}
}

Expand Down
10 changes: 1 addition & 9 deletions crates/api-desc/src/radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ pub(crate) fn new() -> Item {
/// Radio operations.
};
let name = "radio".into();
let items = vec![
item! {
/// Describes errors on radio operations.
enum Error {
Unknown = 0,
}
},
ble::new(),
];
let items = vec![ble::new()];
Item::Mod(Mod { docs, name, items })
}
4 changes: 2 additions & 2 deletions crates/api-desc/src/radio/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub(crate) fn new() -> Item {
/// Pointer to the [`super::Advertisement`] packet.
ptr: *mut u8,
} -> {
/// One if a packet was read. Zero if there was no packet to read. Otherwise
/// complement of error number.
/// One if a packet was read. Zero if there was no packet to read. Negative on
/// error.
res: isize,
}
},
Expand Down
27 changes: 4 additions & 23 deletions crates/api-desc/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,6 @@ pub(crate) fn new() -> Item {
};
let name = "store".into();
let items = vec![
item! {
/// Describes errors interacting with the store.
enum Error {
/// A function pre-condition was broken.
InvalidArgument = 0,

/// The store is full.
NoCapacity = 1,

/// The store reached its end of life.
NoLifetime = 2,

/// An operation to the underlying storage failed.
StorageError = 3,

/// The underlying storage doesn't match the store invariant.
InvalidStorage = 4,
}
},
item! {
/// Inserts an entry in the store.
///
Expand All @@ -57,7 +38,7 @@ pub(crate) fn new() -> Item {
/// Length of the value.
len: usize,
} -> {
/// Zero for success. Otherwise complement of error number.
/// Zero for success. Negative on error.
res: isize,
}
},
Expand All @@ -69,7 +50,7 @@ pub(crate) fn new() -> Item {
/// Key of the entry.
key: usize,
} -> {
/// Zero for success. Otherwise complement of error number.
/// Zero for success. Negative on error.
res: isize,
}
},
Expand Down Expand Up @@ -97,11 +78,11 @@ pub(crate) fn new() -> Item {
#[cfg(feature = "multivalue")]
ptr: *mut u8,

/// Length of the value if non-negative. Otherwise complement of error number.
/// Negative on error. Otherwise length of the value.
#[cfg(feature = "multivalue")]
len: isize,

/// One if found. Zero if not found. Otherwise complement of error number.
/// One if found. Zero if not found. Negative on error.
#[cfg(not(feature = "multivalue"))]
res: isize,
}
Expand Down
10 changes: 1 addition & 9 deletions crates/api-desc/src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ pub(crate) fn new() -> Item {
/// USB operations.
};
let name = "usb".into();
let items = vec![
item! {
/// Describes errors on USB operations.
enum Error {
Unknown = 0,
}
},
serial::new(),
];
let items = vec![serial::new()];
Item::Mod(Mod { docs, name, items })
}
2 changes: 1 addition & 1 deletion crates/api-desc/src/usb/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) fn new() -> Item {
item! {
/// Flushs the USB serial.
fn flush "usf" {} -> {
/// Zero on success, -1 on error.
/// Zero on success. Negative on error.
res: isize,
}
},
Expand Down
6 changes: 5 additions & 1 deletion crates/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Update `wasefire-applet-api-macro` version

### Minor

- Implement conversion from types involving `Error` to `U32`

### Patch

- Build docs.rs for `wasm32-unknown-unknown` only
Expand Down Expand Up @@ -68,4 +72,4 @@

## 0.1.0

<!-- Increment to skip CHANGELOG.md test: 6 -->
<!-- Increment to skip CHANGELOG.md test: 7 -->
Loading