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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Media AST nodes for embedded fonts and graphics
//!
//! Contains Font and Graphic structs representing embedded media from the
//! [Fonts] and [Graphics] sections with zero-copy design and UU-decoding.

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

#[cfg(not(feature = "std"))]
use alloc::format;
use alloc::vec::Vec;

use super::Span;
#[cfg(debug_assertions)]
use core::ops::Range;

/// Embedded font from `[Fonts\]` section
///
/// Represents a font file embedded in the ASS script using UU-encoding.
/// Provides lazy decoding to avoid processing overhead unless the font
/// data is actually needed.
///
/// # Examples
///
/// ```rust
/// use ass_core::parser::ast::{Font, Span};
///
/// let font = Font {
///     filename: "custom.ttf",
///     data_lines: vec!["begin 644 custom.ttf", "M'XL..."],
///     span: Span::new(0, 0, 0, 0),
/// };
///
/// // Decode when needed
/// let decoded = font.decode_data()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Font<'a> {
    /// Font filename as it appears in the `[Fonts\]` section
    pub filename: &'a str,

    /// UU-encoded font data lines as zero-copy spans
    pub data_lines: Vec<&'a str>,

    /// Span in source text where this font is defined
    pub span: Span,
}

impl Font<'_> {
    /// Decode UU-encoded font data with lazy evaluation
    ///
    /// Converts the UU-encoded data lines to raw binary font data.
    /// This is expensive so it's only done when explicitly requested.
    ///
    /// # Returns
    ///
    /// Decoded binary font data on success, error if UU-decoding fails
    ///
    /// # Errors
    ///
    /// Returns an error if the UU-encoded data is malformed or cannot be decoded.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use ass_core::parser::ast::{Font, Span};
    /// # let font = Font { filename: "test.ttf", data_lines: vec![], span: Span::new(0, 0, 0, 0) };
    /// match font.decode_data() {
    ///     Ok(data) => println!("Font size: {} bytes", data.len()),
    ///     Err(e) => eprintln!("Decode error: {}", e),
    /// }
    /// ```
    pub fn decode_data(&self) -> Result<Vec<u8>, crate::utils::CoreError> {
        crate::utils::decode_uu_data(self.data_lines.iter().copied())
    }

    /// Convert font to ASS string representation
    ///
    /// Generates the font entry as it appears in the `[Fonts\]` section.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use ass_core::parser::ast::{Font, Span};
    /// let font = Font {
    ///     filename: "custom.ttf",
    ///     data_lines: vec!["begin 644 custom.ttf", "M'XL...", "end"],
    ///     span: Span::new(0, 0, 0, 0),
    /// };
    /// let ass_string = font.to_ass_string();
    /// assert!(ass_string.starts_with("fontname: custom.ttf\n"));
    /// assert!(ass_string.contains("M'XL..."));
    /// ```
    #[must_use]
    pub fn to_ass_string(&self) -> alloc::string::String {
        let mut result = format!("fontname: {}\n", self.filename);
        for line in &self.data_lines {
            result.push_str(line);
            result.push('\n');
        }
        result
    }

    /// Validate all spans in this Font reference valid source
    ///
    /// Debug helper to ensure zero-copy invariants are maintained.
    /// Validates that filename and all data line references point to
    /// memory within the specified source range.
    ///
    /// Only available in debug builds to avoid performance overhead.
    #[cfg(debug_assertions)]
    #[must_use]
    pub fn validate_spans(&self, source_range: &Range<usize>) -> bool {
        let filename_ptr = self.filename.as_ptr() as usize;
        let filename_valid = source_range.contains(&filename_ptr);

        let data_valid = self.data_lines.iter().all(|line| {
            let ptr = line.as_ptr() as usize;
            source_range.contains(&ptr)
        });

        filename_valid && data_valid
    }
}

/// Embedded graphic from `[Graphics\]` section
///
/// Represents an image file embedded in the ASS script using UU-encoding.
/// Commonly used for logos, textures, and other graphical elements.
/// Provides lazy decoding for performance.
///
/// # Examples
///
/// ```rust
/// use ass_core::parser::ast::{Graphic, Span};
///
/// let graphic = Graphic {
///     filename: "logo.png",
///     data_lines: vec!["begin 644 logo.png", "M89PNG..."],
///     span: Span::new(0, 0, 0, 0),
/// };
///
/// let decoded = graphic.decode_data()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Graphic<'a> {
    /// Graphic filename as it appears in the `[Graphics\]` section
    pub filename: &'a str,

    /// UU-encoded graphic data lines as zero-copy spans
    pub data_lines: Vec<&'a str>,

    /// Span in source text where this graphic is defined
    pub span: Span,
}

impl Graphic<'_> {
    /// Decode UU-encoded graphic data with lazy evaluation
    ///
    /// Converts the UU-encoded data lines to raw binary image data.
    /// This is expensive so it's only done when explicitly requested.
    ///
    /// # Returns
    ///
    /// Decoded binary image data on success, error if UU-decoding fails
    ///
    /// # Errors
    ///
    /// Returns an error if the UU-encoded data is malformed or cannot be decoded.
    pub fn decode_data(&self) -> Result<Vec<u8>, crate::utils::CoreError> {
        crate::utils::decode_uu_data(self.data_lines.iter().copied())
    }

    /// Convert graphic to ASS string representation
    ///
    /// Generates the graphic entry as it appears in the `[Graphics\]` section.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use ass_core::parser::ast::{Graphic, Span};
    /// let graphic = Graphic {
    ///     filename: "logo.png",
    ///     data_lines: vec!["begin 644 logo.png", "M'XL...", "end"],
    ///     span: Span::new(0, 0, 0, 0),
    /// };
    /// let ass_string = graphic.to_ass_string();
    /// assert!(ass_string.starts_with("filename: logo.png\n"));
    /// assert!(ass_string.contains("M'XL..."));
    /// ```
    #[must_use]
    pub fn to_ass_string(&self) -> alloc::string::String {
        let mut result = format!("filename: {}\n", self.filename);
        for line in &self.data_lines {
            result.push_str(line);
            result.push('\n');
        }
        result
    }

    /// Validate all spans in this Graphic reference valid source
    ///
    /// Debug helper to ensure zero-copy invariants are maintained.
    /// Validates that filename and all data line references point to
    /// memory within the specified source range.
    ///
    /// Only available in debug builds to avoid performance overhead.
    #[cfg(debug_assertions)]
    #[must_use]
    pub fn validate_spans(&self, source_range: &Range<usize>) -> bool {
        let filename_ptr = self.filename.as_ptr() as usize;
        let filename_valid = source_range.contains(&filename_ptr);

        let data_valid = self.data_lines.iter().all(|line| {
            let ptr = line.as_ptr() as usize;
            source_range.contains(&ptr)
        });

        filename_valid && data_valid
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(not(feature = "std"))]
    use alloc::{format, vec};

    #[test]
    fn font_creation() {
        let font = Font {
            filename: "test.ttf",
            data_lines: vec!["line1", "line2"],
            span: Span::new(0, 0, 0, 0),
        };

        assert_eq!(font.filename, "test.ttf");
        assert_eq!(font.data_lines.len(), 2);
        assert_eq!(font.data_lines[0], "line1");
        assert_eq!(font.data_lines[1], "line2");
    }

    #[test]
    fn graphic_creation() {
        let graphic = Graphic {
            filename: "logo.png",
            data_lines: vec!["data1", "data2", "data3"],
            span: Span::new(0, 0, 0, 0),
        };

        assert_eq!(graphic.filename, "logo.png");
        assert_eq!(graphic.data_lines.len(), 3);
        assert_eq!(graphic.data_lines[0], "data1");
    }

    #[test]
    fn font_clone_eq() {
        let font = Font {
            filename: "test.ttf",
            data_lines: vec!["data"],
            span: Span::new(0, 0, 0, 0),
        };

        let cloned = font.clone();
        assert_eq!(font, cloned);
    }

    #[test]
    fn graphic_clone_eq() {
        let graphic = Graphic {
            filename: "test.png",
            data_lines: vec!["data"],
            span: Span::new(0, 0, 0, 0),
        };

        let cloned = graphic.clone();
        assert_eq!(graphic, cloned);
    }

    #[test]
    fn font_debug() {
        let font = Font {
            filename: "debug.ttf",
            data_lines: vec!["test"],
            span: Span::new(0, 0, 0, 0),
        };

        let debug_str = format!("{font:?}");
        assert!(debug_str.contains("Font"));
        assert!(debug_str.contains("debug.ttf"));
    }

    #[test]
    fn graphic_debug() {
        let graphic = Graphic {
            filename: "debug.png",
            data_lines: vec!["test"],
            span: Span::new(0, 0, 0, 0),
        };

        let debug_str = format!("{graphic:?}");
        assert!(debug_str.contains("Graphic"));
        assert!(debug_str.contains("debug.png"));
    }

    #[test]
    fn empty_data_lines() {
        let font = Font {
            filename: "empty.ttf",
            data_lines: Vec::new(),
            span: Span::new(0, 0, 0, 0),
        };

        let graphic = Graphic {
            filename: "empty.png",
            data_lines: Vec::new(),
            span: Span::new(0, 0, 0, 0),
        };

        assert!(font.data_lines.is_empty());
        assert!(graphic.data_lines.is_empty());
    }

    #[test]
    fn media_inequality() {
        let font1 = Font {
            filename: "font1.ttf",
            data_lines: vec!["data"],
            span: Span::new(0, 0, 0, 0),
        };

        let font2 = Font {
            filename: "font2.ttf",
            data_lines: vec!["data"],
            span: Span::new(0, 0, 0, 0),
        };

        assert_ne!(font1, font2);
    }

    #[test]
    fn font_decode_data_valid() {
        // Valid UU-encoded data for "Cat" (test with known encoding)
        let font = Font {
            filename: "test.ttf",
            data_lines: vec!["#0V%T", "`"],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = font.decode_data().unwrap();
        assert_eq!(decoded, b"Cat");
    }

    #[test]
    fn font_decode_data_empty_lines() {
        let font = Font {
            filename: "test.ttf",
            data_lines: vec![],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = font.decode_data().unwrap();
        assert!(decoded.is_empty());
    }

    #[test]
    fn font_decode_data_whitespace_lines() {
        let font = Font {
            filename: "test.ttf",
            data_lines: vec!["   ", "\t\n", ""],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = font.decode_data().unwrap();
        assert!(decoded.is_empty());
    }

    #[test]
    fn font_decode_data_with_end_marker() {
        let font = Font {
            filename: "test.ttf",
            data_lines: vec!["#0V%T", "end"],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = font.decode_data().unwrap();
        assert_eq!(decoded, b"Cat");
    }

    #[test]
    fn font_decode_data_zero_length_line() {
        let font = Font {
            filename: "test.ttf",
            data_lines: vec!["#0V%T", " "],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = font.decode_data().unwrap();
        assert_eq!(decoded, b"Cat");
    }

    #[test]
    fn font_decode_data_multiline() {
        // Multi-line UU-encoded data
        let font = Font {
            filename: "test.ttf",
            data_lines: vec!["$4F3\"", "$4F3\""],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = font.decode_data().unwrap();
        // Should decode both lines and concatenate results
        assert_eq!(decoded.len(), 6); // 3 bytes per line
    }

    #[test]
    fn graphic_decode_data_valid() {
        // Valid UU-encoded data for "PNG" (test with known encoding)
        let graphic = Graphic {
            filename: "test.png",
            data_lines: vec!["#4$Y'"],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = graphic.decode_data().unwrap();
        assert_eq!(decoded, b"PNG");
    }

    #[test]
    fn graphic_decode_data_empty_lines() {
        let graphic = Graphic {
            filename: "test.png",
            data_lines: vec![],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = graphic.decode_data().unwrap();
        assert!(decoded.is_empty());
    }

    #[test]
    fn graphic_decode_data_with_end_marker() {
        let graphic = Graphic {
            filename: "test.png",
            data_lines: vec!["#4$Y'", "end"],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = graphic.decode_data().unwrap();
        assert_eq!(decoded, b"PNG");
    }

    #[test]
    fn graphic_decode_data_whitespace_handling() {
        let graphic = Graphic {
            filename: "test.png",
            data_lines: vec!["#4$Y'  ", "\t\n", ""],
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = graphic.decode_data().unwrap();
        assert_eq!(decoded, b"PNG");
    }

    #[test]
    fn font_decode_data_handles_malformed_gracefully() {
        // UU decoding should not panic on malformed data but may return unexpected results
        let font = Font {
            filename: "test.ttf",
            data_lines: vec!["invalid-characters-here"],
            span: Span::new(0, 0, 0, 0),
        };
        // Should not panic, result depends on UU decoder implementation
        let _result = font.decode_data();
    }

    #[test]
    fn graphic_decode_data_handles_malformed_gracefully() {
        // UU decoding should not panic on malformed data but may return unexpected results
        let graphic = Graphic {
            filename: "test.png",
            data_lines: vec!["!@#$%^&*()"],
            span: Span::new(0, 0, 0, 0),
        };
        // Should not panic, result depends on UU decoder implementation
        let _result = graphic.decode_data();
    }

    #[test]
    fn font_decode_data_length_validation() {
        // Test that length encoding in first character is respected
        let font = Font {
            filename: "test.ttf",
            data_lines: vec!["!    "], // '!' encodes length 1, but provides more data
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = font.decode_data().unwrap();
        assert_eq!(decoded.len(), 1); // Should be truncated to declared length
    }

    #[test]
    fn graphic_decode_data_length_validation() {
        // Test that length encoding in first character is respected
        let graphic = Graphic {
            filename: "test.png",
            data_lines: vec!["\"````"], // '"' encodes length 2, provides padding
            span: Span::new(0, 0, 0, 0),
        };
        let decoded = graphic.decode_data().unwrap();
        assert_eq!(decoded.len(), 2); // Should be truncated to declared length
    }

    #[cfg(debug_assertions)]
    #[test]
    fn font_validate_spans() {
        let source = "fontname: test.ttf\ndata1\ndata2";
        let font = Font {
            filename: &source[10..18],                          // "test.ttf"
            data_lines: vec![&source[19..24], &source[25..30]], // "data1", "data2"
            span: Span::new(0, 0, 0, 0),
        };

        let source_range = (source.as_ptr() as usize)..(source.as_ptr() as usize + source.len());
        assert!(font.validate_spans(&source_range));
    }

    #[cfg(debug_assertions)]
    #[test]
    fn graphic_validate_spans() {
        let source = "filename: logo.png\nimage1\nimage2";
        let graphic = Graphic {
            filename: &source[10..18],                          // "logo.png"
            data_lines: vec![&source[19..25], &source[26..32]], // "image1", "image2"
            span: Span::new(0, 0, 0, 0),
        };

        let source_range = (source.as_ptr() as usize)..(source.as_ptr() as usize + source.len());
        assert!(graphic.validate_spans(&source_range));
    }

    #[cfg(debug_assertions)]
    #[test]
    fn font_validate_spans_invalid() {
        let source1 = "fontname: test.ttf";
        let source2 = "different source";

        let font = Font {
            filename: &source1[10..18],       // "test.ttf" from source1
            data_lines: vec![&source2[0..9]], // "different" from source2
            span: Span::new(0, 0, 0, 0),
        };

        let source1_range =
            (source1.as_ptr() as usize)..(source1.as_ptr() as usize + source1.len());
        assert!(!font.validate_spans(&source1_range)); // Should fail because data_lines reference different source
    }
}