use crate::markup::{MarkupAttributeMarker, MarkupValue};
use crate::prelude::*;
use bevy_platform::collections::HashMap;
use core::fmt::Display;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "bevy", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bevy", reflect(Debug, PartialEq))]
#[cfg_attr(
all(feature = "bevy", feature = "serde"),
reflect(Serialize, Deserialize)
)]
pub struct MarkupAttribute {
pub name: String,
pub position: usize,
pub length: usize,
pub properties: HashMap<String, MarkupValue>,
pub source_position: usize,
}
impl MarkupAttribute {
pub(crate) fn from_marker(marker: MarkupAttributeMarker, length: usize) -> Self {
Self {
name: marker.name.unwrap(),
position: marker.position,
length,
properties: marker.properties,
source_position: marker.source_position,
}
}
pub fn property(&self, name: &str) -> Option<&MarkupValue> {
self.properties.get(name)
}
}
impl Display for MarkupAttribute {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let properties = if !self.properties.is_empty() {
format!(", {} properties", self.properties.len())
} else {
String::new()
};
write!(
f,
"[{name}] - {start}-{end} ({length}{properties})",
name = self.name,
start = self.position,
end = self.position + self.length,
length = self.length
)
}
}