Skip to content

Commit

Permalink
Merge pull request #3 from Q1-Energie-AG/rustler_0_22
Browse files Browse the repository at this point in the history
  • Loading branch information
moogle19 authored Oct 9, 2021
2 parents dcdf041 + ca63c4b commit 6991fb6
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 138 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ erl_crash.dump
tds_encoding-*.tar

/priv/native/

/native/*/target
7 changes: 5 additions & 2 deletions lib/tds_encoding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ defmodule Tds.Encoding do
iex> Tds.Encoding.encode("¥₪ש", "windows-1255")
<<0xA5, 0xA4, 0xF9>>
iex> Tds.Encoding.decode(<<0xA5, 0xA4, 0xF9>>, "windows-15")
iex> Tds.Encoding.decode(<<0xA5, 0xA4, 0xF9>>, "windows-1255")
"¥₪ש"
"""
use Rustler, otp_app: :tds_encoding, crate: "tds_encoding"
use Rustler,
otp_app: :tds_encoding,
crate: "tds_encoding",
mode: if Mix.env() == :prod, do: :release, else: :debug

@doc """
Encodes utf-8 string using given codepage. If there are any unknown codes they
Expand Down
9 changes: 2 additions & 7 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ defmodule Tds.Encoding.MixProject do
def project do
[
app: :tds_encoding,
compilers: [:rustler] ++ Mix.compilers(),
compilers: Mix.compilers(),
version: "1.0.0",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
deps: deps(),
rustler_crates: [
tds_encoding: [
mode: (if Mix.env() == :prod, do: :release, else: :debug)
]
],
description: "String encoding/decoding NIF using rust [encoding](https://crates.io/crates/encoding) library",
package: package(),
]
Expand All @@ -29,7 +24,7 @@ defmodule Tds.Encoding.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:rustler, "~> 0.21.0"},
{:rustler, "~> 0.22.0"},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
]
end
Expand Down
16 changes: 9 additions & 7 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
%{
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm"},
"rustler": {:hex, :rustler, "0.21.0", "68cc4fc015d0b9541865ea78e78e9ef2dd91ee4be80bf543fd15791102a45aca", [:mix], [{:toml, "~> 0.5.2", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm"},
"toml": {:hex, :toml, "0.5.2", "e471388a8726d1ce51a6b32f864b8228a1eb8edc907a0edf2bb50eab9321b526", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"},
"earmark_parser": {:hex, :earmark_parser, "1.4.15", "b29e8e729f4aa4a00436580dcc2c9c5c51890613457c193cc8525c388ccb2f06", [:mix], [], "hexpm", "044523d6438ea19c1b8ec877ec221b008661d3c27e3b848f4c879f500421ca5c"},
"ex_doc": {:hex, :ex_doc, "0.25.3", "3edf6a0d70a39d2eafde030b8895501b1c93692effcbd21347296c18e47618ce", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "9ebebc2169ec732a38e9e779fd0418c9189b3ca93f4a676c961be6c1527913f5"},
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
"rustler": {:hex, :rustler, "0.22.0", "e2930f9d6933e910f87526bb0a7f904e32b62a7e838a3ca4a884ee7fdfb957ed", [:mix], [{:toml, "~> 0.5.2", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "01f5989dd511ebec09be481e07d3c59773d5373c5061e09d3ebc3ef61811b49d"},
"toml": {:hex, :toml, "0.5.2", "e471388a8726d1ce51a6b32f864b8228a1eb8edc907a0edf2bb50eab9321b526", [:mix], [], "hexpm", "f1e3dabef71fb510d015fad18c0e05e7c57281001141504c6b69d94e99750a07"},
}
6 changes: 6 additions & 0 deletions native/tds_encoding/.cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]

[target.aarch64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]
100 changes: 18 additions & 82 deletions native/tds_encoding/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions native/tds_encoding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tds_encoding"
version = "0.1.0"
version = "0.2.0"
authors = []

[lib]
Expand All @@ -9,7 +9,7 @@ path = "src/lib.rs"
crate-type = ["dylib"]

[dependencies]
rustler = "0.21.0"
rustler_codegen = "0.21.0"
rustler = "0.22.0"
rustler_codegen = "0.22.0"
lazy_static = "1.4"
encoding = "0.2.33"
61 changes: 24 additions & 37 deletions native/tds_encoding/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,53 @@
#[macro_use]
extern crate rustler;
// #[macro_use]
extern crate rustler_codegen;
// #[macro_use]
extern crate lazy_static;
extern crate encoding;
extern crate lazy_static;


use encoding::{DecoderTrap, EncoderTrap};
use encoding::label::encoding_from_whatwg_label;
use encoding::{DecoderTrap, EncoderTrap};

use rustler::{Env, Term, NifResult, Encoder};
use rustler::types::binary::{ Binary, OwnedBinary };
use rustler::{Error};
use rustler::types::binary::{Binary, OwnedBinary};
use rustler::Error;
use rustler::{Encoder, Env, NifResult, Term};

use std::io::Write;


mod atoms {
rustler_atoms! {
atom ok;
atom error;
atom unknown_encoding;
atoms! {
ok,
error,
unknown_encoding,
//atom __true__ = "true";
//atom __false__ = "false";
}
}

rustler_export_nifs! {
"Elixir.Tds.Encoding",
[
("encode", 2, encode),
("decode", 2, decode)
],
None
}

fn decode<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult<Term<'a>> {
let enc : String = args[1].decode()?;
rustler::init!("Elixir.Tds.Encoding", [encode, decode]);

#[rustler::nif]
fn decode<'a>(env: Env<'a>, in_binary: Binary, enc: String) -> NifResult<Term<'a>> {
match encoding_from_whatwg_label(&enc) {
Some(encoding) => {
let in_binary : Binary = args[0].decode()?;
let in_str = in_binary.to_owned().unwrap();
let res = encoding.decode(in_str.as_slice(), DecoderTrap::Replace).unwrap();
return Ok(res.encode(env))
},
None => return Err(Error::BadArg),
let res = encoding
.decode(in_str.as_slice(), DecoderTrap::Replace)
.unwrap();
Ok(res.encode(env))
}
None => Err(Error::BadArg)
}
}

fn encode<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult<Term<'a>> {
let enc : String = args[1].decode()?;

#[rustler::nif]
fn encode<'a>(env: Env<'a>, in_str: &str, enc: String) -> NifResult<Term<'a>> {
match encoding_from_whatwg_label(&enc) {
Some(encoding) => {
let in_str : &str = args[0].decode()?;
let enc_bin = encoding.encode(in_str, EncoderTrap::Replace).unwrap();
let mut bin = OwnedBinary::new(enc_bin.len()).unwrap();
bin.as_mut_slice().write(&enc_bin).unwrap();
return Ok(bin.release(env).encode(env))
},
None => return Err(Error::BadArg),
bin.as_mut_slice().write_all(&enc_bin).unwrap();
Ok(bin.release(env).encode(env))
}
None => Err(Error::BadArg)
}
}

0 comments on commit 6991fb6

Please sign in to comment.