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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! Encoding detection and validation for MARC records.
//!
//! This module provides tools for detecting and validating character encodings
//! in MARC records, including support for mixed-encoding records and encoding
//! consistency checks.
use crate::encoding::MarcEncoding;
use crate::error::{MarcError, Result};
use crate::record::Record;
/// Result of encoding validation analysis
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EncodingAnalysis {
/// Record uses a single consistent encoding
Consistent(MarcEncoding),
/// Record appears to have mixed encodings
Mixed {
/// Primary encoding (from leader)
primary: MarcEncoding,
/// Secondary encodings detected in data
secondary: Vec<MarcEncoding>,
/// Number of fields with inconsistent encoding
field_count: usize,
},
/// Unable to determine encoding from data
Undetermined,
}
/// Validator for MARC record encodings
#[derive(Debug)]
pub struct EncodingValidator;
impl EncodingValidator {
/// Analyze the encoding of a MARC record
///
/// Attempts to detect if the record uses mixed encodings by examining
/// field data and comparing it to the declared encoding in the leader.
///
/// # Errors
///
/// Returns an error if encoding analysis fails.
pub fn analyze_encoding(record: &Record) -> Result<EncodingAnalysis> {
let primary_encoding = MarcEncoding::from_leader_char(record.leader.character_coding)?;
let mut mixed_encodings = Vec::new();
let mut inconsistent_field_count = 0usize;
// Check control fields
for values in record.control_fields.values() {
for value in values {
if Self::is_likely_different_encoding(value, primary_encoding) {
inconsistent_field_count += 1;
let detected = Self::detect_encoding_from_string(value);
if let Some(enc) = detected {
if enc != primary_encoding && !mixed_encodings.contains(&enc) {
mixed_encodings.push(enc);
}
}
}
}
}
// Check data fields and subfields
for fields in record.fields.values() {
for field in fields {
for subfield in &field.subfields {
if Self::is_likely_different_encoding(&subfield.value, primary_encoding) {
inconsistent_field_count += 1;
let detected = Self::detect_encoding_from_string(&subfield.value);
if let Some(enc) = detected {
if enc != primary_encoding && !mixed_encodings.contains(&enc) {
mixed_encodings.push(enc);
}
}
}
}
}
}
if mixed_encodings.is_empty() {
Ok(EncodingAnalysis::Consistent(primary_encoding))
} else {
Ok(EncodingAnalysis::Mixed {
primary: primary_encoding,
secondary: mixed_encodings,
field_count: inconsistent_field_count,
})
}
}
/// Check if a field's data is consistent with the expected encoding
///
/// Returns true if the field appears to use a different encoding than expected.
fn is_likely_different_encoding(data: &str, expected: MarcEncoding) -> bool {
match expected {
MarcEncoding::Utf8 => {
// Check for patterns that suggest UTF-8 vs MARC-8
// UTF-8 would have multi-byte sequences for non-ASCII characters
// MARC-8 would use escape sequences
contains_escape_sequences(data) && !contains_valid_utf8_multibyte(data)
},
MarcEncoding::Marc8 => {
// Check for patterns that suggest valid UTF-8 encoding of non-ASCII
// Count high bytes (0x80-0xFF) that form valid UTF-8 sequences
let high_byte_count = data.as_bytes().iter().filter(|&&b| b >= 0x80).count();
let total_bytes = data.len();
// If we have significant high bytes but they don't form valid UTF-8
// escape sequences, this is likely UTF-8
if high_byte_count > total_bytes / 10 {
contains_valid_utf8_multibyte(data)
} else {
false
}
},
}
}
/// Attempt to detect the encoding used in a string
fn detect_encoding_from_string(s: &str) -> Option<MarcEncoding> {
let bytes = s.as_bytes();
// Check for UTF-8 multibyte sequences
let utf8_indicator_count = count_utf8_indicators(bytes);
// Check for MARC-8 escape sequences
let marc8_escape_count = bytes.windows(2).filter(|w| w[0] == 0x1B).count();
// Check for actual valid UTF-8 encoding of high bytes
let mut has_valid_utf8 = false;
let mut i = 0;
while i < bytes.len() {
let b = bytes[i];
if b >= 0xC0 {
// Potential UTF-8 multibyte start
let len = utf8_sequence_length(b);
if len > 1 && i + len <= bytes.len() && is_valid_utf8_sequence(&bytes[i..i + len]) {
has_valid_utf8 = true;
i += len;
continue;
}
}
i += 1;
}
if marc8_escape_count > 0 {
Some(MarcEncoding::Marc8)
} else if utf8_indicator_count > 2 || has_valid_utf8 {
Some(MarcEncoding::Utf8)
} else {
None
}
}
/// Validate that a record's encoding is consistent
///
/// Returns `Ok(())` if encoding is consistent, or an error describing the issue.
///
/// # Errors
///
/// Returns an error if mixed encodings or undetermined encodings are detected.
pub fn validate_encoding(record: &Record) -> Result<()> {
match Self::analyze_encoding(record)? {
EncodingAnalysis::Consistent(_) => Ok(()),
EncodingAnalysis::Mixed {
primary,
secondary,
field_count,
} => Err(MarcError::EncodingError(format!(
"Mixed encodings detected: primary={primary:?}, secondary={secondary:?}, affected fields={field_count}"
))),
EncodingAnalysis::Undetermined => Err(MarcError::EncodingError(
"Unable to determine encoding".to_string(),
)),
}
}
}
/// Check if a string contains MARC-8 escape sequences
fn contains_escape_sequences(s: &str) -> bool {
s.as_bytes().contains(&0x1B)
}
/// Check if a string contains valid UTF-8 multibyte sequences
fn contains_valid_utf8_multibyte(s: &str) -> bool {
let bytes = s.as_bytes();
for i in 0..bytes.len() {
let b = bytes[i];
if b >= 0xC0 {
let len = utf8_sequence_length(b);
if len > 1 && i + len <= bytes.len() && is_valid_utf8_sequence(&bytes[i..i + len]) {
return true;
}
}
}
false
}
/// Get the expected length of a UTF-8 sequence from the first byte
fn utf8_sequence_length(first_byte: u8) -> usize {
match first_byte {
0xC0..=0xDF => 2,
0xE0..=0xEF => 3,
0xF0..=0xF7 => 4,
_ => 1,
}
}
/// Check if a byte sequence is a valid UTF-8 sequence
fn is_valid_utf8_sequence(bytes: &[u8]) -> bool {
if bytes.is_empty() {
return false;
}
let first = bytes[0];
let expected_len = utf8_sequence_length(first);
if bytes.len() != expected_len {
return false;
}
// Check continuation bytes
for (i, &byte) in bytes.iter().enumerate() {
if i == 0 {
continue; // First byte already checked
}
if (byte & 0xC0) != 0x80 {
return false; // Invalid continuation byte
}
}
true
}
/// Count indicators of UTF-8 multibyte characters
fn count_utf8_indicators(bytes: &[u8]) -> usize {
bytes.iter().filter(|&&b| matches!(b, 0xC0..=0xF7)).count()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_utf8_sequence_length() {
assert_eq!(utf8_sequence_length(0x41), 1); // 'A'
assert_eq!(utf8_sequence_length(0xC0), 2); // 2-byte
assert_eq!(utf8_sequence_length(0xE0), 3); // 3-byte
assert_eq!(utf8_sequence_length(0xF0), 4); // 4-byte
}
#[test]
fn test_is_valid_utf8_sequence() {
assert!(is_valid_utf8_sequence(b"A")); // 1-byte
assert!(is_valid_utf8_sequence(&[0xC3, 0xA9])); // é in UTF-8
assert!(is_valid_utf8_sequence(&[0xE2, 0x82, 0xAC])); // € in UTF-8
assert!(!is_valid_utf8_sequence(&[0xC3])); // Incomplete
assert!(!is_valid_utf8_sequence(&[0xC3, 0x28])); // Invalid continuation
}
#[test]
fn test_contains_escape_sequences() {
assert!(contains_escape_sequences("test\x1Btest"));
assert!(!contains_escape_sequences("test"));
}
#[test]
fn test_contains_valid_utf8_multibyte() {
assert!(contains_valid_utf8_multibyte("café")); // Has é in UTF-8
assert!(!contains_valid_utf8_multibyte("test")); // All ASCII
}
#[test]
fn test_detect_encoding_utf8() {
let utf8_str = "café"; // Contains UTF-8 encoded character
let result = EncodingValidator::detect_encoding_from_string(utf8_str);
assert_eq!(result, Some(MarcEncoding::Utf8));
}
#[test]
fn test_detect_encoding_ascii() {
let ascii_str = "test";
let result = EncodingValidator::detect_encoding_from_string(ascii_str);
// ASCII alone is ambiguous
assert!(result.is_none() || result == Some(MarcEncoding::Utf8));
}
}