Skip to main content

cityjson_lib/
arrow.rs

1use std::fs::File;
2use std::io::{BufReader, BufWriter, Cursor, Write};
3use std::path::Path;
4
5use crate::{CityModel, Error, Result};
6
7pub use cityjson_arrow::{
8    ExportOptions, ImportOptions, SchemaVersion, WriteReport, read_stream, write_stream,
9};
10
11pub fn from_bytes(bytes: &[u8]) -> Result<CityModel> {
12    from_reader(Cursor::new(bytes))
13}
14
15pub fn to_vec(model: &CityModel) -> Result<Vec<u8>> {
16    let mut bytes = Vec::new();
17    let _ = to_writer(&mut bytes, model)?;
18    Ok(bytes)
19}
20
21pub fn from_reader(reader: impl std::io::Read) -> Result<CityModel> {
22    cityjson_arrow::read_stream(reader, &ImportOptions::default())
23        .map(CityModel::from)
24        .map_err(Error::from)
25}
26
27pub fn to_writer(writer: impl Write, model: &CityModel) -> Result<WriteReport> {
28    cityjson_arrow::write_stream(writer, model, &ExportOptions::default()).map_err(Error::from)
29}
30
31pub fn from_file(path: impl AsRef<Path>) -> Result<CityModel> {
32    let file = File::open(path)?;
33    from_reader(BufReader::new(file))
34}
35
36pub fn to_file(path: impl AsRef<Path>, model: &CityModel) -> Result<WriteReport> {
37    let file = File::create(path)?;
38    let mut writer = BufWriter::new(file);
39    let report = to_writer(&mut writer, model)?;
40    writer.flush()?;
41    Ok(report)
42}