Packages
tds
1.2.3
2.3.8
2.3.7
2.3.6
2.3.5
2.3.4
2.3.2
2.3.1
2.3.0
2.3.0-rc.1
2.3.0-rc.0
2.2.0
2.1.3
2.1.2
2.1.1
2.1.0
2.0.4
2.0.3
2.0.2
2.0.1
2.0.1-rc2
2.0.1-rc1
2.0.0
retired
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.0
1.0.19
1.0.18
1.0.17
1.0.16
1.0.15
1.0.14
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.0
0.3.0
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Microsoft SQL Server client (Elixir implementation of the MS TDS protocol)
Current section
Files
Jump to
Current section
Files
native/tds_encoding/src/lib.rs
#[macro_use]
extern crate rustler;
// #[macro_use]
extern crate rustler_codegen;
// #[macro_use]
extern crate lazy_static;
extern crate encoding;
use encoding::{DecoderTrap, EncoderTrap};
use encoding::label::encoding_from_whatwg_label;
use rustler::{Env, Term, NifResult, Encoder};
use rustler::types::binary::{ Binary, OwnedBinary };
use rustler::{Error};
use std::io::Write;
mod atoms {
rustler_atoms! {
atom ok;
atom error;
atom 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()?;
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::Ignore).unwrap();
return Ok(res.encode(env))
},
None => return Err(Error::BadArg),
}
}
fn encode<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult<Term<'a>> {
let enc : String = args[1].decode()?;
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),
}
}