Skip to content

Commit

Permalink
Create error types that can be used with uniffi
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Jul 28, 2024
1 parent 674b0a2 commit 148f9a9
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ keywords = ["bitcoin", "bip329", "labels"]
categories = ["bitcoin"]

[features]
default = ["encryption"]
default = ["encryption", "uniffi"]
encryption = ["dep:age", "dep:hex"]
uniffi = ["dep:uniffi"]

Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "uniffi")]
pub mod ffi;

/// Errors that can occur when parsing a label.
#[derive(Debug, thiserror::Error)]
pub enum ParseError {
Expand Down
90 changes: 90 additions & 0 deletions src/error/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/// Errors that can occur when parsing a label.
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum ParseError {
#[error("Unable to read file: {0}")]
FileReadError(String),

#[error("Unable to parse file: {0}")]
ParseError(String),
}

/// Errors that can occur when exporting a label.
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum ExportError {
#[error("Unable to write file: {0}")]
FileWriteError(String),

#[error("Unable to serialize labels : {0}")]
SerializeError(String),
}

/// Errors that can occur when encrypting or decrypting a label.
#[cfg(feature = "encryption")]
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum EncryptionError {
#[error("Unable to encrypt labels: {0}")]
EncryptError(String),

#[error("Unable to encrypt labels: {0}")]
DecryptError(String),

#[error("Unable to parse labels: {0}")]
ParseError(#[from] ParseError),

#[error("Unable to export labels: {0}")]
ExportError(#[from] ExportError),

#[error("Unable to write labels: {0}")]
WriteError(String),

#[error("Decrypted to invalid UTF-8 string: {0}")]
Utf8Error(String),

#[error("Invalid hex encoded string: {0}")]
HexError(String),
}

impl From<crate::error::ParseError> for ParseError {
fn from(e: crate::error::ParseError) -> Self {
match e {
crate::error::ParseError::FileReadError(e) => ParseError::FileReadError(e.to_string()),
crate::error::ParseError::ParseError(e) => ParseError::ParseError(e.to_string()),
}
}
}

impl From<crate::error::ExportError> for ExportError {
fn from(e: crate::error::ExportError) -> Self {
match e {
crate::error::ExportError::FileWriteError(e) => {
ExportError::FileWriteError(e.to_string())
}
crate::error::ExportError::SerializeError(e) => {
ExportError::SerializeError(e.to_string())
}
}
}
}

#[cfg(feature = "encryption")]
impl From<crate::error::EncryptionError> for EncryptionError {
fn from(e: crate::error::EncryptionError) -> Self {
match e {
crate::error::EncryptionError::EncryptError(e) => {
EncryptionError::EncryptError(e.to_string())
}
crate::error::EncryptionError::DecryptError(e) => {
EncryptionError::DecryptError(e.to_string())
}
crate::error::EncryptionError::ParseError(e) => EncryptionError::ParseError(e.into()),
crate::error::EncryptionError::ExportError(e) => EncryptionError::ExportError(e.into()),
crate::error::EncryptionError::WriteError(e) => {
EncryptionError::WriteError(e.to_string())
}
crate::error::EncryptionError::Utf8Error(e) => {
EncryptionError::Utf8Error(e.to_string())
}
crate::error::EncryptionError::HexError(e) => EncryptionError::HexError(e.to_string()),
}
}
}
22 changes: 17 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! A library for working with (BIP329 labels)[https://github.com/bitcoin/bips/blob/master/bip-00329.mediawiki].
//! A library for working with [BIP329 labels](https://github.com/bitcoin/bips/blob/master/bip-00329.mediawiki).
//!
//! This library provides a way to work with BIP329 labels in a Rust program.
//!
//! The main data structure is the `Labels` struct, which is a list of `Label` structs.
//! The main data structure is the `[Labels]` struct, which is a list of `Label` structs.
//!
//! The `Label` enum is a discriminated union of all the different types of labels.
//! The `[Label]` enum is a discriminated union of all the different types of labels.
//!
//! The `Labels` struct can be exported to a JSONL file.
//! The `[Labels]` struct can be exported to a JSONL file.
//!
//! The `Labels` struct can be imported from a JSONL file.
//! The `[Labels]` struct can be imported from a JSONL file.
//!
//! Example Import:
//! ```rust
Expand All @@ -26,6 +26,18 @@
//!
//! // Create a JSONL string
//! let jsonl = labels.export().unwrap();
//! ```
//!
//! You can encrypt and decrypt the `[Labels]` struct using the `encryption` feature.
//!
//! Example encryption:
//! ```rust
//! use bip329::{Labels, encryption::EncryptedLabels};
//!
//! let labels = Labels::try_from_file("tests/data/labels.jsonl").unwrap();
//! let encrypted = EncryptedLabels::encrypt(&labels, "passphrase").unwrap();
//! let decrypted = encrypted.decrypt("passphrase").unwrap();
//! ```
//!
pub mod error;

Expand Down

0 comments on commit 148f9a9

Please sign in to comment.