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

feat(derive): update __derive::Error variant #34

Merged
merged 1 commit into from
Feb 22, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"
members = ["stacks", "stacks_derive", "tests"]

[workspace.package]
version = "0.2.5"
version = "0.2.6"
edition = "2021"
license = "MIT OR Apache-2.0"
authors = ["Max Karou <maxkarou@protonmail.com>"]
Expand Down
11 changes: 7 additions & 4 deletions stacks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,22 @@ features = ["json"]
rand = "0.8.5"

[features]
default = ["clarity", "transaction", "wallet-sdk", "rpc"]
default = ["crypto", "clarity", "transaction", "wallet-sdk"]

# Provide convenience derive(...) macros.
derive = ["stacks_derive"]
derive = ["clarity", "stacks_derive"]

# Provide hex, sha, b58 & c32 primitives & methods.
crypto = []

# Provide clarity types & encoding/decoding methods.
clarity = []
clarity = ["crypto"]

# Provide transaction builders. (transfer, call etc.)
transaction = ["clarity", "typed-builder"]

# Provide a wallet-sdk
wallet-sdk = []
wallet-sdk = ["crypto"]

# Provide rpc methods.
rpc = ["clarity", "transaction", "ureq", "serde"]
24 changes: 24 additions & 0 deletions stacks/src/derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// © 2024 Max Karou. All Rights Reserved.
// Licensed under Apache Version 2.0, or MIT License, at your discretion.
//
// Apache License: http://www.apache.org/licenses/LICENSE-2.0
// MIT License: http://opensource.org/licenses/MIT
//
// Usage of this file is permitted solely under a sanctioned license.

#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Downcast error, contains key, ty, & ident.
#[error("Failed to cast field '{0}' as '{1}' on struct '{2}'")]
Cast(String, String, String),
/// Extraction error, contains key & ident.
#[error("Failed to extract value for field '{0}' on '{1}'")]
Extract(String, String),
/// Matching error, contains key & ident.
#[error("Failed to match value for field '{0}' on '{1}'")]
Match(String, String),
}

pub trait FromTuple {}

impl<T: TryFrom<crate::clarity::Tuple>> FromTuple for T {}
15 changes: 13 additions & 2 deletions stacks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,28 @@

#[cfg(feature = "clarity")]
pub mod clarity;

#[cfg(feature = "crypto")]
pub mod crypto;

#[cfg(feature = "rpc")]
pub mod rpc;

#[cfg(feature = "transaction")]
pub mod transaction;

#[cfg(feature = "wallet-sdk")]
pub mod wallet;

#[cfg(feature = "derive")]
#[path = "derive.rs"]
pub mod __derive;

#[cfg(feature = "derive")]
pub mod derive {
pub use stacks_derive::*;

pub use crate::__derive::*;
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -64,8 +75,8 @@ pub enum Error {
Wallet(#[from] wallet::Error),
#[cfg(feature = "derive")]
/// `derive` crate errors.
#[error("{0}")]
Derive(String),
#[error(transparent)]
Derive(#[from] derive::Error),
}

pub use secp256k1::PublicKey;
Expand Down
12 changes: 7 additions & 5 deletions stacks_derive/src/from_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,21 @@ where
fn to_tokens(&self, tokens: &mut TokenStream) {
let mut stream = TokenStream::new();

stream.extend(quote!(::stacks_rs::__derive::Error::));

match self {
Self::Cast(key, ty, ident) => {
stream.extend(quote!("Failed to cast field '{}' as '{}' on struct '{}'", #key, stringify!(#ty), stringify!(#ident)))
stream.extend(quote!(Cast(#key.to_string(), stringify!(#ty).to_string(), stringify!(#ident).to_string())))
}
Self::Extract(key, ident) => {
stream.extend(quote!("Failed to extract value for field '{}' on '{}'", #key, stringify!(#ident)))
stream.extend(quote!(Extract(#key.to_string(), stringify!(#ident).to_string())))
}
Self::Match(key, ident) => {
stream.extend(quote!("Failed to match value for field '{}' on '{}'", #key, stringify!(#ident)))
stream.extend(quote!(Match(#key.to_string(), stringify!(#ident).to_string())))
}
}

tokens.extend(quote!(::stacks_rs::Error::Derive(format!(#stream))));
tokens.extend(stream);
}
}

Expand Down Expand Up @@ -181,7 +183,7 @@ pub(crate) fn __impl(input: proc_macro::TokenStream) -> Result<TokenStream> {

Ok(quote!(
impl #imp ::std::convert::TryFrom<::stacks_rs::clarity::Tuple> for #ident #ty #wher {
type Error = ::stacks_rs::Error;
type Error = ::stacks_rs::__derive::Error;
fn try_from(tuple: ::stacks_rs::clarity::Tuple) -> Result<Self, Self::Error> {
use ::stacks_rs::clarity::Cast;
Ok(Self { #(#tokens),* })
Expand Down
2 changes: 1 addition & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ repository.workspace = true
publish = false

[dependencies.stacks-rs]
version = "0.2.4"
version = "0.2.6"
features = ["derive"]
path = "../stacks"
Loading