fcb_cli 0.7.8

FlatCityBuf is a library for reading and writing CityJSON with FlatBuffers.
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
//! `fcb` — the FlatCityBuf command-line tool.
//!
//! Argument parsing and subcommand dispatch. The conversions themselves are
//! [`fcb_core`]; the shared helpers (multi-file merging, input sniffing, the
//! `inspect` terminal UI) are [`fcb_cli`]. Run `fcb --help` for the full
//! interface, or see the [`fcb_cli`] crate docs for a summary.

use cjseq::{CityJSON, CityJSONFeature, Transform as CjTransform};
use clap::{ArgAction, Parser, Subcommand};
use console::{style, Term};
use fcb_cli::CliError;
use fcb_core::error::Error;
use fcb_core::{
    attribute::{AttributeSchema, AttributeSchemaMethods},
    deserializer,
    header_writer::HeaderWriterOptions,
    FcbReader, FcbWriter,
};
use glob::glob;
use indicatif::{ProgressBar, ProgressStyle};
use std::{
    fs::File,
    io::{self, BufReader, BufWriter, Read, Write},
    path::PathBuf,
};
#[derive(Parser)]
#[command(
    name = "fcb",
    author,
    version,
    about = "CLI tool for CityJSON <-> FCB conversion"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Convert CityJSON to FCB
    Ser {
        /// Input files (glob patterns supported, e.g., "cities/*/*.jsonl")
        #[arg(required = true, num_args = 1..)]
        input: Vec<String>,

        /// Output file (use '-' for stdout)
        output: String,

        /// Comma-separated list of attributes to create index for
        #[arg(short = 'a', long)]
        attr_index: Option<String>,

        /// If index all attributes
        #[arg(short = 'A', long, action = ArgAction::SetTrue)]
        index_all_attributes: bool,

        /// Disable spatial index (spatial index is enabled by default)
        #[arg(short = 's', long, action = ArgAction::SetTrue)]
        no_spatial_index: bool,

        /// Branching factor for attribute index
        #[arg(long)]
        attr_branching_factor: Option<u16>,

        /// Node size of the spatial R-tree index (default 16)
        #[arg(long)]
        index_node_size: Option<u16>,

        /// Write a features_count of 0, which means "unknown" and forces
        /// readers to scan to EOF. Conformance fixtures only.
        #[arg(long, action = ArgAction::SetTrue)]
        no_feature_count: bool,

        /// Bounding box filter in format "minx,miny,maxx,maxy"
        #[arg(short = 'b', long)]
        bbox: Option<String>,

        /// Automatically calculate and set geospatial extent in header
        #[arg(short = 'g', long, action = ArgAction::SetTrue)]
        ge: bool,
    },

    /// Convert FCB to CityJSON
    Deser {
        /// Input file (use '-' for stdin)
        input: String,

        /// Output file (use '-' for stdout)
        output: String,
    },

    /// Convert CityJSON to CBOR
    Cbor {
        /// Input file (use '-' for stdin)
        input: String,
        /// Output file (use '-' for stdout)
        output: String,
    },

    /// Convert CityJSON to BSON
    Bson {
        /// Input file (use '-' for stdin)
        input: String,
        /// Output file (use '-' for stdout)
        output: String,
    },

    /// Inspect an FCB file or URL: a terminal UI on a TTY, a static report
    /// otherwise
    Inspect {
        /// Local path or HTTP(S) URL to an FCB file
        source: String,

        /// Print the static text report instead of the terminal UI. Implied
        /// when stdout is not a terminal.
        #[arg(long = "static", action = ArgAction::SetTrue)]
        static_report: bool,
    },
}

fn get_reader(input: &str) -> Result<Box<dyn Read>, Error> {
    match input {
        "-" => Ok(Box::new(io::stdin())),
        path => Ok(Box::new(File::open(path)?)),
    }
}

fn get_writer(output: &str) -> Result<Box<dyn Write>, Error> {
    match output {
        "-" => Ok(Box::new(io::stdout())),
        path => Ok(Box::new(File::create(path)?)),
    }
}

struct SerializeOptions {
    attr_index: Option<String>,
    index_all_attributes: bool,
    no_spatial_index: bool,
    attr_branching_factor: Option<u16>,
    index_node_size: Option<u16>,
    no_feature_count: bool,
    bbox: Option<String>,
    ge: bool,
}

fn serialize(inputs: &[String], output: &str, options: SerializeOptions) -> Result<(), CliError> {
    let term = Term::stderr();
    let is_stdout = output == "-";

    // Print header
    if !is_stdout {
        term.write_line(&format!(
            "\n{} {}",
            style("━━━").bold().cyan(),
            style("FlatCityBuf Serialization").bold().cyan()
        ))
        .ok();
        term.write_line(&format!(
            "{} {}",
            style("━━━").bold().cyan(),
            style("━━━━━━━━━━━━━━━━━━━━━━━━").bold().cyan()
        ))
        .ok();
    }

    // Expand glob patterns and collect all input files
    let mut input_paths: Vec<PathBuf> = Vec::new();
    for pattern in inputs {
        let paths: Vec<PathBuf> = glob(pattern)?.filter_map(|entry| entry.ok()).collect();
        if paths.is_empty() {
            // If no glob match, treat as literal path
            input_paths.push(PathBuf::from(pattern));
        } else {
            input_paths.extend(paths);
        }
    }

    if input_paths.is_empty() {
        return Err(CliError::NoInputFiles);
    }

    let writer = get_writer(output)?;
    let writer = BufWriter::new(writer);

    // Parse the bbox if provided
    let bbox_parsed = if let Some(bbox_str) = &options.bbox {
        Some(parse_bbox(bbox_str).map_err(|e| {
            CliError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                format!("failed to parse bbox: {e}"),
            ))
        })?)
    } else {
        None
    };

    // Print configuration
    if !is_stdout {
        term.write_line("").ok();
        term.write_line(&format!("{} Configuration", style("").bold().green()))
            .ok();
        term.write_line(&format!(
            "  {} {} file(s)",
            style("Input:").dim(),
            style(input_paths.len()).yellow()
        ))
        .ok();
        for (i, path) in input_paths.iter().enumerate().take(5) {
            term.write_line(&format!(
                "    {}. {}",
                style(i + 1).dim(),
                style(path.display()).yellow()
            ))
            .ok();
        }
        if input_paths.len() > 5 {
            term.write_line(&format!(
                "    {} {} more files...",
                style("...").dim(),
                style(input_paths.len() - 5).dim()
            ))
            .ok();
        }
        term.write_line(&format!(
            "  {} {}",
            style("Output:").dim(),
            style(output).yellow()
        ))
        .ok();
        term.write_line(&format!(
            "  {} {}",
            style("Spatial Index:").dim(),
            if options.no_spatial_index {
                style("disabled").red()
            } else {
                style("enabled").green()
            }
        ))
        .ok();

        if let Some(bbox) = &bbox_parsed {
            term.write_line(&format!(
                "  {} [{:.2}, {:.2}, {:.2}, {:.2}]",
                style("Bounding Box:").dim(),
                bbox[0],
                bbox[1],
                bbox[2],
                bbox[3]
            ))
            .ok();
        }

        if options.index_all_attributes {
            term.write_line(&format!(
                "  {} {}",
                style("Attribute Index:").dim(),
                style("all attributes").green()
            ))
            .ok();
        } else if let Some(attrs) = &options.attr_index {
            term.write_line(&format!(
                "  {} {}",
                style("Attribute Index:").dim(),
                style(attrs).green()
            ))
            .ok();
        }

        if let Some(bf) = options.attr_branching_factor {
            term.write_line(&format!(
                "  {} {}",
                style("Branching Factor:").dim(),
                style(bf).yellow()
            ))
            .ok();
        }

        term.write_line(&format!(
            "  {} {}",
            style("Geospatial Extent:").dim(),
            if options.ge {
                style("auto-calculate").green()
            } else {
                style("not set").dim()
            }
        ))
        .ok();
        term.write_line("").ok();
    }

    // Read and merge input files
    if !is_stdout {
        term.write_line(&format!(
            "{} Reading CityJSON...",
            style("").bold().green()
        ))
        .ok();
    }

    let merge_result = fcb_cli::merger::merge_files(input_paths)?;
    let cj = merge_result.metadata;
    let features = merge_result.features;

    if !is_stdout {
        term.write_line(&format!(
            "  {} {} features",
            style("").bold().green(),
            style(features.len()).bold().yellow()
        ))
        .ok();
    }

    // Filter features by bbox if provided
    if !is_stdout && bbox_parsed.is_some() {
        term.write_line(&format!(
            "{} Filtering by bounding box...",
            style("").bold().green()
        ))
        .ok();
    }

    let filtered_features = if let Some(bbox) = &bbox_parsed {
        features
            .into_iter()
            .filter(|feature| feature_intersects_bbox(feature, bbox, &cj.transform))
            .collect()
    } else {
        features
    };

    if filtered_features.is_empty() {
        if !is_stdout {
            term.write_line(&format!(
                "  {} No features found within the specified bbox",
                style("").bold().yellow()
            ))
            .ok();
        }
    } else if !is_stdout && bbox_parsed.is_some() {
        term.write_line(&format!(
            "  {} {} features after filtering",
            style("").bold().green(),
            style(filtered_features.len()).bold().yellow()
        ))
        .ok();
    }

    // Build attribute schema
    if !is_stdout {
        term.write_line(&format!(
            "{} Building attribute schema...",
            style("").bold().green()
        ))
        .ok();
    }

    let attr_schema = {
        let mut schema = AttributeSchema::new();
        // Limit to max 1000 features for schema building to have faster build time
        for feature in filtered_features.iter().take(1000) {
            // Sorted, because `add_attributes` assigns each new attribute the
            // next free column index -- so a `HashMap`'s random iteration order
            // would hand the same input different column numbers on every run.
            let mut ids: Vec<&String> = feature.city_objects.keys().collect();
            ids.sort_unstable();
            for co in ids
                .into_iter()
                .filter_map(|id| feature.city_objects.get(id))
            {
                if let Some(attributes) = &co.attributes {
                    schema.add_attributes(attributes);
                }
            }
        }
        if schema.is_empty() {
            None
        } else {
            Some(schema)
        }
    };

    if !is_stdout {
        if let Some(ref schema) = attr_schema {
            term.write_line(&format!(
                "  {} {} unique attributes found",
                style("").bold().green(),
                style(schema.len()).bold().yellow()
            ))
            .ok();
        } else {
            term.write_line(&format!(
                "  {} No attributes found",
                style("").bold().green()
            ))
            .ok();
        }
    }

    let semantic_attr_schema = {
        let mut schema = AttributeSchema::new();
        for feature in filtered_features.iter() {
            // Sorted for the same reason as the attribute schema above.
            let mut ids: Vec<&String> = feature.city_objects.keys().collect();
            ids.sort_unstable();
            for co in ids
                .into_iter()
                .filter_map(|id| feature.city_objects.get(id))
            {
                if let Some(geometry) = &co.geometry {
                    for geom in geometry.iter() {
                        if let Some(semantics) = geom.common().and_then(|c| c.semantics.as_ref()) {
                            for sem_obj in semantics.surfaces.iter() {
                                // A semantic surface's `other` holds the
                                // members the schema does not name; they
                                // become attribute columns.
                                if !sem_obj.other.is_empty() {
                                    let other = serde_json::Value::Object(
                                        sem_obj.other.clone().into_iter().collect(),
                                    );
                                    schema.add_attributes(&other);
                                }
                            }
                        }
                    }
                }
            }
        }
        if schema.is_empty() {
            None
        } else {
            Some(schema)
        }
    };

    let attr_index_vec: Option<Vec<(String, Option<u16>)>> =
        if options.index_all_attributes && attr_schema.is_some() {
            // create a vec with all attribute names and branching factor given
            Some(
                attr_schema
                    .clone()
                    .unwrap()
                    .iter()
                    .map(|attr| {
                        (
                            attr.0.to_string(),
                            Some(options.attr_branching_factor.unwrap_or(256)),
                        )
                    })
                    .collect::<Vec<(String, Option<u16>)>>(),
            )
        } else {
            options.attr_index.map(|s| {
                s.split(',')
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .map(|s| (s, options.attr_branching_factor))
                    .collect::<Vec<(String, Option<u16>)>>()
            })
        };

    // Calculate geospatial extent if requested
    let geo_extent = if options.ge {
        if !is_stdout {
            term.write_line(&format!(
                "{} Calculating geospatial extent...",
                style("").bold().green()
            ))
            .ok();
        }
        let extent = calculate_geospatial_extent(&filtered_features, &cj.transform);
        if !is_stdout {
            term.write_line(&format!(
                "  {} Min: [{:.2}, {:.2}, {:.2}]",
                style("").bold().green(),
                extent[0],
                extent[1],
                extent[2]
            ))
            .ok();
            term.write_line(&format!(
                "    Max: [{:.2}, {:.2}, {:.2}]",
                extent[3], extent[4], extent[5]
            ))
            .ok();
        }
        Some(extent)
    } else {
        None
    };

    let header_options = HeaderWriterOptions {
        write_index: !options.no_spatial_index,
        feature_count: if options.no_feature_count {
            0
        } else {
            filtered_features.len() as u64
        },
        // The R-tree node size, NOT the attribute B+tree branching factor:
        // they are unrelated knobs and were previously driven by one flag.
        index_node_size: options.index_node_size.unwrap_or(16),
        attribute_indices: attr_index_vec.clone(),
        geographical_extent: geo_extent,
    };

    // Show index information
    if !is_stdout {
        term.write_line(&format!(
            "{} Building indices...",
            style("").bold().green()
        ))
        .ok();

        if !options.no_spatial_index {
            term.write_line(&format!(
                "  {} Spatial R-tree index (node size: {})",
                style("").bold().green(),
                style(header_options.index_node_size).yellow()
            ))
            .ok();
        }

        if let Some(ref indices) = attr_index_vec {
            term.write_line(&format!(
                "  {} Attribute B+Tree indices for {} attributes:",
                style("").bold().green(),
                style(indices.len()).yellow()
            ))
            .ok();
            for (attr_name, bf) in indices.iter().take(5) {
                term.write_line(&format!(
                    "{} (branching factor: {})",
                    style(attr_name).cyan(),
                    style(bf.unwrap_or(16)).dim()
                ))
                .ok();
            }
            if indices.len() > 5 {
                term.write_line(&format!(
                    "    {} {} more attributes...",
                    style("...").dim(),
                    style(indices.len() - 5).dim()
                ))
                .ok();
            }
        }
        term.write_line("").ok();
    }

    // Write features
    if !is_stdout {
        term.write_line(&format!(
            "{} Writing FCB file...",
            style("").bold().green()
        ))
        .ok();
    }

    let mut fcb = FcbWriter::new(cj, Some(header_options), attr_schema, semantic_attr_schema)?;

    let pb = if !is_stdout {
        let pb = ProgressBar::new(filtered_features.len() as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("  {bar:40.cyan/blue} {pos}/{len} features ({percent}%)")
                .unwrap()
                .progress_chars("━━╾─"),
        );
        Some(pb)
    } else {
        None
    };

    for feature in filtered_features.iter() {
        fcb.add_feature(feature)?;
        if let Some(ref pb) = pb {
            pb.inc(1);
        }
    }

    if let Some(ref pb) = pb {
        pb.finish_and_clear();
    }

    fcb.write(writer)?;

    if !is_stdout {
        term.write_line(&format!(
            "  {} File written successfully",
            style("").bold().green()
        ))
        .ok();
        term.write_line("").ok();
        term.write_line(&format!(
            "{} {}",
            style("━━━").bold().cyan(),
            style("Serialization Complete").bold().cyan()
        ))
        .ok();
        term.write_line(&format!(
            "{} {}",
            style("━━━").bold().cyan(),
            style("━━━━━━━━━━━━━━━━━━━━━━").bold().cyan()
        ))
        .ok();
        term.write_line("").ok();
    }

    Ok(())
}

/// Parse a bounding box string in format "minx,miny,maxx,maxy"
fn parse_bbox(bbox_str: &str) -> Result<[f64; 4], String> {
    let parts: Vec<&str> = bbox_str.split(',').collect();
    if parts.len() != 4 {
        return Err(format!(
            "Invalid bounding box format. Expected 'minx,miny,maxx,maxy', got '{bbox_str}'"
        ));
    }

    let mut bbox = [0.0; 4];
    for (i, part) in parts.iter().enumerate() {
        bbox[i] = part
            .trim()
            .parse::<f64>()
            .map_err(|e| format!("Failed to parse bbox component: {e}"))?;
    }

    // Validate that min <= max
    if bbox[0] > bbox[2] || bbox[1] > bbox[3] {
        return Err(
            "Invalid bounding box: min values must be less than or equal to max values".to_string(),
        );
    }

    Ok(bbox)
}

/// Get all vertices from a feature
fn get_vertices_from_feature(feature: &CityJSONFeature, transform: &CjTransform) -> Vec<[f64; 3]> {
    let mut result = Vec::new();

    for vertex in &feature.vertices {
        if vertex.len() >= 3 {
            // Convert from i64 to f64 and apply transform
            let x = (vertex[0] as f64 * transform.scale[0]) + transform.translate[0];
            let y = (vertex[1] as f64 * transform.scale[1]) + transform.translate[1];
            let z = (vertex[2] as f64 * transform.scale[2]) + transform.translate[2];

            result.push([x, y, z]);
        }
    }

    result
}

/// Check if a CityJSONFeature intersects with a bounding box
fn feature_intersects_bbox(
    feature: &CityJSONFeature,
    bbox: &[f64; 4],
    transform: &CjTransform,
) -> bool {
    // Get transformed vertices from the feature
    let vertices = get_vertices_from_feature(feature, transform);
    if city_object_intersects_bbox(bbox, &vertices) {
        return true;
    }

    false
}

/// Check if a CityObject intersects with a bounding box
fn city_object_intersects_bbox(bbox: &[f64; 4], feature_vertices: &[[f64; 3]]) -> bool {
    // Check if any of the vertices are within the bbox
    for vertex in feature_vertices {
        if point_in_bbox_2d(vertex, bbox) {
            return true;
        }
    }

    false
}

/// Check if a point is inside a 2D bounding box
fn point_in_bbox_2d(point: &[f64; 3], bbox: &[f64; 4]) -> bool {
    point[0] >= bbox[0] && point[0] <= bbox[2] && point[1] >= bbox[1] && point[1] <= bbox[3]
}

/// Calculate the geospatial extent from a list of features
fn calculate_geospatial_extent(features: &[CityJSONFeature], transform: &CjTransform) -> [f64; 6] {
    let mut min_x = f64::MAX;
    let mut min_y = f64::MAX;
    let mut min_z = f64::MAX;
    let mut max_x = f64::MIN;
    let mut max_y = f64::MIN;
    let mut max_z = f64::MIN;

    for feature in features {
        let vertices = get_vertices_from_feature(feature, transform);

        for [x, y, z] in vertices {
            min_x = min_x.min(x);
            min_y = min_y.min(y);
            min_z = min_z.min(z);
            max_x = max_x.max(x);
            max_y = max_y.max(y);
            max_z = max_z.max(z);
        }
    }

    // If no vertices were found, return a default extent
    if min_x == f64::MAX {
        return [0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
    }

    [min_x, min_y, min_z, max_x, max_y, max_z]
}

fn deserialize(input: &str, output: &str) -> Result<(), Error> {
    let reader = BufReader::new(get_reader(input)?);
    let mut writer = BufWriter::new(get_writer(output)?);
    let mut fcb_reader = FcbReader::open(reader)?.select_all_seq()?;

    let header = fcb_reader.header();
    let cj = deserializer::to_cj_metadata(&header)?;

    // Write header
    writeln!(writer, "{}", serde_json::to_string(&cj)?)?;

    // Write features. The iterator stops at the declared feature count, or at
    // EOF when the header declares 0, which means "unknown". Breaking here on
    // `features_count` instead truncated such a file to a single feature.
    // `?` on the iterator, not `while let Ok(..)`: swallowing the error made a
    // mid-file decode failure indistinguishable from a clean end of stream, so
    // a count-0 file could be truncated to a short, wrong output and still exit 0.
    while let Some(feat_buf) = fcb_reader.next()? {
        let feature = feat_buf.cur_cj_feature()?;
        writeln!(writer, "{}", serde_json::to_string(&feature)?)?;
    }

    if output != "-" {
        eprintln!("Successfully decoded to CityJSON");
    }
    Ok(())
}

fn encode_cbor(input: &str, output: &str) -> Result<(), Error> {
    let reader = BufReader::new(get_reader(input)?);
    let writer = BufWriter::new(get_writer(output)?);

    let value: serde_json::Value = serde_json::from_reader(reader)?;
    serde_cbor::to_writer(writer, &value).map_err(|e| {
        Error::IoError(std::io::Error::other(format!(
            "failed to encode to cbor: {e}"
        )))
    })?;

    if output != "-" {
        eprintln!("successfully encoded to cbor");
    }
    Ok(())
}

fn encode_bson(input: &str, output: &str) -> Result<(), Error> {
    let mut reader = BufReader::new(get_reader(input)?);
    let json_str = {
        let mut s = String::new();
        reader.read_to_string(&mut s)?;
        s
    };

    let cityjson: CityJSON = serde_json::from_str(&json_str)?;
    let bson = bson::to_bson(&cityjson).map_err(|e| {
        Error::IoError(std::io::Error::other(format!(
            "failed to encode to bson: {e}"
        )))
    })?;
    let doc = bson.as_document().unwrap();

    let mut writer = get_writer(output)?;
    doc.to_writer(&mut writer).map_err(|e| {
        Error::IoError(std::io::Error::other(format!(
            "failed to encode to bson: {e}"
        )))
    })?;

    if output != "-" {
        eprintln!("successfully encoded to bson");
    }
    Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Ser {
            input,
            output,
            attr_index,
            index_all_attributes,
            no_spatial_index,
            attr_branching_factor,
            index_node_size,
            no_feature_count,
            bbox,
            ge,
        } => serialize(
            &input,
            &output,
            SerializeOptions {
                attr_index,
                index_all_attributes,
                no_spatial_index,
                attr_branching_factor,
                index_node_size,
                no_feature_count,
                bbox,
                ge,
            },
        )?,
        Commands::Deser { input, output } => deserialize(&input, &output)?,
        Commands::Cbor { input, output } => encode_cbor(&input, &output)?,
        Commands::Bson { input, output } => encode_bson(&input, &output)?,
        Commands::Inspect {
            source,
            static_report,
        } => {
            if let Err(err) = fcb_cli::inspect::run_inspect(&source, static_report) {
                eprintln!("{err}");
                std::process::exit(1);
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn verify_cli() {
        use clap::CommandFactory;
        Cli::command().debug_assert();
    }
}