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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::Exceptions;
use super::{high_level_encoder, Encoder, EncoderContext};
pub struct EdifactEncoder;
impl Encoder for EdifactEncoder {
fn getEncodingMode(&self) -> usize {
high_level_encoder::EDIFACT_ENCODATION
}
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> {
let mut buffer = String::new();
while context.hasMoreCharacters() {
let c = context.getCurrentChar();
Self::encodeChar(c, &mut buffer);
context.pos += 1;
let count = buffer.chars().count();
if count >= 4 {
context.writeCodewords(&Self::encodeToCodewords(&buffer)?);
buffer.replace_range(0..4, "");
let newMode = high_level_encoder::lookAheadTest(
context.getMessage(),
context.pos,
self.getEncodingMode() as u32,
);
if newMode != self.getEncodingMode() {
context.signalEncoderChange(high_level_encoder::ASCII_ENCODATION);
break;
}
}
}
buffer.push(31 as char); Self::handleEOD(context, &mut buffer)?;
Ok(())
}
}
impl EdifactEncoder {
pub fn new() -> Self {
Self
}
fn handleEOD(context: &mut EncoderContext, buffer: &mut String) -> Result<(), Exceptions> {
let mut runner = || -> Result<(), Exceptions> {
let count = buffer.chars().count();
if count == 0 {
return Ok(()); }
if count == 1 {
context.updateSymbolInfo();
let mut available = context.getSymbolInfo().unwrap().getDataCapacity()
- context.getCodewordCount() as u32;
let remaining = context.getRemainingCharacters();
if remaining > available {
context.updateSymbolInfoWithLength(context.getCodewordCount() + 1);
available = context.getSymbolInfo().unwrap().getDataCapacity()
- context.getCodewordCount() as u32;
}
if remaining <= available && available <= 2 {
return Ok(()); }
}
if count > 4 {
return Err(Exceptions::IllegalStateException(
"Count must not exceed 4".to_owned(),
));
}
let restChars = count - 1;
let encoded = Self::encodeToCodewords(buffer)?;
let endOfSymbolReached = !context.hasMoreCharacters();
let mut restInAscii = endOfSymbolReached && restChars <= 2;
if restChars <= 2 {
context.updateSymbolInfoWithLength(context.getCodewordCount() + restChars);
let available = context.getSymbolInfo().unwrap().getDataCapacity()
- context.getCodewordCount() as u32;
if available >= 3 {
restInAscii = false;
context.updateSymbolInfoWithLength(
context.getCodewordCount() + encoded.chars().count(),
);
}
}
if restInAscii {
context.resetSymbolInfo();
context.pos -= restChars as u32;
} else {
context.writeCodewords(&encoded);
}
Ok(())
};
let res = runner();
context.signalEncoderChange(high_level_encoder::ASCII_ENCODATION);
res
}
fn encodeChar(c: char, sb: &mut String) {
if c >= ' ' && c <= '?' {
sb.push(c);
} else if c >= '@' && c <= '^' {
sb.push((c as u8 - 64) as char);
} else {
high_level_encoder::illegalCharacter(c).expect("");
}
}
fn encodeToCodewords(sb: &str) -> Result<String, Exceptions> {
let len = sb.chars().count();
if len == 0 {
return Err(Exceptions::IllegalStateException(
"StringBuilder must not be empty".to_owned(),
));
}
let c1 = sb.chars().nth(0).unwrap();
let c2 = if len >= 2 {
sb.chars().nth(1).unwrap()
} else {
0 as char
};
let c3 = if len >= 3 {
sb.chars().nth(2).unwrap()
} else {
0 as char
};
let c4 = if len >= 4 {
sb.chars().nth(3).unwrap()
} else {
0 as char
};
let v: u32 = ((c1 as u32) << 18) + ((c2 as u32) << 12) + ((c3 as u32) << 6) + c4 as u32;
let cw1 = (v as u32 >> 16) & 255;
let cw2 = (v as u32 >> 8) & 255;
let cw3 = v as u32 & 255;
let mut res = String::with_capacity(3);
res.push(char::from_u32(cw1).unwrap());
if len >= 2 {
res.push(char::from_u32(cw2).unwrap());
}
if len >= 3 {
res.push(char::from_u32(cw3).unwrap());
}
Ok(res)
}
}