ui/src/builder/validator/labelinfo.rs

88 lines
2.6 KiB
Rust

use crate::prelude::*;
use anyhow::Result;
use utilities::prelude::*;
use super::gridinfo::GridInfo;
use super::mandatory::Mandatory;
use std::convert::TryFrom;
use std::str::from_utf8;
use std::sync::{Arc, RwLock, Weak};
use super::validator::{cow_to_fill_type, cow_to_str, cow_to_text_alignment, str_into};
pub struct LabelInfo {
pub id: String,
pub x_slot: Mandatory<usize>,
pub y_slot: Mandatory<usize>,
pub x_dim: u32,
pub y_dim: u32,
pub text_ratio: Option<f32>,
pub text: RwLock<String>,
pub text_color: Color,
pub text_alignment: TextAlignment,
pub background_type: Option<FillTypeInfo>,
pub parent: Weak<GridInfo>,
}
impl LabelInfo {
pub fn new<'a>(
attributes: quick_xml::events::attributes::Attributes<'a>,
grid: &Arc<GridInfo>,
) -> Result<LabelInfo> {
let mut label_info = LabelInfo {
id: String::new(),
x_slot: Mandatory::default(),
y_slot: Mandatory::default(),
x_dim: 1,
y_dim: 1,
text_ratio: None,
text: RwLock::new(String::new()),
text_color: Color::default(),
text_alignment: TextAlignment::default(),
background_type: None,
parent: Arc::downgrade(grid),
};
for attribute_res in attributes {
let attribute = attribute_res?;
match attribute.key.into_inner() {
b"id" => label_info.id = cow_to_str(attribute.value),
b"x_slot" => label_info.x_slot.set(str_into(attribute.value)?),
b"y_slot" => label_info.y_slot.set(str_into(attribute.value)?),
b"x_size" => label_info.x_dim = str_into(attribute.value)?,
b"y_size" => label_info.y_dim = str_into(attribute.value)?,
b"text_color" => {
let text = cow_to_str(attribute.value);
label_info.text_color = Color::try_from(text.as_str())?;
}
b"text_ratio" => label_info.text_ratio = Some(str_into(attribute.value)?),
b"text_alignment" => {
label_info.text_alignment = cow_to_text_alignment(attribute.value)?
}
b"background" => {
label_info.background_type = Some(cow_to_fill_type(attribute.value));
}
_ => {
return Err(anyhow::Error::msg(format!(
"Unsupported attribute in Label: {}",
from_utf8(attribute.key.into_inner())?
)))
}
}
}
Ok(label_info)
}
}