-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
serde_ipld_dagcbor crate NIF wrapper
- Loading branch information
Showing
6 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |