Skip to content

Commit

Permalink
chore: ffi prelude to avoid dupe types, makefile entries for bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Jul 5, 2024
1 parent 149f5d5 commit fc7e9cf
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/target
expanded.rs
stealth_address_kit.h
stealth_address_kit.nim
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.PHONY: deps clean example
deps:
@cargo install cross --git https://github.com/cross-rs/cross.git --rev 1511a28
@cargo install cbindgen

clean:
@cargo clean
Expand All @@ -11,3 +12,11 @@ example:
bench:
@cargo bench --all-features
cp -r target/criterion/** benchmarks/

generate_c_bindings:
@cargo expand --all-features -p stealth_address_kit > expanded.rs
@cbindgen --output stealth_address_kit.h --lang c expanded.rs

generate_nim_bindings:
@cargo expand --all-features -p stealth_address_kit > expanded.rs
@nbindgen --output stealth_address_kit.nim expanded.rs
29 changes: 1 addition & 28 deletions sdk/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,8 @@ macro_rules! define_curve_ffi {
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
use num_traits::Zero;
use std::ops::Add;
use crate::ffi_prelude::{CReturn, CErrorCode};

#[repr(C)]
#[derive(Debug, PartialOrd, PartialEq)]
pub enum CErrorCode {
NoError = 0,
SerializationErrorNotEnoughSpace = 1,
SerializationErrorInvalidData = 2,
SerializationErrorUnexpectedFlags = 3,
SerializationErrorIoError = 4,
InvalidKeys = 5,
}

impl From<SerializationError> for CErrorCode {
fn from(value: SerializationError) -> Self {
match value {
SerializationError::NotEnoughSpace => CErrorCode::SerializationErrorNotEnoughSpace,
SerializationError::InvalidData => CErrorCode::SerializationErrorInvalidData,
SerializationError::UnexpectedFlags => CErrorCode::SerializationErrorUnexpectedFlags,
SerializationError::IoError(_) => CErrorCode::SerializationErrorIoError,
}
}
}

#[repr(C)]
#[derive(Debug)]
pub struct CReturn<T> {
value: T,
err_code: CErrorCode,
}
paste! {
#[repr(C)]
#[derive(Debug)]
Expand Down
30 changes: 30 additions & 0 deletions sdk/src/ffi_prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use ark_serialize::SerializationError;

#[repr(C)]
#[derive(Debug, PartialOrd, PartialEq)]
pub enum CErrorCode {
NoError = 0,
SerializationErrorNotEnoughSpace = 1,
SerializationErrorInvalidData = 2,
SerializationErrorUnexpectedFlags = 3,
SerializationErrorIoError = 4,
InvalidKeys = 5,
}

impl From<SerializationError> for CErrorCode {
fn from(value: SerializationError) -> Self {
match value {
SerializationError::NotEnoughSpace => CErrorCode::SerializationErrorNotEnoughSpace,
SerializationError::InvalidData => CErrorCode::SerializationErrorInvalidData,
SerializationError::UnexpectedFlags => CErrorCode::SerializationErrorUnexpectedFlags,
SerializationError::IoError(_) => CErrorCode::SerializationErrorIoError,
}
}
}

#[repr(C)]
#[derive(Debug)]
pub struct CReturn<T> {
pub(crate) value: T,
pub(crate) err_code: CErrorCode,
}
3 changes: 3 additions & 0 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ mod vesta_impl;
#[cfg(feature = "ffi")]
#[cfg_attr(docsrs, doc(cfg(feature = "ffi")))]
mod ffi;
#[cfg(feature = "ffi")]
#[cfg_attr(docsrs, doc(cfg(feature = "ffi")))]
mod ffi_prelude;

#[cfg(feature = "baby_jub_jub")]
#[cfg_attr(docsrs, doc(cfg(feature = "baby_jub_jub")))]
Expand Down

0 comments on commit fc7e9cf

Please sign in to comment.