ass-core 0.1.1

High-performance ASS subtitle format parser and analyzer
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Comprehensive tests for utils module functionality

use super::*;
use alloc::{format, string::String, vec::Vec};

#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]

#[test]
fn time_parsing_valid_formats() {
    // Test standard H:MM:SS.CC format
    assert!(parse_time_string("0:00:30.50").is_ok());
    assert!(parse_time_string("1:23:45.67").is_ok());
    assert!(parse_time_string("12:34:56.78").is_ok());

    // Test single digit components
    assert!(parse_time_string("0:01:02.03").is_ok());

    // Test edge cases
    assert!(parse_time_string("0:00:00.00").is_ok());
    assert!(parse_time_string("23:59:59.99").is_ok());
}

#[test]
fn time_parsing_invalid_formats() {
    // Missing components
    assert!(parse_time_string("").is_err());
    assert!(parse_time_string("1:23").is_err());
    assert!(parse_time_string("1:23:45").is_err());

    // Invalid characters
    assert!(parse_time_string("a:23:45.67").is_err());
    assert!(parse_time_string("1:ab:45.67").is_err());
    assert!(parse_time_string("1:23:cd.67").is_err());
    assert!(parse_time_string("1:23:45.ef").is_err());

    // Out of range values
    assert!(parse_time_string("1:60:45.67").is_err());
    assert!(parse_time_string("1:23:60.67").is_err());
    assert!(parse_time_string("1:23:45.100").is_err());
}

#[test]
fn time_parsing_precision() {
    let result = parse_time_string("1:23:45.67").unwrap();
    assert_eq!(result.hours, 1);
    assert_eq!(result.minutes, 23);
    assert_eq!(result.seconds, 45);
    assert_eq!(result.centiseconds, 67);
}

#[test]
fn color_parsing_hex_formats() {
    // Standard hex colors
    assert!(parse_color_string("#FF0000").is_ok());
    assert!(parse_color_string("#00FF00").is_ok());
    assert!(parse_color_string("#0000FF").is_ok());

    // ASS format colors
    assert!(parse_color_string("&H0000FF&").is_ok());
    assert!(parse_color_string("&HFF0000&").is_ok());
    assert!(parse_color_string("&H00FF00&").is_ok());

    // Without alpha
    assert!(parse_color_string("FF0000").is_ok());
    assert!(parse_color_string("00FF00").is_ok());

    // With alpha
    assert!(parse_color_string("&H80FF0000&").is_ok());
}

#[test]
fn color_parsing_invalid_formats() {
    // Invalid hex characters
    assert!(parse_color_string("&HGGGGGG&").is_err());
    assert!(parse_color_string("#ZZZZZZ").is_err());

    // Wrong length
    assert!(parse_color_string("&HFF&").is_err());
    assert!(parse_color_string("#FF").is_err());
    assert!(parse_color_string("FFFFFFF").is_err());

    // Malformed ASS format
    assert!(parse_color_string("&HFF0000").is_err());
    assert!(parse_color_string("FF0000&").is_err());
}

#[test]
fn numeric_parsing_integers() {
    assert_eq!(parse_numeric_string("0").unwrap(), NumericValue::Integer(0));
    assert_eq!(
        parse_numeric_string("42").unwrap(),
        NumericValue::Integer(42)
    );
    assert_eq!(
        parse_numeric_string("-123").unwrap(),
        NumericValue::Integer(-123)
    );
    assert_eq!(
        parse_numeric_string("1000").unwrap(),
        NumericValue::Integer(1000)
    );
}

#[test]
fn numeric_parsing_floats() {
    assert_eq!(
        parse_numeric_string("3.14").unwrap(),
        NumericValue::Float(3.14)
    );
    assert_eq!(
        parse_numeric_string("-2.5").unwrap(),
        NumericValue::Float(-2.5)
    );
    assert_eq!(
        parse_numeric_string("0.0").unwrap(),
        NumericValue::Float(0.0)
    );
}

#[test]
fn numeric_parsing_invalid() {
    assert!(parse_numeric_string("").is_err());
    assert!(parse_numeric_string("abc").is_err());
    assert!(parse_numeric_string("12.34.56").is_err());
    assert!(parse_numeric_string("1.2.3").is_err());
    assert!(parse_numeric_string("not_a_number").is_err());
}

#[test]
fn text_validation_ascii() {
    assert!(validate_text_content("Hello World").is_ok());
    assert!(validate_text_content("1234567890").is_ok());
    assert!(validate_text_content("!@#$%^&*()").is_ok());
}

#[test]
fn text_validation_unicode() {
    assert!(validate_text_content("こんにちは").is_ok());
    assert!(validate_text_content("Здравствуй").is_ok());
    assert!(validate_text_content("مرحبا").is_ok());
    assert!(validate_text_content("🎵🎬🎭").is_ok());
}

#[test]
fn text_validation_control_chars() {
    // Control characters should be flagged
    let result = validate_text_content("Hello\x00World");
    assert!(result.is_err());

    let result = validate_text_content("Text\x1FTest");
    assert!(result.is_err());
}

#[test]
fn text_validation_line_endings() {
    // Line endings should be allowed
    assert!(validate_text_content("Line1\nLine2").is_ok());
    assert!(validate_text_content("Line1\r\nLine2").is_ok());
    assert!(validate_text_content("Line1\rLine2").is_ok());
}

#[test]
fn text_sanitization() {
    let input = "Hello\x00\x1F World";
    let result = sanitize_text_content(input);
    assert_eq!(result, "Hello World");

    let input = "Good\x0BText\x0CTest";
    let result = sanitize_text_content(input);
    assert_eq!(result, "GoodTextTest");
}

#[test]
fn field_name_normalization() {
    assert_eq!(normalize_field_name("Title"), "title");
    assert_eq!(normalize_field_name("ScriptType"), "scripttype");
    assert_eq!(normalize_field_name("WrapStyle"), "wrapstyle");
    assert_eq!(normalize_field_name("UPPERCASE"), "uppercase");
    assert_eq!(normalize_field_name("MixedCase"), "mixedcase");
}

#[test]
fn field_name_normalization_whitespace() {
    assert_eq!(normalize_field_name(" Title "), "title");
    assert_eq!(normalize_field_name("\tScriptType\t"), "scripttype");
    assert_eq!(normalize_field_name("\nWrapStyle\n"), "wrapstyle");
}

#[test]
fn escape_sequence_parsing() {
    assert_eq!(parse_escape_sequence("\\n"), Some('\n'));
    assert_eq!(parse_escape_sequence("\\r"), Some('\r'));
    assert_eq!(parse_escape_sequence("\\t"), Some('\t'));
    assert_eq!(parse_escape_sequence("`[Events]`"), Some('\\'));
    assert_eq!(parse_escape_sequence("\\{"), Some('{'));
    assert_eq!(parse_escape_sequence("\\}"), Some('}'));
}

#[test]
fn escape_sequence_parsing_invalid() {
    assert_eq!(parse_escape_sequence("\\x"), None);
    assert_eq!(parse_escape_sequence("\\z"), None);
    assert_eq!(parse_escape_sequence("n"), None);
    assert_eq!(parse_escape_sequence(""), None);
}

#[test]
fn unescape_text_basic() {
    assert_eq!(unescape_text("Hello\\nWorld"), "Hello\nWorld");
    assert_eq!(unescape_text("Tab\\tSeparated"), "Tab\tSeparated");
    assert_eq!(unescape_text("Quote\\\"Test"), "Quote\"Test");
    assert_eq!(unescape_text("Brace\\{Test\\}"), "Brace{Test}");
}

#[test]
fn unescape_text_multiple() {
    assert_eq!(
        unescape_text("Line1\\nLine2\\nLine3"),
        "Line1\nLine2\nLine3"
    );
    assert_eq!(unescape_text("\\t\\r\\n"), "\t\r\n");
}

#[test]
fn unescape_text_no_escapes() {
    assert_eq!(unescape_text("Plain text"), "Plain text");
    assert_eq!(unescape_text("No escapes here"), "No escapes here");
    assert_eq!(unescape_text(""), "");
}

#[test]
fn escape_text_basic() {
    assert_eq!(escape_text("Hello\nWorld"), "Hello\\nWorld");
    assert_eq!(escape_text("Tab\tSeparated"), "Tab\\tSeparated");
    assert_eq!(escape_text("Quote\"Test"), "Quote\\\"Test");
    assert_eq!(escape_text("Brace{Test}"), "Brace\\{Test\\}");
}

#[test]
fn escape_unescape_round_trip() {
    let original = "Hello\nWorld\tWith\"Quotes{And}Braces";
    let escaped = escape_text(original);
    let unescaped = unescape_text(&escaped);
    assert_eq!(original, unescaped);
}

#[test]
fn percentage_parsing() {
    assert_eq!(parse_percentage("50%").unwrap(), 0.5);
    assert_eq!(parse_percentage("100%").unwrap(), 1.0);
    assert_eq!(parse_percentage("0%").unwrap(), 0.0);
    assert_eq!(parse_percentage("25.5%").unwrap(), 0.255);
}

#[test]
fn percentage_parsing_invalid() {
    assert!(parse_percentage("50").is_err()); // Missing %
    assert!(parse_percentage("%50").is_err()); // % at start
    assert!(parse_percentage("abc%").is_err()); // Invalid number
    assert!(parse_percentage("").is_err()); // Empty
}

#[test]
fn boolean_parsing() {
    assert_eq!(parse_boolean_value("1").unwrap(), true);
    assert_eq!(parse_boolean_value("-1").unwrap(), true);
    assert_eq!(parse_boolean_value("0").unwrap(), false);
    assert_eq!(parse_boolean_value("true").unwrap(), true);
    assert_eq!(parse_boolean_value("false").unwrap(), false);
    assert_eq!(parse_boolean_value("yes").unwrap(), true);
    assert_eq!(parse_boolean_value("no").unwrap(), false);
}

#[test]
fn boolean_parsing_case_insensitive() {
    assert_eq!(parse_boolean_value("TRUE").unwrap(), true);
    assert_eq!(parse_boolean_value("False").unwrap(), false);
    assert_eq!(parse_boolean_value("YES").unwrap(), true);
    assert_eq!(parse_boolean_value("No").unwrap(), false);
}

#[test]
fn boolean_parsing_invalid() {
    assert!(parse_boolean_value("maybe").is_err());
    assert!(parse_boolean_value("2").is_err());
    assert!(parse_boolean_value("").is_err());
    assert!(parse_boolean_value("on").is_err());
    assert!(parse_boolean_value("off").is_err());
}

#[test]
fn memory_limit_checking() {
    assert!(check_memory_usage(1000, 2000).is_ok());
    assert!(check_memory_usage(1000, 1000).is_ok());
    assert!(check_memory_usage(2000, 1000).is_err());
}

#[test]
fn string_trimming() {
    assert_eq!(trim_ass_whitespace("  hello  "), "hello");
    assert_eq!(trim_ass_whitespace("\thello\t"), "hello");
    assert_eq!(trim_ass_whitespace("\nhello\n"), "hello");
    assert_eq!(trim_ass_whitespace(" \t\nhello\n\t "), "hello");
}

#[test]
fn string_trimming_unicode() {
    assert_eq!(trim_ass_whitespace("  こんにちは  "), "こんにちは");
    assert_eq!(trim_ass_whitespace("\t🎵\t"), "🎵");
}

#[test]
fn string_splitting_csv() {
    let result = split_csv_line("a,b,c");
    assert_eq!(result, vec!["a", "b", "c"]);

    let result = split_csv_line("field1, field2 , field3");
    assert_eq!(result, vec!["field1", "field2", "field3"]);
}

#[test]
fn string_splitting_csv_empty() {
    let result = split_csv_line("");
    assert_eq!(result, vec![""]);

    let result = split_csv_line(",");
    assert_eq!(result, vec!["", ""]);

    let result = split_csv_line("a,,c");
    assert_eq!(result, vec!["a", "", "c"]);
}

#[test]
fn string_splitting_csv_quoted() {
    let result = split_csv_line("\"quoted field\",normal,\"another quoted\"");
    assert_eq!(result, vec!["quoted field", "normal", "another quoted"]);

    let result = split_csv_line("\"field with, comma\",other");
    assert_eq!(result, vec!["field with, comma", "other"]);
}

#[test]
fn line_ending_normalization() {
    assert_eq!(normalize_line_endings("line1\r\nline2"), "line1\nline2");
    assert_eq!(normalize_line_endings("line1\rline2"), "line1\nline2");
    assert_eq!(normalize_line_endings("line1\nline2"), "line1\nline2");

    let mixed = "line1\r\nline2\rline3\nline4";
    assert_eq!(normalize_line_endings(mixed), "line1\nline2\nline3\nline4");
}

#[test]
fn performance_measurement() {
    let start = get_timestamp();
    // Small operation
    let _ = format!("test");
    let end = get_timestamp();

    assert!(end >= start);
    assert!(end - start < 1000); // Should be less than 1ms
}

#[test]
fn error_conversion_chain() {
    let parse_error = "not_a_number".parse::<i32>().unwrap_err();
    let core_error: CoreError = parse_error.into();

    assert!(matches!(core_error, CoreError::InvalidNumeric(_)));
    assert!(core_error.is_recoverable());
    assert!(!core_error.is_internal_bug());
}

#[test]
fn utility_function_integration() {
    // Test combination of multiple utility functions
    let input = "  Title:  Test Script  \r\n";
    let normalized = normalize_line_endings(input);
    let trimmed = trim_ass_whitespace(&normalized);
    let parts: Vec<&str> = trimmed.split(':').collect();

    assert_eq!(parts.len(), 2);
    assert_eq!(normalize_field_name(parts[0]), "title");
    assert_eq!(trim_ass_whitespace(parts[1]), "Test Script");
}

#[test]
fn edge_case_empty_inputs() {
    assert!(parse_time_string("").is_err());
    assert!(parse_color_string("").is_err());
    assert!(parse_numeric_string("").is_err());
    assert!(validate_text_content("").is_ok());
    assert_eq!(normalize_field_name(""), "");
    assert_eq!(trim_ass_whitespace(""), "");
    assert_eq!(unescape_text(""), "");
    assert_eq!(escape_text(""), "");
}

#[test]
fn large_input_handling() {
    let large_string = "a".repeat(10000);
    assert!(validate_text_content(&large_string).is_ok());

    let large_field = "field".repeat(1000);
    let normalized = normalize_field_name(&large_field);
    assert_eq!(normalized.len(), large_field.len());

    // Test memory limits
    assert!(check_memory_usage(5000, 10000).is_ok());
    assert!(check_memory_usage(15000, 10000).is_err());
}