I've currently got the following code, which does work:
use colored::{ColoredString, Colorize};
#[repr(u8)]
#[derive(Copy, Clone, PartialEq, Debug)]
enum BubbleColor {
None = 0,
Red = 1,
Green = 2,
Blue = 3,
Yellow = 4,
Purple = 5,
Orange = 6,
Pink = 7,
}
impl From<u8> for BubbleColor {
fn from(value: u8) -> Self {
match value {
1 => BubbleColor::Red,
2 => BubbleColor::Green,
3 => BubbleColor::Blue,
4 => BubbleColor::Yellow,
5 => BubbleColor::Purple,
6 => BubbleColor::Orange,
7 => BubbleColor::Pink,
_ => BubbleColor::None,
}
}
}
impl BubbleColor {
fn to_colored_string(&self) -> ColoredString {
match self {
BubbleColor::None => " ".bold().white(),
BubbleColor::Red => "R".bold().red(),
BubbleColor::Green => "G".bold().green(),
BubbleColor::Blue => "B".bold().blue(),
BubbleColor::Yellow => "Y".bold().yellow(),
BubbleColor::Purple => "P".bold().magenta(),
BubbleColor::Orange => "O".bold().truecolor(255, 165, 0), // Orange color
BubbleColor::Pink => "K".bold().truecolor(255, 192, 203), // Pink color
}
}
}
But it feels a bit unsatisfying, because I come from Python and from that perspective this looks clunky and slow.
I'm also working with copilot, and I've tried 'correcting' this code a couple of time, but copilot keeps changing it back.
So I'm wondering, am I wrong? Is this actually a good pattern in Rust?
(I'm building a game with the intention to train AI on it, hence my caring about performance more than I usually would.)
((My natural inclination is that the enum is stored as a u8 under the hood, so converting to/from u8 should be trivial. And that the options of ColoredString should be stored in an array, with index-lookup being used to map from BubbleColor to ColoredString.))