use std::fmt;
use std::ops::Deref;
use crate::color::RgbColor;
use crate::element::Element;
use crate::node::LineTagDefinition;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct LineTag<'a> {
pub properties: &'a LineTagProperties,
pub element: Option<&'a Element>,
}
impl Deref for LineTag<'_> {
type Target = LineTagProperties;
fn deref(&self) -> &Self::Target {
self.properties
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LineTagProperties {
pub window: Option<String>,
pub fore: Option<RgbColor>,
pub back: Option<RgbColor>,
pub gag: bool,
pub enable: bool,
}
impl LineTagProperties {
pub(crate) fn apply(&mut self, definition: LineTagDefinition) {
if let Some(enable) = definition.enable {
self.enable = enable;
}
if let Some(fore) = definition.fore {
self.fore = Some(fore);
}
if let Some(back) = definition.back {
self.back = Some(back);
}
if let Some(gag) = definition.gag {
self.gag = gag;
}
if let Some(window) = definition.window {
self.window = Some(window.to_owned());
}
}
}
impl fmt::Display for LineTagProperties {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use crate::display::{DelimAfterFirst, Escape};
let Self {
window,
fore,
back,
gag,
enable,
} = self;
let delim = DelimAfterFirst::new(" ");
if let Some(window) = window {
write!(f, "{delim}WINDOWNAME={}", Escape(window))?;
}
if let Some(fore) = fore {
write!(f, "{delim}FORE={fore}")?;
}
if let Some(back) = back {
write!(f, "{delim}BACK={back}")?;
}
if *gag {
write!(f, "{delim}GAG")?;
}
if !*enable {
write!(f, "{delim}DISABLE")?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fmt() {
let properties = LineTagProperties {
window: Some("_top".into()),
fore: Some(RgbColor::hex(0x123456)),
back: Some(RgbColor::hex(0x789abc)),
gag: true,
enable: false,
};
assert_eq!(
properties.to_string(),
"WINDOWNAME=\"_top\" FORE=#123456 BACK=#789abc GAG DISABLE"
);
}
}