rust-expect 0.1.0

Next-generation Expect-style terminal automation library for Rust
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Asciinema asciicast v2 format support.

use std::fmt::Write as FmtWrite;
use std::io::{BufRead, Write};
use std::time::Duration;

use super::format::{EventType, Transcript, TranscriptEvent, TranscriptMetadata};
use crate::error::{ExpectError, Result};

/// Asciicast v2 header.
#[derive(Debug, Clone)]
pub struct AsciicastHeader {
    /// Format version.
    pub version: u8,
    /// Terminal width.
    pub width: u16,
    /// Terminal height.
    pub height: u16,
    /// Recording timestamp.
    pub timestamp: Option<u64>,
    /// Total duration.
    pub duration: Option<f64>,
    /// Idle time limit.
    pub idle_time_limit: Option<f64>,
    /// Command.
    pub command: Option<String>,
    /// Title.
    pub title: Option<String>,
    /// Environment.
    pub env: std::collections::HashMap<String, String>,
}

impl Default for AsciicastHeader {
    fn default() -> Self {
        Self {
            version: 2,
            width: 80,
            height: 24,
            timestamp: None,
            duration: None,
            idle_time_limit: None,
            command: None,
            title: None,
            env: std::collections::HashMap::new(),
        }
    }
}

impl AsciicastHeader {
    /// Create a new header.
    #[must_use]
    pub fn new(width: u16, height: u16) -> Self {
        Self {
            width,
            height,
            ..Default::default()
        }
    }

    /// Convert to JSON string.
    #[must_use]
    pub fn to_json(&self) -> String {
        let mut parts = vec![
            format!("\"version\": {}", self.version),
            format!("\"width\": {}", self.width),
            format!("\"height\": {}", self.height),
        ];

        if let Some(ts) = self.timestamp {
            parts.push(format!("\"timestamp\": {ts}"));
        }
        if let Some(dur) = self.duration {
            parts.push(format!("\"duration\": {dur:.6}"));
        }
        if let Some(limit) = self.idle_time_limit {
            parts.push(format!("\"idle_time_limit\": {limit:.1}"));
        }
        if let Some(ref cmd) = self.command {
            parts.push(format!("\"command\": \"{}\"", escape_json(cmd)));
        }
        if let Some(ref title) = self.title {
            parts.push(format!("\"title\": \"{}\"", escape_json(title)));
        }
        if !self.env.is_empty() {
            let env_parts: Vec<String> = self
                .env
                .iter()
                .map(|(k, v)| format!("\"{}\": \"{}\"", escape_json(k), escape_json(v)))
                .collect();
            parts.push(format!("\"env\": {{{}}}", env_parts.join(", ")));
        }

        format!("{{{}}}", parts.join(", "))
    }
}

/// Write a transcript in asciicast v2 format.
pub fn write_asciicast<W: Write>(writer: &mut W, transcript: &Transcript) -> Result<()> {
    let header = AsciicastHeader {
        width: transcript.metadata.width,
        height: transcript.metadata.height,
        timestamp: transcript.metadata.timestamp,
        duration: transcript.metadata.duration.map(|d| d.as_secs_f64()),
        command: transcript.metadata.command.clone(),
        title: transcript.metadata.title.clone(),
        env: transcript.metadata.env.clone(),
        ..Default::default()
    };

    // Write header
    writeln!(writer, "{}", header.to_json())
        .map_err(|e| ExpectError::io_context("writing asciicast header", e))?;

    // Write events
    for event in &transcript.events {
        let time = event.timestamp.as_secs_f64();
        let event_type = match event.event_type {
            EventType::Output => "o",
            EventType::Input => "i",
            EventType::Resize => "r",
            EventType::Marker => "m",
        };
        let data = String::from_utf8_lossy(&event.data);
        writeln!(
            writer,
            "[{:.6}, \"{}\", \"{}\"]",
            time,
            event_type,
            escape_json(&data)
        )
        .map_err(|e| ExpectError::io_context("writing asciicast event", e))?;
    }

    Ok(())
}

/// Read a transcript from asciicast v2 format.
pub fn read_asciicast<R: BufRead>(reader: R) -> Result<Transcript> {
    let mut lines = reader.lines();

    // Parse header
    let header_line = lines
        .next()
        .ok_or_else(|| ExpectError::config("Empty asciicast file"))?
        .map_err(|e| ExpectError::io_context("reading asciicast header line", e))?;

    let header = parse_header(&header_line);

    let metadata = TranscriptMetadata {
        width: header.width,
        height: header.height,
        command: header.command,
        title: header.title,
        timestamp: header.timestamp,
        duration: header.duration.map(Duration::from_secs_f64),
        env: header.env,
    };

    let mut transcript = Transcript::new(metadata);

    // Parse events
    for line in lines {
        let line = line.map_err(|e| ExpectError::io_context("reading asciicast event line", e))?;
        if line.trim().is_empty() {
            continue;
        }
        if let Some(event) = parse_event(&line)? {
            transcript.push(event);
        }
    }

    Ok(transcript)
}

fn parse_header(line: &str) -> AsciicastHeader {
    // Parse numeric fields
    let mut header = AsciicastHeader {
        width: parse_json_number(line, "width").unwrap_or(80) as u16,
        height: parse_json_number(line, "height").unwrap_or(24) as u16,
        version: parse_json_number(line, "version").unwrap_or(2) as u8,
        ..Default::default()
    };

    if let Some(ts) = parse_json_number(line, "timestamp") {
        header.timestamp = Some(ts as u64);
    }

    if let Some(dur) = parse_json_float(line, "duration") {
        header.duration = Some(dur);
    }

    if let Some(limit) = parse_json_float(line, "idle_time_limit") {
        header.idle_time_limit = Some(limit);
    }

    // Parse string fields
    header.command = parse_json_string(line, "command");
    header.title = parse_json_string(line, "title");

    // Parse env object (simplified - handles flat env objects)
    if let Some(env) = parse_json_object(line, "env") {
        header.env = env;
    }

    header
}

/// Parse a numeric JSON field.
fn parse_json_number(json: &str, field: &str) -> Option<i64> {
    let pattern = format!("\"{field}\":");
    let start = json.find(&pattern)?;
    let rest = &json[start + pattern.len()..];
    let rest = rest.trim_start();

    // Find the end of the number
    let end = rest
        .find(|c: char| !c.is_ascii_digit() && c != '-')
        .unwrap_or(rest.len());

    rest[..end].trim().parse().ok()
}

/// Parse a floating-point JSON field.
fn parse_json_float(json: &str, field: &str) -> Option<f64> {
    let pattern = format!("\"{field}\":");
    let start = json.find(&pattern)?;
    let rest = &json[start + pattern.len()..];
    let rest = rest.trim_start();

    // Find the end of the number (including decimal point and exponent)
    let end = rest
        .find(|c: char| {
            !c.is_ascii_digit() && c != '.' && c != '-' && c != 'e' && c != 'E' && c != '+'
        })
        .unwrap_or(rest.len());

    rest[..end].trim().parse().ok()
}

/// Parse a string JSON field.
fn parse_json_string(json: &str, field: &str) -> Option<String> {
    let pattern = format!("\"{field}\":");
    let start = json.find(&pattern)?;
    let rest = &json[start + pattern.len()..];
    let rest = rest.trim_start();

    // Must start with a quote
    if !rest.starts_with('"') {
        return None;
    }

    // Find the closing quote (handling escapes)
    let content = &rest[1..];
    let mut end = 0;
    let mut escaped = false;

    for (i, c) in content.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }
        if c == '\\' {
            escaped = true;
            continue;
        }
        if c == '"' {
            end = i;
            break;
        }
    }

    if end == 0 && !content.is_empty() && !content.starts_with('"') {
        // No closing quote found, check if string is at end
        end = content.len();
    }

    Some(unescape_json(&content[..end]))
}

/// Parse a JSON object field (simplified, handles flat string-value objects).
fn parse_json_object(json: &str, field: &str) -> Option<std::collections::HashMap<String, String>> {
    let pattern = format!("\"{field}\":");
    let start = json.find(&pattern)?;
    let rest = &json[start + pattern.len()..];
    let rest = rest.trim_start();

    // Must start with {
    if !rest.starts_with('{') {
        return None;
    }

    // Find matching closing brace
    let mut depth = 0;
    let mut end = 0;

    for (i, c) in rest.char_indices() {
        match c {
            '{' => depth += 1,
            '}' => {
                depth -= 1;
                if depth == 0 {
                    end = i + 1;
                    break;
                }
            }
            _ => {}
        }
    }

    if end == 0 {
        return None;
    }

    let obj_str = &rest[1..end - 1]; // Content inside braces
    let mut result = std::collections::HashMap::new();

    // Parse key-value pairs
    for pair in obj_str.split(',') {
        let pair = pair.trim();
        if let Some(colon) = pair.find(':') {
            let key = pair[..colon].trim().trim_matches('"');
            let value = pair[colon + 1..].trim().trim_matches('"');
            if !key.is_empty() {
                result.insert(key.to_string(), unescape_json(value));
            }
        }
    }

    Some(result)
}

fn parse_event(line: &str) -> Result<Option<TranscriptEvent>> {
    let line = line.trim();
    if !line.starts_with('[') || !line.ends_with(']') {
        return Ok(None);
    }

    let inner = &line[1..line.len() - 1];
    let parts: Vec<&str> = inner.splitn(3, ',').collect();
    if parts.len() < 3 {
        return Ok(None);
    }

    let time: f64 = parts[0]
        .trim()
        .parse()
        .map_err(|_| ExpectError::config("Invalid timestamp"))?;

    let event_type = parts[1].trim().trim_matches('"');
    let data = parts[2].trim().trim_matches('"');

    let event_type = match event_type {
        "o" => EventType::Output,
        "i" => EventType::Input,
        "r" => EventType::Resize,
        "m" => EventType::Marker,
        _ => return Ok(None),
    };

    Ok(Some(TranscriptEvent {
        timestamp: Duration::from_secs_f64(time),
        event_type,
        data: unescape_json(data).into_bytes(),
    }))
}

fn escape_json(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '"' => result.push_str("\\\""),
            '\\' => result.push_str("\\\\"),
            '\n' => result.push_str("\\n"),
            '\r' => result.push_str("\\r"),
            '\t' => result.push_str("\\t"),
            c if c.is_control() => {
                let _ = write!(result, "\\u{:04x}", c as u32);
            }
            c => result.push(c),
        }
    }
    result
}

fn unescape_json(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('n') => result.push('\n'),
                Some('r') => result.push('\r'),
                Some('t') => result.push('\t'),
                Some('b') => result.push('\u{0008}'), // backspace
                Some('f') => result.push('\u{000C}'), // form feed
                Some('"') => result.push('"'),
                Some('\\') => result.push('\\'),
                Some('/') => result.push('/'),
                Some('u') => {
                    // Parse \uXXXX unicode escape
                    let mut hex = String::with_capacity(4);
                    for _ in 0..4 {
                        if let Some(&c) = chars.peek() {
                            if c.is_ascii_hexdigit() {
                                hex.push(chars.next().unwrap());
                            } else {
                                break;
                            }
                        }
                    }
                    if hex.len() == 4
                        && let Ok(code) = u32::from_str_radix(&hex, 16)
                        && let Some(ch) = char::from_u32(code)
                    {
                        result.push(ch);
                        continue;
                    }
                    // Invalid escape, keep as-is
                    result.push_str("\\u");
                    result.push_str(&hex);
                }
                Some(c) => {
                    result.push('\\');
                    result.push(c);
                }
                None => result.push('\\'),
            }
        } else {
            result.push(c);
        }
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn asciicast_header() {
        let header = AsciicastHeader::new(80, 24);
        let json = header.to_json();
        assert!(json.contains("\"version\": 2"));
        assert!(json.contains("\"width\": 80"));
    }

    #[test]
    fn escape_special_chars() {
        assert_eq!(escape_json("hello\nworld"), "hello\\nworld");
        assert_eq!(escape_json("say \"hi\""), "say \\\"hi\\\"");
    }

    #[test]
    fn roundtrip() {
        let mut transcript = Transcript::new(TranscriptMetadata::new(80, 24));
        transcript.push(TranscriptEvent::output(
            Duration::from_millis(100),
            b"hello",
        ));

        let mut buf = Vec::new();
        write_asciicast(&mut buf, &transcript).unwrap();

        let parsed = read_asciicast(buf.as_slice()).unwrap();
        assert_eq!(parsed.events.len(), 1);
    }

    #[test]
    fn parse_json_number_basic() {
        let json = r#"{"version": 2, "width": 120, "height": 40}"#;
        assert_eq!(parse_json_number(json, "version"), Some(2));
        assert_eq!(parse_json_number(json, "width"), Some(120));
        assert_eq!(parse_json_number(json, "height"), Some(40));
        assert_eq!(parse_json_number(json, "nonexistent"), None);
    }

    #[test]
    fn parse_json_number_negative() {
        let json = r#"{"offset": -100}"#;
        assert_eq!(parse_json_number(json, "offset"), Some(-100));
    }

    #[test]
    fn parse_json_float_basic() {
        let json = r#"{"duration": 123.456789, "idle_time_limit": 2.5}"#;
        assert!((parse_json_float(json, "duration").unwrap() - 123.456_789).abs() < 0.000_001);
        assert!((parse_json_float(json, "idle_time_limit").unwrap() - 2.5).abs() < 0.000_001);
        assert_eq!(parse_json_float(json, "nonexistent"), None);
    }

    #[test]
    fn parse_json_float_scientific() {
        let json = r#"{"value": 1.5e10}"#;
        assert!((parse_json_float(json, "value").unwrap() - 1.5e10).abs() < 1.0);
    }

    #[test]
    fn parse_json_string_basic() {
        let json = r#"{"command": "/bin/bash", "title": "My Recording"}"#;
        assert_eq!(
            parse_json_string(json, "command"),
            Some("/bin/bash".to_string())
        );
        assert_eq!(
            parse_json_string(json, "title"),
            Some("My Recording".to_string())
        );
        assert_eq!(parse_json_string(json, "nonexistent"), None);
    }

    #[test]
    fn parse_json_string_escaped() {
        let json = r#"{"path": "C:\\Users\\test", "msg": "say \"hello\""}"#;
        assert_eq!(
            parse_json_string(json, "path"),
            Some("C:\\Users\\test".to_string())
        );
        assert_eq!(
            parse_json_string(json, "msg"),
            Some("say \"hello\"".to_string())
        );
    }

    #[test]
    fn parse_json_object_basic() {
        let json = r#"{"env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}"#;
        let env = parse_json_object(json, "env").unwrap();
        assert_eq!(env.get("SHELL"), Some(&"/bin/bash".to_string()));
        assert_eq!(env.get("TERM"), Some(&"xterm-256color".to_string()));
    }

    #[test]
    fn parse_json_object_empty() {
        let json = r#"{"env": {}}"#;
        let env = parse_json_object(json, "env").unwrap();
        assert!(env.is_empty());
    }

    #[test]
    fn parse_header_full() {
        let header_json = r#"{"version": 2, "width": 120, "height": 40, "timestamp": 1704067200, "duration": 60.5, "idle_time_limit": 2.0, "command": "/bin/zsh", "title": "Demo", "env": {"SHELL": "/bin/zsh"}}"#;
        let header = parse_header(header_json);

        assert_eq!(header.version, 2);
        assert_eq!(header.width, 120);
        assert_eq!(header.height, 40);
        assert_eq!(header.timestamp, Some(1_704_067_200));
        assert!((header.duration.unwrap() - 60.5).abs() < 0.001);
        assert!((header.idle_time_limit.unwrap() - 2.0).abs() < 0.001);
        assert_eq!(header.command, Some("/bin/zsh".to_string()));
        assert_eq!(header.title, Some("Demo".to_string()));
        assert_eq!(header.env.get("SHELL"), Some(&"/bin/zsh".to_string()));
    }

    #[test]
    fn parse_header_minimal() {
        let header_json = r#"{"version": 2, "width": 80, "height": 24}"#;
        let header = parse_header(header_json);

        assert_eq!(header.version, 2);
        assert_eq!(header.width, 80);
        assert_eq!(header.height, 24);
        assert_eq!(header.timestamp, None);
        assert_eq!(header.duration, None);
        assert_eq!(header.command, None);
        assert!(header.env.is_empty());
    }

    #[test]
    fn unescape_json_sequences() {
        assert_eq!(unescape_json("hello\\nworld"), "hello\nworld");
        assert_eq!(unescape_json("tab\\there"), "tab\there");
        assert_eq!(unescape_json("quote\\\"here"), "quote\"here");
        assert_eq!(unescape_json("back\\\\slash"), "back\\slash");
        assert_eq!(unescape_json("return\\rhere"), "return\rhere");
    }

    #[test]
    fn unescape_json_backspace_formfeed() {
        assert_eq!(unescape_json("back\\bspace"), "back\u{0008}space");
        assert_eq!(unescape_json("form\\ffeed"), "form\u{000C}feed");
    }

    #[test]
    fn unescape_json_forward_slash() {
        // Forward slash can be escaped but doesn't need to be
        assert_eq!(unescape_json("path\\/to\\/file"), "path/to/file");
        assert_eq!(unescape_json("path/to/file"), "path/to/file");
    }

    #[test]
    fn unescape_json_unicode() {
        // Basic ASCII via unicode escape
        assert_eq!(unescape_json("\\u0041"), "A");
        assert_eq!(unescape_json("\\u0048\\u0069"), "Hi");

        // Control characters
        assert_eq!(unescape_json("\\u001b"), "\u{001b}"); // ESC
        assert_eq!(unescape_json("\\u0000"), "\u{0000}"); // NULL

        // Non-ASCII unicode
        assert_eq!(unescape_json("\\u00e9"), "é");
        assert_eq!(unescape_json("\\u4e2d\\u6587"), "中文");

        // Mixed content
        assert_eq!(unescape_json("hello\\u0020world"), "hello world");
        assert_eq!(unescape_json("\\u0041\\u0042\\u0043"), "ABC");
    }

    #[test]
    fn unescape_json_unicode_invalid() {
        // Invalid: not enough hex digits
        assert_eq!(unescape_json("\\u00"), "\\u00");
        assert_eq!(unescape_json("\\u0"), "\\u0");
        assert_eq!(unescape_json("\\u"), "\\u");

        // Invalid: non-hex characters
        assert_eq!(unescape_json("\\u00GH"), "\\u00GH");
    }

    #[test]
    fn unescape_json_mixed_escapes() {
        // Combine various escape types
        assert_eq!(
            unescape_json("line1\\nline2\\ttab\\u0021"),
            "line1\nline2\ttab!"
        );
        assert_eq!(
            unescape_json("\\\"quoted\\\" and \\u003Ctag\\u003E"),
            "\"quoted\" and <tag>"
        );
    }

    #[test]
    fn escape_json_control_chars() {
        // Control characters should be escaped as \uXXXX
        assert_eq!(escape_json("\u{001b}"), "\\u001b"); // ESC
        assert_eq!(escape_json("\u{0007}"), "\\u0007"); // BEL
    }

    #[test]
    fn roundtrip_with_metadata() {
        let mut metadata = TranscriptMetadata::new(120, 40);
        metadata.command = Some("/bin/bash".to_string());
        metadata.title = Some("Test Recording".to_string());
        metadata.timestamp = Some(1_704_067_200);
        metadata.duration = Some(Duration::from_secs_f64(30.5));
        metadata
            .env
            .insert("SHELL".to_string(), "/bin/bash".to_string());
        metadata.env.insert("TERM".to_string(), "xterm".to_string());

        let mut transcript = Transcript::new(metadata);
        transcript.push(TranscriptEvent::output(Duration::from_millis(100), b"$ "));
        transcript.push(TranscriptEvent::input(Duration::from_millis(200), b"ls\n"));
        transcript.push(TranscriptEvent::output(
            Duration::from_millis(300),
            b"file1.txt\nfile2.txt\n",
        ));

        let mut buf = Vec::new();
        write_asciicast(&mut buf, &transcript).unwrap();

        let parsed = read_asciicast(buf.as_slice()).unwrap();
        assert_eq!(parsed.metadata.width, 120);
        assert_eq!(parsed.metadata.height, 40);
        assert_eq!(parsed.metadata.command, Some("/bin/bash".to_string()));
        assert_eq!(parsed.metadata.title, Some("Test Recording".to_string()));
        assert_eq!(parsed.metadata.timestamp, Some(1_704_067_200));
        assert_eq!(parsed.events.len(), 3);
    }
}