use crate::config::RColorConfig;
use nu_ansi_term::{Color, Style};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenType {
Comment,
String,
Number,
Keyword,
Constant,
Operator,
Punctuation,
Identifier,
Whitespace,
Other,
}
impl TokenType {
pub fn style(self, config: &RColorConfig) -> Style {
match self {
TokenType::Comment => color_to_style(config.comment),
TokenType::String => color_to_style(config.string),
TokenType::Number => color_to_style(config.number),
TokenType::Keyword => color_to_style(config.keyword),
TokenType::Constant => color_to_style(config.constant),
TokenType::Operator => color_to_style(config.operator),
TokenType::Punctuation => color_to_style(config.punctuation),
TokenType::Identifier => color_to_style(config.identifier),
TokenType::Whitespace => Style::new(),
TokenType::Other => Style::new(),
}
}
}
fn color_to_style(color: Color) -> Style {
match color {
Color::Default => Style::new(),
c => Style::new().fg(c),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_style_uses_configured_color() {
let config = RColorConfig {
comment: Color::Green,
string: Color::Default,
..RColorConfig::default()
};
assert_eq!(
TokenType::Comment.style(&config),
Style::new().fg(Color::Green)
);
assert_eq!(TokenType::String.style(&config), Style::new());
assert_eq!(TokenType::Whitespace.style(&config), Style::new());
assert_eq!(TokenType::Other.style(&config), Style::new());
}
}