1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use std::fs::File;
use std::io::{Cursor, Read, Seek, SeekFrom};

use anyhow::{anyhow, Context};
use flate2::bufread::ZlibDecoder;
use flate2::Compression;
use flate2::read::ZlibEncoder;
use prost::Message;

use crate::{osm, osmpbf};
use crate::osm::model::bounding_box::BoundingBox;
use crate::osm::model::element::Element;
use crate::osm::pbf::blob_desc::BlobDesc;
use crate::osm::pbf::compression_type::CompressionType;
use crate::osm::pbf::file_block_metadata::FileBlockMetadata;
use crate::osm::pbf::osm_data::OsmData;
use crate::osm::pbf::osm_header::OsmHeader;
use crate::osmpbf::{Blob, BlobHeader};
use crate::osmpbf::blob::Data;

/// A header or data file block in *.osm.pbf file
#[derive(Debug)]
pub enum FileBlock {
    Header {
        metadata: FileBlockMetadata,
        header: OsmHeader,
    },
    Data {
        metadata: FileBlockMetadata,
        data: OsmData,
    },
}

impl FileBlock {
    pub(crate) fn new(index: usize, blob_type: String, data: Vec<u8>) -> Result<FileBlock, anyhow::Error> {
        let blob_type_str = blob_type.as_str();
        match blob_type_str {
            "OSMHeader" => {
                Ok(
                    FileBlock::Header {
                        metadata: FileBlockMetadata::new(blob_type, index),
                        header: OsmHeader::from_bytes(data)?,
                    }
                )
            }
            "OSMData" => {
                Ok(
                    FileBlock::Data {
                        metadata: FileBlockMetadata::new(blob_type, index),
                        data: OsmData::new(data)?,
                    }
                )
            }
            _ => {
                Err(anyhow!("Failed to decode file block"))
            }
        }
    }

    pub(crate) fn from_elements(index: usize, elements: Vec<Element>) -> FileBlock {
        FileBlock::Data {
            metadata: FileBlockMetadata::new("OSMData".to_string(), index),
            data: OsmData::from_elements( elements, None),
        }
    }

    #[allow(dead_code)]
    pub(crate) fn compute_bounding_box(&self) -> Option<BoundingBox> {
        match self {
            FileBlock::Header { metadata: _, header } => {
                header.info().bounding_box().clone()
            }
            FileBlock::Data { metadata: _, data } => {
                data.compute_bounding_box()
            }
        }
    }

    pub(crate) fn from_header(osm_header: OsmHeader) -> FileBlock {
        FileBlock::Header {
            metadata: FileBlockMetadata::new("OSMHeader".to_string(), 0),
            header: osm_header.clone(),
        }
    }

    fn zlib_decode(data: Vec<u8>, raw_size: usize) -> Result<Vec<u8>, anyhow::Error> {
        let mut decoder = ZlibDecoder::new(data.as_slice());
        let mut decoded = vec![0_u8; raw_size];
        decoder.read_exact(&mut decoded)?;
        Ok(decoded)
    }

    fn zlib_encode(buf: Vec<u8>, compression_level: Compression) -> Result<Vec<u8>, anyhow::Error> {
        let mut encoder = ZlibEncoder::new(buf.as_slice(), compression_level);
        let mut encoded = Vec::<u8>::new();
        encoder.read_to_end(&mut encoded)?;
        Ok(encoded)
    }

    pub(crate) fn read_blob_data(blob: osmpbf::Blob) -> Result<Vec<u8>, anyhow::Error> {
        match blob.data {
            None => {
                Err(
                    anyhow!("Input file too short")
                )
            }
            Some(data) => {
                match data {
                    Data::Raw(_) => {
                        Err(
                            // TODO:
                            anyhow!("Raw data type not implemented")
                        )
                    }
                    Data::ZlibData(zlib_data) => {
                        // for now ignore that the uncompressed size is optional
                        FileBlock::zlib_decode(zlib_data, blob.raw_size.unwrap() as usize)
                    }
                    Data::LzmaData(_) => {
                        Err(
                            // TODO:
                            anyhow!("Lzma data type not implemented")
                        )
                    }
                    Data::ObsoleteBzip2Data(_) => {
                        Err(
                            anyhow!("Obsolete Bzip data type not implemented")
                        )
                    }
                    Data::Lz4Data(_) => {
                        Err(
                            // TODO:
                            anyhow!("Lz4 data type not implemented")
                        )
                    }
                    Data::ZstdData(_) => {
                        Err(
                            anyhow!("Zstd data type not implemented")
                        )
                    }
                }
            }
        }
    }

    pub(crate) fn from_blob_desc(blob_desc: &osm::pbf::blob_desc::BlobDesc) -> Result<FileBlock, anyhow::Error> {
        let mut file = File::open(blob_desc.path()).with_context(
            || anyhow!("Failed to open {:?} for reading", blob_desc.path())
        )?;
        file.seek(SeekFrom::Start(blob_desc.start())).with_context(
            || anyhow!("Failed seek to {} in {:?} ", blob_desc.start(), blob_desc.path())
        )?;
        let mut blob_buffer = vec![0; blob_desc.length() as usize];
        file.read_exact(&mut blob_buffer).ok().with_context(
            || anyhow!("Failed to read {} bytes from {:?} ", blob_desc.length(), blob_desc.path())
        )?;
        Self::deserialize(blob_desc, &mut blob_buffer)
    }

    pub(crate) fn serialize(file_block: &FileBlock, compression: CompressionType) -> Result<(Vec<u8>, Vec<u8>), anyhow::Error> {
        let (blob_type, compression_level, block_data) = match file_block {
            FileBlock::Header { metadata: _, header } => {
                ("OSMHeader".to_string(), Compression::none(), header.serialize()?)
            }
            FileBlock::Data { metadata: _, data } => {
                ("OSMData".to_string(), Compression::default(), data.serialize()?)
            }
        };

        let mut raw_size = None;
        let mut data = None;
        if block_data.len() != 0 {
            raw_size = Some(block_data.len() as i32);
            data = match compression {
                CompressionType::Uncompressed => {
                    Some(osmpbf::blob::Data::Raw(block_data))
                }
                CompressionType::Zlib => {
                    Some(osmpbf::blob::Data::ZlibData(Self::zlib_encode(block_data, compression_level)?))
                }
            };
        }

        let blob = Blob {
            raw_size,
            data,
        };

        let mut body = Vec::<u8>::with_capacity(blob.encoded_len());
        blob.encode(&mut body)?;


        let blob_header = BlobHeader {
            r#type: blob_type,
            indexdata: None,
            datasize: body.len() as i32,
        };

        let mut header = Vec::<u8>::with_capacity(blob_header.encoded_len());
        blob_header.encode(&mut header)?;

        Ok((header, body))
    }

    fn deserialize(blob_desc: &BlobDesc, blob_buffer: &mut Vec<u8>) -> Result<FileBlock, anyhow::Error> {
        // use BlobDesc rather than BlobHeader to skip reading again the blob header
        let protobuf_blob = osmpbf::Blob::decode(&mut Cursor::new(blob_buffer)).with_context(
            || anyhow!("Failed to decode a message from blob {} from {:?}", blob_desc.index(), blob_desc.path())
        )?;
        let data = FileBlock::read_blob_data(protobuf_blob)?;
        FileBlock::new(blob_desc.index(), blob_desc.t(), data)
    }

    #[allow(dead_code)]
    pub(crate) fn metadata(&self) -> &FileBlockMetadata {
        match self {
            FileBlock::Header { metadata, header: _ } => {
                metadata
            }
            FileBlock::Data { metadata, data: _ } => {
                metadata
            }
        }
    }

    pub(crate) fn as_osm_header(&self) -> Result<&OsmHeader, anyhow::Error> {
        match self {
            FileBlock::Header { header, .. } => {
                Ok(header)
            }
            FileBlock::Data { .. } => {
                Err(anyhow!("Not an OSMHeader"))
            }
        }
    }
    pub(crate) fn is_osm_header(&self) -> bool {
        match self {
            FileBlock::Header { header: _, .. } => {
                true
            }
            FileBlock::Data { .. } => {
                false
            }
        }
    }

    pub(crate) fn is_osm_data(&self) -> bool {
        !self.is_osm_header()
    }

    #[allow(dead_code)]
    pub(crate) fn as_osm_data(&self) -> Result<&OsmData, anyhow::Error> {
        match self {
            FileBlock::Header { .. } => {
                Err(anyhow!("Not an OSMData"))
            }
            FileBlock::Data { data, .. } => {
                Ok(data)
            }
        }
    }

    #[allow(dead_code)]
    pub(crate) fn elements(&self) -> &Vec<Element> {
        self.as_osm_data().unwrap().elements()
    }

    pub(crate) fn take_elements(&mut self) -> Vec<Element> {
        match self {
            FileBlock::Header { .. } => {
                panic!("Not a Data variant")
            }
            FileBlock::Data { data, .. } => {
                data.take_elements()
            }
        }
    }
}

impl Default for FileBlock {
    fn default() -> Self {
        FileBlock::Data { metadata: Default::default(), data: Default::default() }
    }
}