Skip to content

Commit 56e3099

Browse files
feat: consts as enum in ffi (#94)
* feat: consts as enum
1 parent 5208045 commit 56e3099

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed

crates/algokit_transact_ffi/src/lib.rs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use algokit_transact::{constants, ALGORAND_PUBLIC_KEY_BYTE_LENGTH};
2-
use algokit_transact::{
3-
AlgorandMsgpack, Byte32, EstimateTransactionSize, TransactionId, ALGORAND_SIGNATURE_BYTE_LENGTH,
4-
};
5-
use ffi_macros::{ffi_func, ffi_record};
1+
use algokit_transact::constants::*;
2+
use algokit_transact::{AlgorandMsgpack, Byte32, EstimateTransactionSize, TransactionId};
3+
use ffi_macros::{ffi_enum, ffi_func, ffi_record};
64
use serde::{Deserialize, Serialize};
75
use serde_bytes::ByteBuf;
86

@@ -477,47 +475,51 @@ pub fn get_transaction_id(tx: &Transaction) -> Result<String, AlgoKitTransactErr
477475
Ok(tx_internal.id()?)
478476
}
479477

480-
#[ffi_func]
481-
pub fn get_hash_bytes_length() -> u64 {
482-
constants::HASH_BYTES_LENGTH.try_into().unwrap()
483-
}
478+
/// Enum containing all constants used in this crate.
479+
#[ffi_enum]
480+
pub enum AlgorandConstant {
481+
/// Length of hash digests (32)
482+
HashLength,
484483

485-
#[ffi_func]
486-
pub fn get_algorand_checksum_byte_length() -> u64 {
487-
constants::ALGORAND_CHECKSUM_BYTE_LENGTH.try_into().unwrap()
488-
}
484+
/// Length of the checksum used in Algorand addresses (4)
485+
ChecksumLength,
489486

490-
#[ffi_func]
491-
pub fn get_algorand_address_length() -> u64 {
492-
constants::ALGORAND_ADDRESS_LENGTH.try_into().unwrap()
493-
}
487+
/// Length of a base32-encoded Algorand address (58)
488+
AddressLength,
494489

495-
#[ffi_func]
496-
pub fn get_algorand_public_key_byte_length() -> u64 {
497-
constants::ALGORAND_PUBLIC_KEY_BYTE_LENGTH
498-
.try_into()
499-
.unwrap()
500-
}
490+
/// Length of an Algorand public key in bytes (32)
491+
PublicKeyLength,
501492

502-
#[ffi_func]
503-
pub fn get_algorand_secret_key_byte_length() -> u64 {
504-
constants::ALGORAND_SECRET_KEY_BYTE_LENGTH
505-
.try_into()
506-
.unwrap()
493+
/// Length of an Algorand secret key in bytes (32)
494+
SecretKeyLength,
495+
496+
/// Length of an Algorand signature in bytes (64)
497+
SignatureLength,
498+
499+
/// Increment in the encoded byte size when a signature is attached to a transaction (75)
500+
SignatureEncodingIncrLength,
507501
}
508502

509-
#[ffi_func]
510-
pub fn get_algorand_signature_byte_length() -> u64 {
511-
constants::ALGORAND_SIGNATURE_BYTE_LENGTH
512-
.try_into()
513-
.unwrap()
503+
impl AlgorandConstant {
504+
/// Get the numeric value of the constant
505+
pub fn value(&self) -> u64 {
506+
match self {
507+
AlgorandConstant::HashLength => HASH_BYTES_LENGTH as u64,
508+
AlgorandConstant::ChecksumLength => ALGORAND_CHECKSUM_BYTE_LENGTH as u64,
509+
AlgorandConstant::AddressLength => ALGORAND_ADDRESS_LENGTH as u64,
510+
AlgorandConstant::PublicKeyLength => ALGORAND_PUBLIC_KEY_BYTE_LENGTH as u64,
511+
AlgorandConstant::SecretKeyLength => ALGORAND_SECRET_KEY_BYTE_LENGTH as u64,
512+
AlgorandConstant::SignatureLength => ALGORAND_SIGNATURE_BYTE_LENGTH as u64,
513+
AlgorandConstant::SignatureEncodingIncrLength => {
514+
ALGORAND_SIGNATURE_ENCODING_INCR as u64
515+
}
516+
}
517+
}
514518
}
515519

516520
#[ffi_func]
517-
pub fn get_algorand_signature_encoding_incr() -> u64 {
518-
constants::ALGORAND_SIGNATURE_ENCODING_INCR
519-
.try_into()
520-
.unwrap()
521+
pub fn get_algorand_constant(constant: AlgorandConstant) -> u64 {
522+
constant.value()
521523
}
522524

523525
#[cfg(test)]

crates/ffi_macros/src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use convert_case::{Case, Casing};
44
use proc_macro::TokenStream;
55
use quote::{quote, ToTokens};
6-
use syn::{parse_macro_input, Field, Fields, ItemFn, ItemStruct, Type, TypePath};
6+
use syn::{parse_macro_input, Field, Fields, ItemEnum, ItemFn, ItemStruct, Type, TypePath};
77

88
#[proc_macro_attribute]
99
pub fn ffi_func(_attr: TokenStream, item: TokenStream) -> TokenStream {
@@ -56,6 +56,30 @@ pub fn ffi_record(_attr: TokenStream, item: TokenStream) -> TokenStream {
5656
combined.into()
5757
}
5858

59+
#[proc_macro_attribute]
60+
pub fn ffi_enum(_attr: TokenStream, item: TokenStream) -> TokenStream {
61+
let input = parse_macro_input!(item as ItemEnum);
62+
63+
let enum_attrs = quote! {
64+
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
65+
#[cfg_attr(feature = "ffi_wasm", derive(Tsify))]
66+
#[cfg_attr(
67+
feature = "ffi_wasm",
68+
tsify(into_wasm_abi, from_wasm_abi, large_number_types_as_bigints)
69+
)]
70+
#[cfg_attr(feature = "ffi_wasm", serde(rename_all = "camelCase"))]
71+
#[cfg_attr(feature = "ffi_uniffi", derive(uniffi::Enum))]
72+
};
73+
74+
// Combine original attributes with new ones
75+
let combined = quote! {
76+
#enum_attrs
77+
#input
78+
};
79+
80+
combined.into()
81+
}
82+
5983
fn is_option_type(field: &Field) -> bool {
6084
if let Type::Path(TypePath { path, .. }) = &field.ty {
6185
if let Some(segment) = path.segments.last() {

0 commit comments

Comments
 (0)