1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use errors;
fn to_utf16_vec<'a>(input: &'a str) -> Vec<i8> {
input.encode_utf16().chain(vec![0]).flat_map(|chr| vec![(chr % 256) as i8, (chr / 256) as i8]).collect()
}
#[derive(Debug, Clone)]
pub struct SapString(Vec<i8>);
impl From<Vec<i8>> for SapString {
fn from(data: Vec<i8>) -> Self {
SapString(data)
}
}
impl<'a> From<&'a [i8]> for SapString {
fn from(data: &'a [i8]) -> Self {
SapString(Vec::from(data))
}
}
impl<'a> From<&'a SapString> for SapString {
fn from(input: &'a SapString) -> Self {
input.clone()
}
}
impl<'a> From<&'a String> for SapString {
fn from(input: &'a String) -> Self {
SapString(to_utf16_vec(input))
}
}
impl From<String> for SapString {
fn from(input: String) -> Self {
SapString(to_utf16_vec(&input))
}
}
impl<'a> From<&'a str> for SapString {
fn from(input: &'a str) -> Self {
SapString(to_utf16_vec(input))
}
}
impl SapString {
pub fn as_ptr(&self) -> *const i8 {
self.0.as_ptr()
}
pub fn as_string(&self) -> errors::Result<String> {
let (_, utf16) = self.0.iter().fold(
(None, Vec::with_capacity(self.0.len() / 2)),
|mut acc: (Option<i8>, Vec<u16>), &chr| {
match acc.0 {
None => (Some(chr), acc.1),
Some(last) => {
acc.1.push((chr as u16) * 256 + (last as u16));
(None, acc.1)
}
}
}
);
Ok(String::from_utf16(utf16.as_slice())?.trim_right_matches('\0').into())
}
}