pub mod yaml_keys {
pub mod transform {
pub const POSITION: &str = "position";
pub const ROTATION: &str = "rotation";
pub const SCALE: &str = "scale";
}
pub mod sprite {
pub const TEXTURE: &str = "texture";
pub const SIZE: &str = "size";
pub const VISIBLE: &str = "visible";
pub const TINT: &str = "tint";
pub const FLIP_X: &str = "flip_x";
pub const FLIP_Y: &str = "flip_y";
pub const LAYER: &str = "layer";
}
pub mod text {
pub const CONTENT: &str = "content";
pub const FONT: &str = "font";
pub const FONT_SIZE: &str = "font_size";
pub const COLOR: &str = "color";
pub const VISIBLE: &str = "visible";
pub const ALIGNMENT: &str = "alignment";
pub const LINE_SPACING: &str = "line_spacing";
pub const LAYER: &str = "layer";
}
}
pub mod yaml_utils {
use serde_yaml::Value;
use crate::math::Vec2;
use crate::components::Color;
#[derive(Debug, thiserror::Error)]
pub enum YamlParseError {
#[error("Invalid component data: {0}")]
InvalidData(String),
}
pub fn parse_vec2_array(value: &Value, field_name: &str) -> Result<Vec2, YamlParseError> {
if let Some(array) = value.as_sequence() {
let x = array.get(0)
.and_then(|v| v.as_f64())
.ok_or_else(|| YamlParseError::InvalidData(format!("Invalid {} x component", field_name)))? as f32;
let y = array.get(1)
.and_then(|v| v.as_f64())
.ok_or_else(|| YamlParseError::InvalidData(format!("Invalid {} y component", field_name)))? as f32;
Ok(Vec2::new(x, y))
} else {
Err(YamlParseError::InvalidData(format!("Expected array for {}", field_name)))
}
}
pub fn parse_color_array(value: &Value) -> Result<Color, YamlParseError> {
if let Some(array) = value.as_sequence() {
let r = array.get(0).and_then(|v| v.as_f64()).unwrap_or(1.0) as f32;
let g = array.get(1).and_then(|v| v.as_f64()).unwrap_or(1.0) as f32;
let b = array.get(2).and_then(|v| v.as_f64()).unwrap_or(1.0) as f32;
let a = array.get(3).and_then(|v| v.as_f64()).unwrap_or(1.0) as f32;
Ok(Color::new(r, g, b, a))
} else {
Ok(Color::white())
}
}
}