Skip to content

Commit

Permalink
serde_ipld_dagcbor crate NIF wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mekaem committed Feb 16, 2024
1 parent b787641 commit 5b8ab6f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/hexpds/dagcbor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Hexdps.DagCBOR do
defmodule Internal do
use Rustler, otp_app: :hexpds, crate: "hexpds_dagcbor_internal"
@spec encode_dag_cbor(binary()) :: {:ok, binary()} | {:error, String.t()}
def encode_dag_cbor(_json), do: :erlang.nif_error(:nif_not_loaded)
end
end
5 changes: 5 additions & 0 deletions native/hexpds_dagcbor_internal/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.'cfg(target_os = "macos")']
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
14 changes: 14 additions & 0 deletions native/hexpds_dagcbor_internal/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
15 changes: 15 additions & 0 deletions native/hexpds_dagcbor_internal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "hexpds_dagcbor_internal"
version = "0.1.0"
authors = [ "NetWatchInc" ]
edition = "2021"

[lib]
name = "hexpds_dagcbor_internal"
path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
rustler = "0.31.0"
serde_ipld_dagcbor = "0.4.2"
serde_json = "1.0.113"
20 changes: 20 additions & 0 deletions native/hexpds_dagcbor_internal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# NIF for Elixir.Hexpds.DagCBOR.Internal

## To build the NIF module:

- Your NIF will now build along with your project.

## To load the NIF:

```elixir
defmodule Hexpds.DagCBOR.Internal do
use Rustler, otp_app: :hexpds, crate: "hexpds_dagcbor_internal"

# When your NIF is loaded, it will override this function.
def encode_dag_cbor(json), do: :erlang.nif_error(:nif_not_loaded)
end
```

## Examples

[This](https://github.com/rusterlium/NifIo) is a complete example of a NIF written in Rust.
29 changes: 29 additions & 0 deletions native/hexpds_dagcbor_internal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use rustler::Encoder;
use rustler::Env;
use rustler::Term;
use serde_json::from_str;
use serde_ipld_dagcbor::to_vec;

mod atoms {
rustler::atoms! {
ok,
error,
}
}

#[rustler::nif]
fn encode_dag_cbor(env: Env, json: String) -> Term {
let parsed_json = match from_str(&json) {
Ok(json) => json,
Err(e) => return (atoms::error(), format!("Failed to parse JSON: {}", e)).encode(env),
};

let cbor_data = match to_vec(&parsed_json) {
Ok(data) => data,
Err(e) => return (atoms::error(), format!("Failed to encode to DAG-CBOR: {}", e)).encode(env),
};

(atoms::ok(), cbor_data).encode(env)
}

rustler::init!("Elixir.Hexpds.DagCBOR.Internal", [encode_dag_cbor]);

0 comments on commit 5b8ab6f

Please sign in to comment.