diff --git a/.gitignore b/.gitignore index 31c2bf1..65e5eb0 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ erl_crash.dump tds_encoding-*.tar /priv/native/ + +/native/*/target diff --git a/lib/tds_encoding.ex b/lib/tds_encoding.ex index 762e86f..ed0d87f 100644 --- a/lib/tds_encoding.ex +++ b/lib/tds_encoding.ex @@ -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 diff --git a/mix.exs b/mix.exs index d369a7b..6c88ac6 100644 --- a/mix.exs +++ b/mix.exs @@ -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(), ] @@ -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 diff --git a/mix.lock b/mix.lock index f59f9a9..63353d7 100644 --- a/mix.lock +++ b/mix.lock @@ -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"}, } diff --git a/native/tds_encoding/.cargo/config b/native/tds_encoding/.cargo/config index a237fd1..1564217 100644 --- a/native/tds_encoding/.cargo/config +++ b/native/tds_encoding/.cargo/config @@ -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", ] \ No newline at end of file diff --git a/native/tds_encoding/Cargo.lock b/native/tds_encoding/Cargo.lock index 43f545f..8bdd186 100644 --- a/native/tds_encoding/Cargo.lock +++ b/native/tds_encoding/Cargo.lock @@ -1,38 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "backtrace" -version = "0.3.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f80256bc78f67e7df7e36d77366f636ed976895d91fe2ab9efa3973e8fe8c4f" -dependencies = [ - "backtrace-sys", - "cfg-if", - "libc", - "rustc-demangle", -] - -[[package]] -name = "backtrace-sys" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d6575f128516de27e3ce99689419835fce9643a9b215a14d2b5b685be018491" -dependencies = [ - "cc", - "libc", -] - -[[package]] -name = "cc" -version = "1.0.50" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" - -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +version = 3 [[package]] name = "encoding" @@ -98,15 +66,6 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" -[[package]] -name = "failure" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8273f13c977665c5db7eb2b99ae520952fe5ac831ae4cd09d80c4c7042b5ed9" -dependencies = [ - "backtrace", -] - [[package]] name = "heck" version = "0.3.1" @@ -122,53 +81,40 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -[[package]] -name = "libc" -version = "0.2.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" - [[package]] name = "proc-macro2" -version = "0.4.30" +version = "1.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +checksum = "b9f5105d4fdaab20335ca9565e106a5d9b82b6219b5ba735731124ac6711d23d" dependencies = [ "unicode-xid", ] [[package]] name = "quote" -version = "0.6.13" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" dependencies = [ "proc-macro2", ] -[[package]] -name = "rustc-demangle" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" - [[package]] name = "rustler" -version = "0.21.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "087d812084e88e84d09ad05abebb24e2967b287ddfa692296f742bcf3e403f93" +checksum = "b787d3b2a80007f41cd4c0c310cdeb3936192768159585f65ecc7e96faf97fc3" dependencies = [ "lazy_static", "rustler_codegen", "rustler_sys", - "which", ] [[package]] name = "rustler_codegen" -version = "0.21.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62d5150810c3a06997d644cacfd0d9464441aae1d46e0bdf66cef67d379fa443" +checksum = "b5a1f867002b6f0130f47abf215cac4405646db6f5d7b009b21c890980490aa4" dependencies = [ "heck", "proc-macro2", @@ -178,18 +124,18 @@ dependencies = [ [[package]] name = "rustler_sys" -version = "2.0.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e2c6dd78a5cffd4294c4aca568522736b8a67f0e8ccdb6a55354dc933726bc4" +checksum = "1cb382fde4f421c51555919e9920b058c0286f6bf59e53d02eb4d281eae6758b" dependencies = [ "unreachable", ] [[package]] name = "syn" -version = "0.15.44" +version = "1.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +checksum = "5239bc68e0fef57495900cfea4e8dc75596d9a319d7e16b1e0a440d24e6fe0a0" dependencies = [ "proc-macro2", "quote", @@ -198,7 +144,7 @@ dependencies = [ [[package]] name = "tds_encoding" -version = "0.1.0" +version = "0.2.0" dependencies = [ "encoding", "lazy_static", @@ -214,15 +160,15 @@ checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" [[package]] name = "unicode-xid" -version = "0.1.0" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" [[package]] name = "unreachable" -version = "0.1.1" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" +checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" dependencies = [ "void", ] @@ -232,13 +178,3 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - -[[package]] -name = "which" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b57acb10231b9493c8472b20cb57317d0679a49e0bdbee44b3b803a6473af164" -dependencies = [ - "failure", - "libc", -] diff --git a/native/tds_encoding/Cargo.toml b/native/tds_encoding/Cargo.toml index f2b9eef..f1c1859 100644 --- a/native/tds_encoding/Cargo.toml +++ b/native/tds_encoding/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tds_encoding" -version = "0.1.0" +version = "0.2.0" authors = [] [lib] @@ -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" \ No newline at end of file diff --git a/native/tds_encoding/src/lib.rs b/native/tds_encoding/src/lib.rs index 065d401..fee0855 100644 --- a/native/tds_encoding/src/lib.rs +++ b/native/tds_encoding/src/lib.rs @@ -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> { - 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> { 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> { - let enc : String = args[1].decode()?; - +#[rustler::nif] +fn encode<'a>(env: Env<'a>, in_str: &str, enc: String) -> NifResult> { 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) } }