diff --git a/Cargo.toml b/Cargo.toml index 49dc320..30d6ed5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,10 +12,11 @@ description = "A flexible and secure key storage interface for working with cryp keywords = ["crypto", "storage", "keys", "signatures", "security"] [dependencies] -anyhow = "1" -thiserror = "2" -async-trait = "0.1" +thiserror = { version = "2", optional = true, default-features = false } +async-trait = { version = "0.1", optional = true } [features] -default = ["send-sync-storage"] +default = ["std", "send-sync-storage"] +std = ["thiserror/std", "async-trait"] send-sync-storage = [] +no-std = ["thiserror", "async-trait"] diff --git a/src/error.rs b/src/error.rs index 7fb2167..0a1d739 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,8 +3,21 @@ use thiserror::Error; -pub type Result = std::result::Result; +#[cfg(not(feature = "std"))] +extern crate alloc; +#[cfg(not(feature = "std"))] +use alloc::boxed::Box; + +#[cfg(feature = "std")] +use std::error::Error as StdError; + +#[cfg(not(feature = "std"))] +use core::error::Error as CoreError; + +pub type Result = core::result::Result; + +#[cfg(feature = "std")] #[derive(Debug, Error)] pub enum Error { #[error("key with ID {0} could not be found")] @@ -14,5 +27,18 @@ pub enum Error { #[error("failed to generate key with provided options")] InvalidOptions, #[error(transparent)] - Other(anyhow::Error), + Other(Box), +} + +#[cfg(not(feature = "std"))] +#[derive(Debug, Error)] +pub enum Error { + #[error("key with ID {0} could not be found")] + KeyNotFound(alloc::string::String), + #[error("unable to communicate with key store: {0}")] + StoreDisconnected(alloc::string::String), + #[error("failed to generate key with provided options")] + InvalidOptions, + #[error(transparent)] + Other(Box), } diff --git a/src/lib.rs b/src/lib.rs index 39f6ffc..f0795f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,14 @@ // Copyright 2020-2024 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(feature = "std")] +extern crate std; + +#[cfg(not(feature = "std"))] +extern crate alloc; + mod error; mod signature_scheme; mod signer; diff --git a/src/signer.rs b/src/signer.rs index faa7fcd..1488190 100644 --- a/src/signer.rs +++ b/src/signer.rs @@ -3,6 +3,9 @@ use async_trait::async_trait; +#[cfg(not(feature = "std"))] +use alloc::boxed::Box; + use crate::Result; use crate::SignatureScheme; diff --git a/src/storage.rs b/src/storage.rs index d84917e..72e9dc0 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -3,6 +3,9 @@ use async_trait::async_trait; +#[cfg(not(feature = "std"))] +use alloc::boxed::Box; + use crate::signature_scheme::SignatureScheme; use crate::signer::Signer; use crate::Result;