rl_lang/repl/utils.rs
1//! Small shared utilities for the REPL.
2
3use crate::repl::lines_types::OutputLine;
4
5/// Appends an [`OutputLine::Error`] derived from `e` to `output`.
6pub fn push_error(output: &mut Vec<OutputLine>, e: &crate::utils::errors::Error) {
7 output.push(OutputLine::Error(format!("error: {}", e.message())));
8}
9
10/// Converts a char-indexed position in `s` to its byte offset.
11///
12/// Returns `s.len()` if `char_idx` is past the end, making it safe to use
13/// directly with [`String::insert`] and [`String::remove`].
14pub fn char_to_byte(s: &str, char_idx: usize) -> usize {
15 s.char_indices()
16 .nth(char_idx)
17 .map(|(b, _)| b)
18 .unwrap_or(s.len())
19}