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

python ffi creation #59

Merged
merged 1 commit into from
Feb 4, 2023
Merged

Conversation

jasoncolburne
Copy link
Collaborator

@jasoncolburne jasoncolburne commented Feb 3, 2023

Rationale

Wanted to set us up for success. I created the beginnings of the Rust API layer and the Python FFI. Comments welcome.

Also, this unlocks parallel streams of work and tasks with a lower bar to entry for the project.

Changes

  • Exposes Rust API and maintains standard crate configuration.
  • Creates Python API with a simple Matter class exposed.
  • Adds a Makefile and some targets for easier scripting (abusing make a bit but it's available everywhere)
  • Updates README.md with instructions for new developers.

I am pretty happy with how I cleaned up the directory structure after a few iterations and separated the python code from the main library. The only changes to the core library are to support a simple #[pyclass] above the definition of the struct.

Testing

You are encouraged to check out the branch and try these commands.

> make clean libs preflight python-shell
Python 3.10.6 (main, Aug 11 2022, 13:49:25) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from cesride import Matter
>>> m = Matter(qb64="BGlOiUdp5sMmfotHfCWQKEzWR91C72AH0lT84c0um-Qj")
>>> qb2 = m.qb2()
>>> print(qb2)
[4, 105, 78, 137, 71, 105, 230, 195, 38, 126, 139, 71, 124, 37, 144, 40, 76, 214, 71, 221, 66, 239, 96, 7, 210, 84, 252, 225, 205, 46, 155, 228, 35]
>>> m2 = Matter(qb2=bytes(qb2))
>>> m2.qb64()
'BGlOiUdp5sMmfotHfCWQKEzWR91C72AH0lT84c0um-Qj'
>>> m3 = Matter(code=m.code(), raw=bytes(m.raw()), raw_size=len(m.raw()))
>>> m3.qb64()
'BGlOiUdp5sMmfotHfCWQKEzWR91C72AH0lT84c0um-Qj'
>>> Matter(qb64="_" * 44)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: unexpected code error: op code start
>>> Matter(qb64="-" * 44)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Exception: unexpected code error: count code start

Screenshots (updated README.md)

Screenshot 2023-02-04 at 1 17 41 PM

Here is a live link to a preview of this file.

@codecov
Copy link

codecov bot commented Feb 3, 2023

Codecov Report

Merging #59 (7b93440) into main (a3beb9a) will increase coverage by 0.10%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main      #59      +/-   ##
==========================================
+ Coverage   97.72%   97.83%   +0.10%     
==========================================
  Files           9        9              
  Lines         969      968       -1     
==========================================
  Hits          947      947              
+ Misses         22       21       -1     
Impacted Files Coverage Δ
src/core/counter/mod.rs 95.55% <ø> (ø)
src/core/diger.rs 100.00% <ø> (ø)
src/core/matter/mod.rs 95.35% <ø> (+0.40%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more


python:
@head -n $(LINE) Cargo.toml > cargo/$@/Cargo.toml
@echo "pyo3 = { version = \"0.18.0\", features = [\"abi3\", \"extension-module\"] }" >> cargo/$@/Cargo.toml
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was pretty gross but I was in a rush.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to use a toml parser or something to edit the section and re-export.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, template everything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah add an issue so we track it I think it's fine for now

Comment on lines 35 to 48
let result = if let Some(code) = code {
let (raw, raw_size) = if raw.is_none() || raw_size.is_none() {
return Err(PyValueError::new_err("code present, raw and raw_size missing"));
} else {
(raw.unwrap(), raw_size.unwrap())
};

Self::new_with_code_and_raw(code, raw, raw_size)
} else if let Some(qb64) = qb64 {
Self::new_with_qb64(qb64)
} else if let Some(qb64b) = qb64b {
Self::new_with_qb64b(qb64b)
} else if let Some(qb2) = qb2 {
Self::new_with_qb2(qb2)
} else {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone should add validation if this gets merged, that multiple things aren't set. Example: qb2 and qb64 both specified = bad.

Copy link
Collaborator Author

@jasoncolburne jasoncolburne Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is only outdated because code moved, we should still do this.

src/lib_python.rs Outdated Show resolved Hide resolved
@jasoncolburne jasoncolburne changed the title ffi spike python ffi creation Feb 4, 2023
@jasoncolburne jasoncolburne force-pushed the ffideas branch 3 times, most recently from 578062b to eb6efd2 Compare February 4, 2023 15:14

use crate::core::util;
use crate::error::{err, Error, Result};

pub mod tables;

#[cfg_attr(feature = "python", pyclass)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just this line in the core code, and the required gated-use.

@jasoncolburne jasoncolburne force-pushed the ffideas branch 2 times, most recently from 132f2b4 to c8496d6 Compare February 4, 2023 15:54
@jasoncolburne jasoncolburne marked this pull request as ready for review February 4, 2023 15:59
@jasoncolburne jasoncolburne requested a review from m00sey as a code owner February 4, 2023 15:59
@jasoncolburne jasoncolburne force-pushed the ffideas branch 3 times, most recently from 221c1f7 to 321caad Compare February 4, 2023 17:16
@jasoncolburne
Copy link
Collaborator Author

Side note: the tarpaulin action occasionally fails, we should look at using a make or shell command rather than the action.

Copy link
Member

@m00sey m00sey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

cargo fix
cargo fmt

preflight:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been wanting to do this !!

pub mod cigar;
pub mod counter;
pub mod diger;
pub mod matter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Matter want to be public? I wouldn't expect people to be initializing it as it's a base for the others?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly not, but in the Rust syntax we'd probably need a type because using a trait you initialize like this:

let verfer = <Matter as Verfer>::new_with_code_and_raw(/* ... */);

.. we may be able to alias all these? I can look into this in another issue.


python:
@head -n $(LINE) Cargo.toml > cargo/$@/Cargo.toml
@echo "pyo3 = { version = \"0.18.0\", features = [\"abi3\", \"extension-module\"] }" >> cargo/$@/Cargo.toml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah add an issue so we track it I think it's fine for now

@jasoncolburne jasoncolburne merged commit 817fda3 into WebOfTrust:main Feb 4, 2023
@jasoncolburne jasoncolburne deleted the ffideas branch February 4, 2023 20:16
@Artemkaaas
Copy link
Contributor

@jasoncolburne I would consider usage of uniffi Rust crate for FFI bindings.
It can generate bindings for multiple languages at once (Kotlin, Swift, Python, Ruby).
You may have the following project structure:

  • cesride-rs rust package published to crates.io
  • cesride crate depending on cesride-rs which provides system library and FFI bindings with using uniffi.

@jasoncolburne jasoncolburne mentioned this pull request Feb 6, 2023
@jasoncolburne
Copy link
Collaborator Author

@Artemkaaas Thanks for this, it sounds less hacky than what I threw together! Someone mentioned it recently but it went past me and I didn't really know what it offered. I created a ticket for investigation: #65

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants