use std::fmt;
use std::str::FromStr;
use crate::arguments::{ArgumentScanner, ExpectArg, FromArgs};
use crate::color::RgbColor;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Color {
pub fore: Option<RgbColor>,
pub back: Option<RgbColor>,
}
impl<'a, S: AsRef<str>> FromArgs<'a, S> for Color {
fn from_args<A: ArgumentScanner<'a, Decoded = S>>(mut scanner: A) -> crate::Result<Self> {
let fore = scanner.get_next_or("fore")?.expect_color()?;
let back = scanner.get_next_or("back")?.expect_color()?;
scanner.expect_end()?;
Ok(Self { fore, back })
}
}
impl From<RgbColor> for Color {
fn from(fore: RgbColor) -> Self {
Self {
fore: Some(fore),
..Default::default()
}
}
}
impl<'a> TryFrom<&'a str> for Color {
type Error = crate::parse::FromStrError;
#[inline]
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
crate::parse::parse_element(s)
}
}
impl FromStr for Color {
type Err = crate::parse::FromStrError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
crate::parse::parse_element::<Color>(s)
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Color { fore, back } = self;
crate::display::ElementFormatter {
name: "COLOR",
arguments: &[fore, back],
keywords: &[],
}
.fmt(f)
}
}