rsql_repl 0.20.0

rsql library for creating a REPL command line SQL interface
Documentation
use crate::shell::helper::ReplHelper;
use rustyline::highlight::{CmdKind, Highlighter};
use std::borrow::Cow;

impl Highlighter for ReplHelper {
    fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
        self.highlighter
            .highlight(line)
            .unwrap_or(Cow::Borrowed(line))
    }

    fn highlight_char(&self, line: &str, pos: usize, _kind: CmdKind) -> bool {
        let _ = (line, pos);
        true
    }
}

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

    #[test]
    fn test_highlight_color_disabled() {
        let configuration = Configuration {
            color: false,
            ..Default::default()
        };
        let helper = ReplHelper::new(&configuration);
        let line = "SELECT";
        let highlighted = helper.highlight(line, 0);
        assert!(highlighted.contains(line));
    }

    #[test]
    fn test_highlight_color_forced() {
        let configuration = Configuration {
            color: true,
            ..Default::default()
        };
        let helper = ReplHelper::new(&configuration);
        let line = "SELECT";
        let highlighted = helper.highlight(line, 0);
        assert!(highlighted.contains(line));
    }

    #[test]
    fn test_highlight_char() {
        let configuration = Configuration::default();
        let helper = ReplHelper::new(&configuration);
        let line = "SELECT";
        let highlighted = helper.highlight_char(line, 0, CmdKind::ForcedRefresh);
        assert!(highlighted);
    }
}