use std::io::Write;
use quick_xml::Writer;
use crate::model::control::{FormObject, FormType};
use super::utils::{empty_tag, end_tag, start_tag_attrs, text};
use super::SerializeError;
fn form_tag(form_type: FormType) -> &'static str {
match form_type {
FormType::PushButton => "hp:btn",
FormType::CheckBox => "hp:checkBtn",
FormType::RadioButton => "hp:radioBtn",
FormType::ComboBox => "hp:comboBox",
FormType::Edit => "hp:edit",
}
}
fn color_to_hex(color: u32) -> String {
let r = color & 0xFF;
let g = (color >> 8) & 0xFF;
let b = (color >> 16) & 0xFF;
format!("#{:02X}{:02X}{:02X}", r, g, b)
}
fn prop<'a>(form: &'a FormObject, key: &str, default: &'a str) -> &'a str {
form.properties
.get(key)
.map(|s| s.as_str())
.unwrap_or(default)
}
fn combo_items(form: &FormObject) -> Vec<&str> {
let mut items = Vec::new();
let mut i = 0;
while let Some(v) = form.properties.get(&format!("listItem{}", i)) {
items.push(v.as_str());
i += 1;
}
items
}
pub fn write_form<W: Write>(w: &mut Writer<W>, form: &FormObject) -> Result<(), SerializeError> {
let tag = form_tag(form.form_type);
let fore = color_to_hex(form.fore_color);
let back = color_to_hex(form.back_color);
let default_border = super::super::control::default_border_type(form.form_type).to_string();
let enabled = if form.enabled { "1" } else { "0" };
let checked = match form.value {
1 => "CHECKED",
2 => "INDETERMINATE",
_ => "UNCHECKED",
};
let mut attrs: Vec<(&str, &str)> = Vec::new();
match form.form_type {
FormType::PushButton | FormType::CheckBox | FormType::RadioButton => {
attrs.push(("caption", &form.caption));
attrs.push(("value", checked));
attrs.push(("radioGroupName", prop(form, "RadioGroupName", "")));
attrs.push(("triState", prop(form, "TriState", "0")));
attrs.push(("backStyle", prop(form, "BackStyle", "OPAQUE")));
}
FormType::ComboBox => {
attrs.push(("listBoxRows", prop(form, "ListBoxRows", "4")));
attrs.push(("listBoxWidth", prop(form, "ListBoxWidth", "0")));
attrs.push(("editEnable", prop(form, "EditEnable", "1")));
attrs.push(("selectedValue", &form.text));
}
FormType::Edit => {
attrs.push(("multiLine", prop(form, "MultiLine", "0")));
attrs.push(("passwordChar", prop(form, "PasswordChar", "*")));
attrs.push(("maxLength", prop(form, "MaxLength", "2147483647")));
attrs.push(("scrollBars", prop(form, "ScrollBars", "NONE")));
attrs.push((
"tabKeyBehavior",
prop(form, "TabKeyBehavior", "NEXT_OBJECT"),
));
attrs.push(("numOnly", prop(form, "Number", "0")));
attrs.push(("readOnly", prop(form, "ReadOnly", "0")));
attrs.push(("alignText", prop(form, "AlignText", "LEFT")));
}
}
attrs.push(("name", &form.name));
attrs.push(("foreColor", &fore));
attrs.push(("backColor", &back));
attrs.push(("groupName", prop(form, "GroupName", "")));
attrs.push(("tabStop", prop(form, "TabStop", "1")));
attrs.push(("editable", prop(form, "Editable", "1")));
attrs.push(("tabOrder", prop(form, "TabOrder", "0")));
attrs.push(("enabled", enabled));
attrs.push(("borderTypeIDRef", prop(form, "BorderType", &default_border)));
attrs.push(("drawFrame", prop(form, "DrawFrame", "1")));
attrs.push(("printable", prop(form, "Printable", "1")));
attrs.push(("command", prop(form, "Command", "")));
start_tag_attrs(w, tag, &attrs)?;
empty_tag(
w,
"hp:formCharPr",
&[
("charPrIDRef", prop(form, "CharShapeID", "0")),
("followContext", prop(form, "FollowContext", "0")),
("autoSz", prop(form, "AutoSize", "0")),
("wordWrap", prop(form, "WordWrap", "0")),
],
)?;
match form.form_type {
FormType::ComboBox => {
for (i, item) in combo_items(form).into_iter().enumerate() {
let display_key = format!("listItemDisplay{}", i);
let display = prop(form, &display_key, "");
empty_tag(
w,
"hp:listItem",
&[("displayText", display), ("value", item)],
)?;
}
}
FormType::Edit => {
start_tag_attrs(w, "hp:text", &[])?;
if !form.text.is_empty() {
text(w, &form.text)?;
}
end_tag(w, "hp:text")?;
}
_ => {}
}
let width = form.width.to_string();
let height = form.height.to_string();
empty_tag(
w,
"hp:sz",
&[
("width", &width),
("widthRelTo", prop(form, "SzWidthRelTo", "ABSOLUTE")),
("height", &height),
("heightRelTo", prop(form, "SzHeightRelTo", "ABSOLUTE")),
("protect", prop(form, "SzProtect", "0")),
],
)?;
empty_tag(
w,
"hp:pos",
&[
("treatAsChar", prop(form, "PosTreatAsChar", "1")),
("affectLSpacing", prop(form, "PosAffectLSpacing", "0")),
("flowWithText", prop(form, "PosFlowWithText", "1")),
("allowOverlap", prop(form, "PosAllowOverlap", "1")),
("holdAnchorAndSO", prop(form, "PosHoldAnchorAndSO", "0")),
("vertRelTo", prop(form, "PosVertRelTo", "PARA")),
("horzRelTo", prop(form, "PosHorzRelTo", "COLUMN")),
("vertAlign", prop(form, "PosVertAlign", "TOP")),
("horzAlign", prop(form, "PosHorzAlign", "LEFT")),
("vertOffset", prop(form, "PosVertOffset", "0")),
("horzOffset", prop(form, "PosHorzOffset", "0")),
],
)?;
empty_tag(
w,
"hp:outMargin",
&[
("left", prop(form, "OutMarginLeft", "0")),
("right", prop(form, "OutMarginRight", "0")),
("top", prop(form, "OutMarginTop", "0")),
("bottom", prop(form, "OutMarginBottom", "0")),
],
)?;
end_tag(w, tag)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
fn to_string<F: FnOnce(&mut Writer<Vec<u8>>) -> Result<(), SerializeError>>(f: F) -> String {
let mut w: Writer<Vec<u8>> = Writer::new(Vec::new());
f(&mut w).expect("write");
String::from_utf8(w.into_inner()).unwrap()
}
#[test]
fn color_inverts_parse_color_str() {
assert_eq!(color_to_hex(0x003C2B1A), "#1A2B3C");
assert_eq!(color_to_hex(0x00F0F0F0), "#F0F0F0");
}
#[test]
fn checkbox_emits_indeterminate_value() {
let form = FormObject {
form_type: FormType::CheckBox,
name: "CheckBox".to_string(),
value: 2,
enabled: true,
..Default::default()
};
let xml = to_string(|w| write_form(w, &form));
assert!(xml.contains(r#"value="INDETERMINATE""#), "{xml}");
}
#[test]
fn checkbox_emits_checked_value_and_caption() {
let form = FormObject {
form_type: FormType::CheckBox,
name: "CheckBox".to_string(),
caption: "선택 상자".to_string(),
value: 1,
enabled: true,
..Default::default()
};
let xml = to_string(|w| write_form(w, &form));
assert!(xml.starts_with("<hp:checkBtn"), "{xml}");
assert!(xml.contains(r#"caption="선택 상자""#), "{xml}");
assert!(xml.contains(r#"value="CHECKED""#), "{xml}");
assert!(xml.contains(r#"name="CheckBox""#), "{xml}");
assert!(xml.contains("</hp:checkBtn>"), "{xml}");
}
#[test]
fn combobox_emits_list_items_and_selected_value() {
let mut props = HashMap::new();
props.insert("listItem0".to_string(), "계절 선택".to_string());
props.insert("listItem1".to_string(), "봄".to_string());
let form = FormObject {
form_type: FormType::ComboBox,
name: "ComboBox".to_string(),
text: "봄".to_string(),
enabled: true,
properties: props,
..Default::default()
};
let xml = to_string(|w| write_form(w, &form));
assert!(xml.starts_with("<hp:comboBox"), "{xml}");
assert!(xml.contains(r#"selectedValue="봄""#), "{xml}");
assert!(
xml.contains(r#"<hp:listItem displayText="" value="계절 선택"/>"#),
"{xml}"
);
assert!(
xml.contains(r#"<hp:listItem displayText="" value="봄"/>"#),
"{xml}"
);
}
#[test]
fn edit_emits_text_child() {
let form = FormObject {
form_type: FormType::Edit,
name: "Edit".to_string(),
text: "입력값".to_string(),
enabled: true,
..Default::default()
};
let xml = to_string(|w| write_form(w, &form));
assert!(xml.starts_with("<hp:edit"), "{xml}");
assert!(xml.contains("<hp:text>입력값</hp:text>"), "{xml}");
}
#[test]
fn task3001_edit_password_char_defaults_to_asterisk() {
let form = FormObject {
form_type: FormType::Edit,
name: "Edit".to_string(),
enabled: true,
..Default::default()
};
let xml = to_string(|w| write_form(w, &form));
assert!(xml.contains(r#"passwordChar="*""#), "{xml}");
}
#[test]
fn nondefault_pos_outmargin_sz_listdisplay_are_emitted_from_props() {
let mut props = HashMap::new();
props.insert("PosVertOffset".to_string(), "1234".to_string());
props.insert("PosVertRelTo".to_string(), "PAPER".to_string());
props.insert("OutMarginLeft".to_string(), "283".to_string());
props.insert("SzWidthRelTo".to_string(), "RELATIVE".to_string());
props.insert("SzProtect".to_string(), "1".to_string());
props.insert("listItem0".to_string(), "v0".to_string());
props.insert("listItemDisplay0".to_string(), "표시0".to_string());
let form = FormObject {
form_type: FormType::ComboBox,
name: "ComboBox".to_string(),
properties: props,
..Default::default()
};
let xml = to_string(|w| write_form(w, &form));
assert!(xml.contains(r#"vertOffset="1234""#), "{xml}");
assert!(xml.contains(r#"vertRelTo="PAPER""#), "{xml}");
assert!(xml.contains(r#"left="283""#), "{xml}");
assert!(xml.contains(r#"widthRelTo="RELATIVE""#), "{xml}");
assert!(xml.contains(r#"protect="1""#), "{xml}");
assert!(
xml.contains(r#"<hp:listItem displayText="표시0" value="v0"/>"#),
"{xml}"
);
}
}