artichoke_readline/
lib.rs

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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::cargo)]
#![allow(clippy::manual_let_else)]
#![allow(clippy::multiple_crate_versions)]
#![allow(clippy::question_mark)] // https://github.com/rust-lang/rust-clippy/issues/8281
#![allow(unknown_lints)]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![warn(missing_copy_implementations)]
#![warn(rust_2018_idioms)]
#![warn(rust_2021_compatibility)]
#![warn(trivial_casts, trivial_numeric_casts)]
#![warn(unused_qualifications)]
#![warn(variant_size_differences)]
#![forbid(unsafe_code)]
// Enable feature callouts in generated documentation:
// https://doc.rust-lang.org/beta/unstable-book/language-features/doc-cfg.html
//
// This approach is borrowed from tokio.
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_alias))]

//! Helpers for integrating REPLs with GNU Readline.
//!
//! This crate can be used to parse Readline editing mode from the standard set
//! of GNU Readline config files.
//!
//! # Examples
//!
//! ```
//! use artichoke_readline::{get_readline_edit_mode, rl_read_init_file, EditMode};
//!
//! if let Some(config) = rl_read_init_file() {
//!     if matches!(get_readline_edit_mode(config), Some(EditMode::Vi)) {
//!         println!("You have chosen wisely");
//!     }
//! }
//! ```
//!
//! # Crate Features
//!
//! The **rustyline** feature (enabled by default) adds trait implementations to
//! allow [`EditMode`] to interoperate with the corresponding enum in the
//! `rustyline` crate.

// Ensure code blocks in `README.md` compile
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme {}

use std::env;
use std::fs;
use std::path::PathBuf;

use bstr::ByteSlice;

/// Readline editing mode.
#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum EditMode {
    /// Emacs keymap.
    ///
    /// Emacs is the default keymap.
    #[default]
    Emacs,
    /// Vi keymap.
    Vi,
}

#[cfg(feature = "rustyline")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustyline")))]
impl From<EditMode> for rustyline::config::EditMode {
    fn from(mode: EditMode) -> Self {
        match mode {
            EditMode::Emacs => Self::Emacs,
            EditMode::Vi => Self::Vi,
        }
    }
}

/// Read inputrc contents according to the GNU Readline hierarchy of config
/// files.
///
/// This function will try each file in the config hierarchy (with the addition
/// of `%USERPROFILE%\_inputrc` on Windows). This function returns the contents
/// of the first file that exists and is successfully read. If no config file is
/// found, [`None`] is returned.
///
/// # Upstream Implementation
///
/// This routine is ported from GNU Readline's `rl_read_init_file` function as
/// of commit [`7274faabe97ce53d6b464272d7e6ab6c1392837b`][upstream], which has
/// the following documentation:
///
/// > Do key bindings from a file. If FILENAME is NULL it defaults to the first
/// > non-null filename from this list:
/// >
/// > 1. the filename used for the previous call
/// > 2. the value of the shell variable `INPUTRC`
/// > 3. ~/.inputrc
/// > 4. /etc/inputrc
/// >
/// > If the file existed and could be opened and read, 0 is returned, otherwise
/// > errno is returned.
///
/// The routine is also documented in [section 8.3 of the bash manual][readline-man].
///
/// [upstream]: https://git.savannah.gnu.org/cgit/readline.git/tree/bind.c?h=7274faabe97ce53d6b464272d7e6ab6c1392837b#n1032
/// [readline-man]: https://www.gnu.org/software/bash/manual/bash.html#Readline-Init-File
#[must_use]
pub fn rl_read_init_file() -> Option<Vec<u8>> {
    if let Some(inputrc) = env::var_os("INPUTRC") {
        return fs::read(inputrc).ok();
    }

    let home_dir = home_dir();
    if let Some(ref home_dir) = home_dir {
        let inputrc = home_dir.join(".inputrc");
        if let Ok(content) = fs::read(inputrc) {
            return Some(content);
        }
    }

    if let Ok(content) = fs::read("/etc/inputrc") {
        return Some(content);
    }

    if cfg!(windows) {
        if let Some(home_dir) = home_dir {
            let inputrc = home_dir.join("_inputrc");
            if let Ok(content) = fs::read(inputrc) {
                return Some(content);
            }
        }
    }

    None
}

#[cfg(not(any(unix, windows)))]
fn home_dir() -> Option<PathBuf> {
    None
}

#[cfg(unix)]
fn home_dir() -> Option<PathBuf> {
    // `std::env::home_dir` does not have problematic behavior on `unix`
    // targets, which includes all apple target OSes and Redox. Per the docs:
    //
    // > Deprecated since 1.29.0: This function's behavior may be unexpected on
    // > Windows. Consider using a crate from crates.io instead.
    // >
    // > -- https://doc.rust-lang.org/1.69.0/std/env/fn.home_dir.html
    //
    // Additionally, the `home` crate on crates.io, which is owned by the
    // @rust-lang organization and used in Rustup and Cargo, uses `std::env::home_dir`
    // to implement `home::home_dir` on `unix` and `target_os = "redox"` targets:
    //
    // https://docs.rs/home/0.5.5/src/home/lib.rs.html#71-75
    #[allow(deprecated)]
    env::home_dir()
}

#[cfg(windows)]
fn home_dir() -> Option<PathBuf> {
    use known_folders::{get_known_folder_path, KnownFolder};

    get_known_folder_path(KnownFolder::Profile)
}

/// Parse readline editing mode from the given byte content, which should be
/// the contents of an inputrc config file.
///
/// See [`rl_read_init_file`] for how to retrieve the contents of the effective
/// inputrc file.
///
/// This function looks for the `editing-mode` variable in the given inputrc
/// bytes. Per the [manual, section 8.3.1]:
///
/// > `editing-mode`
/// >
/// > The `editing-mode` variable controls which default set of key bindings is
/// > used. By default, Readline starts up in Emacs editing mode, where the
/// > keystrokes are most similar to Emacs. This variable can be set to either
/// > '`emacs`' or '`vi`'.
///
/// # Examples
///
/// ```
/// use artichoke_readline::{get_readline_edit_mode, EditMode};
///
/// const INPUTRC: &str = "
/// # Vi mode
/// set editing-mode vi
/// ";
///
/// assert_eq!(get_readline_edit_mode(INPUTRC), Some(EditMode::Vi));
/// ```
///
/// # Implementation Notes
///
/// This parser does not support GNU Readline's [conditional init constructs]
/// (the `$if` construct).
///
/// [manual]: https://www.gnu.org/software/bash/manual/bash.html#Readline-Init-File-Syntax
/// [conditional init constructs]: https://www.gnu.org/software/bash/manual/bash.html#Conditional-Init-Constructs
#[must_use]
pub fn get_readline_edit_mode(contents: impl AsRef<[u8]>) -> Option<EditMode> {
    fn inner(contents: &[u8]) -> Option<EditMode> {
        let mut edit_mode = None; // Stores the last encountered editing mode

        for line in contents.lines() {
            // Skip leading whitespace.
            let line = trim_whitespace_front(line);

            // If the line is not a comment, then parse it.
            if matches!(line.first(), Some(b'#') | None) {
                continue;
            }

            // If this is a command to set a variable, then do that.
            let line = match line.strip_prefix(b"set") {
                Some(rest) => rest,
                None => continue,
            };
            // Skip leading whitespace.
            let line = trim_whitespace_front(line);

            // Per the manual, section 8.3.1:
            // https://www.gnu.org/software/bash/manual/bash.html#Readline-Init-File-Syntax
            //
            // > Variable names and values, where appropriate, are recognized
            // > without regard to case. Unrecognized variable names are ignored.
            //
            // In this case `editing-mode` is a variable name.
            let line = match line {
                [b'e' | b'E', b'd' | b'D', b'i' | b'I', b't' | b'T', b'i' | b'I', b'n' | b'N', b'g' | b'G', b'-', b'm' | b'M', b'o' | b'O', b'd' | b'D', b'e' | b'E', rest @ ..] => {
                    rest
                }
                _ => continue,
            };
            // Skip leading whitespace.
            let line = trim_whitespace_front(line);

            // Per the manual, section 8.3.1:
            // https://www.gnu.org/software/bash/manual/bash.html#Readline-Init-File-Syntax
            //
            // > Variable names and values, where appropriate, are recognized
            // > without regard to case. Unrecognized variable names are ignored.
            //
            // The values 'vi' and 'emacs' in the 'set' directive are case
            // insensitive as they are the variable value.
            match line {
                [b'v' | b'V', b'i' | b'I'] => {
                    // Last occurrence of editing mode directive takes effect.
                    edit_mode = Some(EditMode::Vi);
                }
                [b'e' | b'E', b'm' | b'M', b'a' | b'A', b'c' | b'C', b's' | b'S'] => {
                    // Last occurrence of editing mode directive takes effect.
                    edit_mode = Some(EditMode::Emacs);
                }
                [b'v' | b'V', b'i' | b'I', next, ..] if posix_space::is_space(*next) => {
                    // Last occurrence of editing mode directive takes effect.
                    edit_mode = Some(EditMode::Vi);
                }
                [b'e' | b'E', b'm' | b'M', b'a' | b'A', b'c' | b'C', b's' | b'S', next, ..]
                    if posix_space::is_space(*next) =>
                {
                    // Last occurrence of editing mode directive takes effect.
                    edit_mode = Some(EditMode::Emacs);
                }
                // Ignore unrecognized or invalid lines.
                // Lines without a valid editing mode directive are skipped.
                _ => {}
            }
        }

        edit_mode
    }

    inner(contents.as_ref())
}

/// Skip leading POSIX whitespace.
fn trim_whitespace_front(mut s: &[u8]) -> &[u8] {
    loop {
        if let Some((&head, tail)) = s.split_first() {
            if posix_space::is_space(head) {
                s = tail;
                continue;
            }
        }
        break s;
    }
}

#[cfg(test)]
mod tests {
    use super::{get_readline_edit_mode, EditMode};

    #[test]
    fn test_default_edit_mode_is_emacs() {
        assert_eq!(EditMode::default(), EditMode::Emacs);
    }

    #[test]
    #[cfg(feature = "rustyline")]
    fn test_edit_mode_rustyline_into() {
        assert_eq!(rustyline::config::EditMode::Emacs, EditMode::Emacs.into());
        assert_eq!(rustyline::config::EditMode::Vi, EditMode::Vi.into());
    }

    #[test]
    fn test_get_readline_edit_mode_vi_mode() {
        let config = "set editing-mode vi";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_emacs_mode() {
        let config = "set editing-mode emacs";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Emacs));
    }

    #[test]
    fn test_get_readline_edit_mode_parse_empty_and_blank_lines() {
        let test_cases = [
            "",
            "              ",
            "\t\t\t",
            "          \n              ",
            "\n",
            "\r\n",
            "              \r\n           ",
        ];
        for contents in test_cases {
            assert_eq!(get_readline_edit_mode(contents), None);
        }
    }

    #[test]
    fn test_get_readline_edit_mode_whitespace_only_lines() {
        let contents = "
            \t
            \n
            \r
        ";
        assert_eq!(get_readline_edit_mode(contents), None);
    }

    #[test]
    fn test_get_readline_edit_mode_empty_contents() {
        let contents = "";
        assert_eq!(get_readline_edit_mode(contents), None);
    }

    #[test]
    fn test_get_readline_edit_mode_no_set_directive() {
        let contents = "editing-mode vi";
        assert_eq!(get_readline_edit_mode(contents), None);
    }

    #[test]
    fn test_get_readline_edit_mode_comment_lines() {
        let contents = "
            # This is a comment
            # set editing-mode vi
            # set editing-mode emacs
        ";
        assert_eq!(get_readline_edit_mode(contents), None);
    }

    #[test]
    fn test_get_readline_edit_mode_set_editing_mode_with_space_before_variable_name() {
        let test_cases = [
            ("set     editing-mode vi", EditMode::Vi),
            ("set     editing-mode emacs", EditMode::Emacs),
        ];

        for (config, expected_mode) in test_cases {
            assert_eq!(get_readline_edit_mode(config), Some(expected_mode));
        }
    }

    #[test]
    fn test_get_readline_edit_mode_set_editing_mode_with_space_after_variable_name() {
        let test_cases = [
            ("set editing-mode    vi", EditMode::Vi),
            ("set editing-mode    emacs", EditMode::Emacs),
        ];

        for (config, expected_mode) in test_cases {
            assert_eq!(get_readline_edit_mode(config), Some(expected_mode));
        }
    }

    #[test]
    fn test_get_readline_edit_mode_excess_whitespace() {
        let test_cases = [
            ("set editing-mode  \t vi  \t \r\n", EditMode::Vi),
            ("set editing-mode  \t emacs  \t \r\n", EditMode::Emacs),
            ("set editing-mode   vi  \t \n", EditMode::Vi),
            ("set editing-mode   emacs  \t \n", EditMode::Emacs),
        ];

        for (config, expected_mode) in test_cases {
            assert_eq!(get_readline_edit_mode(config), Some(expected_mode));
        }
    }

    #[test]
    fn test_get_readline_edit_mode_ignore_trailing_garbage() {
        let test_cases = [
            ("set editing-mode vi 1234", EditMode::Vi),
            ("set editing-mode emacs 1234", EditMode::Emacs),
            ("set editing-mode vi this-is-extra-content", EditMode::Vi),
            ("set editing-mode emacs this-is-extra-content", EditMode::Emacs),
        ];

        for (config, expected_mode) in test_cases {
            assert_eq!(get_readline_edit_mode(config), Some(expected_mode));
        }
    }

    #[test]
    fn test_get_readline_edit_mode_requires_lowercase_set() {
        let test_cases = [
            "SET editing-mode vi",
            "SET editing-mode emacs",
            "Set editing-mode vi",
            "Set editing-mode emacs",
            "sET editing-mode vi",
            "sET editing-mode emacs",
        ];

        for config in test_cases {
            assert_eq!(get_readline_edit_mode(config), None);
        }
    }

    #[test]
    fn test_get_readline_editing_mode_variable_name_case_insensitive() {
        let test_cases = [
            // Lowercase
            ("set editing-mode vi", EditMode::Vi),
            ("set editing-mode emacs", EditMode::Emacs),
            // Uppercase
            ("set EDITING-MODE emacs", EditMode::Emacs),
            ("set EDITING-MODE vi", EditMode::Vi),
            // Mixed case
            ("set Editing-Mode vi", EditMode::Vi),
            ("set Editing-Mode emacs", EditMode::Emacs),
            ("set EdItInG-MoDe vi", EditMode::Vi),
            ("set EdItInG-MoDe emacs", EditMode::Emacs),
            ("set eDiTiNg-mOdE vi", EditMode::Vi),
            ("set eDiTiNg-mOdE emacs", EditMode::Emacs),
        ];

        for (config, expected_mode) in test_cases {
            assert_eq!(get_readline_edit_mode(config), Some(expected_mode));
        }
    }

    #[test]
    fn test_get_readline_editing_mode_variable_value_case_insensitive() {
        let test_cases = [
            // Lowercase
            ("set editing-mode vi", EditMode::Vi),
            ("set editing-mode emacs", EditMode::Emacs),
            // Uppercase
            ("set editing-mode VI", EditMode::Vi),
            ("set editing-mode EMACS", EditMode::Emacs),
            // Mixed case
            ("set editing-mode Vi", EditMode::Vi),
            ("set editing-mode vI", EditMode::Vi),
            ("set editing-mode eMaCs", EditMode::Emacs),
            ("set editing-mode EmAcS", EditMode::Emacs),
            ("set editing-mode emACS", EditMode::Emacs),
            ("set editing-mode EMacs", EditMode::Emacs),
        ];

        for (config, expected_mode) in test_cases {
            assert_eq!(get_readline_edit_mode(config), Some(expected_mode));
        }
    }

    #[test]
    fn test_get_readline_editing_mode_ignores_unrecognized_variable_names() {
        // Test handling unrecognized variable names
        let input = "set unknown-variable foo";
        assert_eq!(get_readline_edit_mode(input), None);
    }

    #[test]
    fn test_get_readline_edit_mode_multiple_lines_with_comments() {
        let contents = "
            # This is a comment
            set some-other-setting 123

            # Another comment
            set editing-mode vi

            # One more comment
            set another-setting true
        ";
        assert_eq!(get_readline_edit_mode(contents), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_no_mode_directive() {
        let config = "set blink-matching-paren on\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_multiple_lines() {
        let config = "set editing-mode vi\nset blink-matching-paren on\n";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_variable_value() {
        let config = "set editing-mode vim\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_characters_variable_value() {
        let config = "set editing-mode vī\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_with_posix_spaces() {
        let test_cases = [
            ("set editing-mode     vi", EditMode::Vi),
            ("set editing-mode     emacs", EditMode::Emacs),
            ("set editing-mode\tvi", EditMode::Vi),
            ("set editing-mode\temacs", EditMode::Emacs),
            ("set editing-mode     \t \tvi", EditMode::Vi),
            ("set editing-mode     \t \temacs", EditMode::Emacs),
            ("set editing-mode\t\t\t\t\tvi", EditMode::Vi),
            ("set editing-mode\t\t\t\t\temacs", EditMode::Emacs),
        ];

        for (config, expected_mode) in test_cases {
            assert_eq!(get_readline_edit_mode(config), Some(expected_mode));
        }
    }

    #[test]
    fn test_get_readline_edit_mode_vi_mode_with_multibyte_utf8() {
        let config = "set editing-mode vī\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_emacs_mode_with_multibyte_utf8() {
        let config = "set editing-mode eĦmacs\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_vi_mode_with_trailing_invalid_utf8() {
        let config = b"set editing-mode vi \x80\n";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_vi_mode() {
        let config = b"set editing-mode v\xFFi\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_emacs_mode() {
        let config = b"set editing-mode e\xEFmacs\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_vi_mode_with_trailing_content() {
        let config = b"set editing-mode vi \xFF\xFF\xFF\n";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_emacs_mode_with_trailing_content() {
        let config = b"set editing-mode emacs this-\x80is-extra-content\n";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Emacs));
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_multiple_lines() {
        let config = b"set editing-mode vi\nset blink-matching-paren \xC0\x80on\n";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_emacs_mode_excess_whitespace() {
        let config = b"set editing-mode  \x80emacs  \t \n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_vi_mode_excess_whitespace() {
        let config = b"set editing-mode  \x80vi  \t \r\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_no_mode_directive() {
        let config = b"set blink-matching-\x80paren on\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_invalid_directive() {
        let config = b"set editing-\x80mode vim\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_emacs_mode_with_posix_spaces() {
        let config = b"set editing-mode     e\xEFmacs\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_vi_mode_with_posix_spaces() {
        let config = b"set editing-\x80mode\tvi\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_emacs_mode_with_multiple_posix_spaces() {
        let config = b"set editing-mode     \t \nem\xF4cs\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_invalid_utf8_bytes_vi_mode_with_multiple_posix_spaces() {
        let config = b"set editing-\xF4mode     \t \nvi\n";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_quotes() {
        let test_cases = [
            // Test cases for single quotes
            "set editing-mode 'emacs'",
            "set editing-mode 'vi'",
            // Test cases for double quotes
            "set editing-mode \"emacs\"",
            "set editing-mode \"vi\"",
            // Test cases for mixed quotes
            "set editing-mode 'emacs\"",
            "set editing-mode 'vi\"",
        ];

        for config in test_cases {
            assert_eq!(get_readline_edit_mode(config), None);
        }
    }

    #[test]
    fn test_get_readline_edit_mode_last_set_directive_vi() {
        let contents = "
            set editing-mode emacs
            set editing-mode vi
        ";
        assert_eq!(get_readline_edit_mode(contents), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_last_set_directive_emacs() {
        let contents = "
            set editing-mode vi
            set editing-mode emacs
        ";
        assert_eq!(get_readline_edit_mode(contents), Some(EditMode::Emacs));
    }

    #[test]
    fn test_get_readline_edit_mode_last_set_directive_vi_with_whitespace() {
        let contents = "
            set editing-mode emacs
            set editing-mode   vi
        ";
        assert_eq!(get_readline_edit_mode(contents), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_last_set_directive_emacs_with_whitespace() {
        let contents = "
            set editing-mode vi
            set editing-mode    emacs
        ";
        assert_eq!(get_readline_edit_mode(contents), Some(EditMode::Emacs));
    }

    #[test]
    fn test_get_readline_edit_mode_multiple_set_directives_mixed() {
        let contents = "
            set some-other-setting 123

            set editing-mode vi

            set another-setting true

            set editing-mode emacs

            set extra-setting abc
            set extra-setting xyz

            set editing-mode vi
        ";
        assert_eq!(get_readline_edit_mode(contents), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_integration_1() {
        let config = "
            set blink-matching-paren on
            set keymap vi-command
            set editing-mode emacs
            set completion-ignore-case on
        ";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Emacs));
    }

    #[test]
    fn test_get_readline_edit_mode_integration_2() {
        let config = "
            set blink-matching-paren on
            set editing-mode vi
            set completion-ignore-case on
            set keymap vi-command
        ";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Vi));
    }

    #[test]
    fn test_get_readline_edit_mode_integration_3() {
        let config = "
            set blink-matching-paren on
            set completion-ignore-case on
            set editing-mode emacs
            set keymap vi-command
        ";
        assert_eq!(get_readline_edit_mode(config), Some(EditMode::Emacs));
    }

    #[test]
    fn test_get_readline_edit_mode_integration_4() {
        let config = "
            set blink-matching-paren on
            set keymap vi-command
            set completion-ignore-case on
        ";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_integration_5() {
        let config = "
            set blink-matching-paren on
            set completion-ignore-case on
            set keymap vi-command
        ";
        assert_eq!(get_readline_edit_mode(config), None);
    }

    #[test]
    fn test_get_readline_edit_mode_integration_6() {
        let config = "
            set blink-matching-paren on
            set keymap vi-command
        ";
        assert_eq!(get_readline_edit_mode(config), None);
    }
}