rust_xlsxwriter/
rich_value_types.rs

1// RichValueTypes - A module for creating the Excel rdRichValueTypes.xml file.
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4//
5// Copyright 2022-2025, John McNamara, jmcnamara@cpan.org
6
7use std::io::Cursor;
8
9use crate::xmlwriter::{
10    xml_declaration, xml_empty_tag, xml_end_tag, xml_start_tag, xml_start_tag_only,
11};
12
13pub struct RichValueTypes {
14    pub(crate) writer: Cursor<Vec<u8>>,
15}
16
17impl RichValueTypes {
18    // -----------------------------------------------------------------------
19    // Crate public methods.
20    // -----------------------------------------------------------------------
21
22    // Create a new RichValueTypes struct.
23    pub(crate) fn new() -> RichValueTypes {
24        let writer = Cursor::new(Vec::with_capacity(2048));
25
26        RichValueTypes { writer }
27    }
28
29    // -----------------------------------------------------------------------
30    // XML assembly methods.
31    // -----------------------------------------------------------------------
32
33    // Assemble and generate the XML file.
34    pub(crate) fn assemble_xml_file(&mut self) {
35        xml_declaration(&mut self.writer);
36
37        // Write the rvTypesInfo element.
38        self.write_rv_types_info();
39
40        // Write the global element.
41        self.write_global();
42
43        // Close the final tag.
44        xml_end_tag(&mut self.writer, "rvTypesInfo");
45    }
46
47    // Write the <rvTypesInfo> element.
48    fn write_rv_types_info(&mut self) {
49        let attributes = [
50            (
51                "xmlns",
52                "http://schemas.microsoft.com/office/spreadsheetml/2017/richdata2",
53            ),
54            (
55                "xmlns:mc",
56                "http://schemas.openxmlformats.org/markup-compatibility/2006",
57            ),
58            ("mc:Ignorable", "x"),
59            (
60                "xmlns:x",
61                "http://schemas.openxmlformats.org/spreadsheetml/2006/main",
62            ),
63        ];
64
65        xml_start_tag(&mut self.writer, "rvTypesInfo", &attributes);
66    }
67
68    // Write the <global> element.
69    fn write_global(&mut self) {
70        let key_flags = [
71            ("_Self", "ExcludeFromFile", "ExcludeFromCalcComparison"),
72            ("_DisplayString", "ExcludeFromCalcComparison", ""),
73            ("_Flags", "ExcludeFromCalcComparison", ""),
74            ("_Format", "ExcludeFromCalcComparison", ""),
75            ("_SubLabel", "ExcludeFromCalcComparison", ""),
76            ("_Attribution", "ExcludeFromCalcComparison", ""),
77            ("_Icon", "ExcludeFromCalcComparison", ""),
78            ("_Display", "ExcludeFromCalcComparison", ""),
79            ("_CanonicalPropertyNames", "ExcludeFromCalcComparison", ""),
80            ("_ClassificationId", "ExcludeFromCalcComparison", ""),
81        ];
82
83        xml_start_tag_only(&mut self.writer, "global");
84        xml_start_tag_only(&mut self.writer, "keyFlags");
85
86        for (key, flag1, flag2) in key_flags {
87            self.write_key(key);
88            self.write_flag(flag1);
89
90            if !flag2.is_empty() {
91                self.write_flag(flag2);
92            }
93
94            xml_end_tag(&mut self.writer, "key");
95        }
96
97        xml_end_tag(&mut self.writer, "keyFlags");
98        xml_end_tag(&mut self.writer, "global");
99    }
100
101    // Write the <key> element.
102    fn write_key(&mut self, name: &str) {
103        let attributes = [("name", name)];
104
105        xml_start_tag(&mut self.writer, "key", &attributes);
106    }
107
108    // Write the <flag> element.
109    fn write_flag(&mut self, name: &str) {
110        let attributes = [("name", name), ("value", "1")];
111
112        xml_empty_tag(&mut self.writer, "flag", &attributes);
113    }
114}