use std::collections::HashMap;
use xml::attribute::OwnedAttribute;
use crate::{
error::Error,
properties::{parse_properties, Color, Properties},
util::{get_attrs, parse_tag, XmlEventResult},
Result, TileId,
};
#[derive(Debug, PartialEq, Clone)]
pub struct WangColor {
pub name: String,
#[allow(missing_docs)]
pub color: Color,
pub tile: Option<TileId>,
pub probability: f32,
pub properties: Properties,
}
impl WangColor {
pub fn new(
parser: &mut impl Iterator<Item = XmlEventResult>,
attrs: Vec<OwnedAttribute>,
) -> Result<WangColor> {
let (name, color, tile, probability) = get_attrs!(
for v in attrs {
"name" => name ?= v.parse::<String>(),
"color" => color ?= v.parse(),
"tile" => tile ?= v.parse::<i64>(),
"probability" => probability ?= v.parse::<f32>(),
}
(name, color, tile, probability)
);
let tile = if tile >= 0 { Some(tile as u32) } else { None };
let mut properties = HashMap::new();
parse_tag!(parser, "wangcolor", {
"properties" => |_| {
properties = parse_properties(parser)?;
Ok(())
},
});
Ok(WangColor {
name,
color,
tile,
probability,
properties,
})
}
}