fcb_core/writer/
header_writer.rs1use crate::error::Result;
2use crate::packed_rtree::PackedRTree;
3use crate::serializer::to_fcb_header;
4use cjseq::CityJSON;
5use flatbuffers::FlatBufferBuilder;
6
7use super::{attribute::AttributeSchema, serializer::AttributeIndexInfo};
8
9pub struct HeaderWriter<'a> {
11 pub fbb: FlatBufferBuilder<'a>,
13 pub cj: CityJSON,
15
16 pub header_options: HeaderWriterOptions,
18 pub attr_schema: AttributeSchema,
20
21 pub semantic_attr_schema: Option<AttributeSchema>,
23 pub(super) attribute_indices_info: Option<Vec<AttributeIndexInfo>>,
25}
26
27#[derive(Debug, Clone)]
29pub struct HeaderWriterOptions {
30 pub write_index: bool,
32 pub feature_count: u64,
33 pub index_node_size: u16,
35 pub attribute_indices: Option<Vec<(String, Option<u16>)>>, pub geographical_extent: Option<[f64; 6]>,
39}
40
41impl Default for HeaderWriterOptions {
42 fn default() -> Self {
43 HeaderWriterOptions {
44 write_index: true,
45 index_node_size: PackedRTree::DEFAULT_NODE_SIZE,
46 feature_count: 0,
47 attribute_indices: None,
48 geographical_extent: None,
49 }
50 }
51}
52
53impl<'a> HeaderWriter<'a> {
54 pub(super) fn new(
61 cj: CityJSON,
62 header_options: Option<HeaderWriterOptions>,
63 attr_schema: AttributeSchema,
64 semantic_attr_schema: Option<AttributeSchema>,
65 ) -> HeaderWriter<'a> {
66 Self::new_with_options(
67 header_options.unwrap_or_default(),
68 cj,
69 attr_schema,
70 semantic_attr_schema,
71 )
72 }
73
74 fn new_with_options(
81 mut options: HeaderWriterOptions,
82 cj: CityJSON,
83 attr_schema: AttributeSchema,
84 semantic_attr_schema: Option<AttributeSchema>,
85 ) -> HeaderWriter<'a> {
86 let fbb = FlatBufferBuilder::new();
87 if !options.write_index {
93 options.index_node_size = 0;
94 }
95 HeaderWriter {
96 fbb,
97 cj,
98 header_options: options,
99 attr_schema,
100 semantic_attr_schema,
101 attribute_indices_info: None,
102 }
103 }
104
105 pub(super) fn finish_to_header(mut self) -> Result<Vec<u8>> {
111 let header = to_fcb_header(
112 &mut self.fbb,
113 &self.cj,
114 self.header_options,
115 &self.attr_schema,
116 self.semantic_attr_schema.as_ref(),
117 self.attribute_indices_info
118 .as_ref()
119 .filter(|info| !info.is_empty())
120 .map(|info| info.as_slice()),
121 )?;
122 self.fbb.finish_size_prefixed(header, None);
123 Ok(self.fbb.finished_data().to_vec())
124 }
125}