Skip to main content

export_structural_catalog/
export_structural_catalog.rs

1//! Export the bundled structural catalog for a schema version.
2//!
3//! Writes the catalog to stdout, or to the path given as the first argument.
4
5use std::env;
6use std::io::{self, BufWriter, Write};
7
8use ifc_schema::{write_direct_structural_catalog, write_structural_catalog, SchemaVersion};
9
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let requested = env::args().nth(1).ok_or_else(|| {
12        io::Error::new(
13            io::ErrorKind::InvalidInput,
14            "usage: cargo run -p ifc-schema --example export_structural_catalog -- <ifc2x3|ifc4|ifc4x1|ifc4x2|ifc4x3> [--direct]",
15        )
16    })?;
17    let version = match requested.to_ascii_lowercase().as_str() {
18        "ifc2x3" | "ifc2x3_tc1" => SchemaVersion::Ifc2x3,
19        "ifc4" | "ifc4_add2_tc1" => SchemaVersion::Ifc4,
20        "ifc4x1" | "ifc4x1_final" => SchemaVersion::Ifc4x1,
21        "ifc4x2" | "ifc4x2_final" => SchemaVersion::Ifc4x2,
22        "ifc4x3" | "ifc4x3_add2" => SchemaVersion::Ifc4x3,
23        _ => {
24            return Err(io::Error::new(
25                io::ErrorKind::InvalidInput,
26                format!("unsupported bundled IFC release: {requested}"),
27            )
28            .into());
29        }
30    };
31
32    let direct = match env::args().nth(2).as_deref() {
33        None => false,
34        Some("--direct") => true,
35        Some(value) => {
36            return Err(io::Error::new(
37                io::ErrorKind::InvalidInput,
38                format!("unsupported export option: {value}"),
39            )
40            .into());
41        }
42    };
43    if env::args().nth(3).is_some() {
44        return Err(io::Error::new(io::ErrorKind::InvalidInput, "too many arguments").into());
45    }
46
47    let stdout = io::stdout();
48    let mut output = BufWriter::new(stdout.lock());
49    let summary = if direct {
50        write_direct_structural_catalog(version, &mut output)?
51    } else {
52        write_structural_catalog(version, &mut output)?
53    };
54    output.flush()?;
55    eprintln!(
56        "exported {} entities and {} types for {}",
57        summary.entity_rows,
58        summary.type_count,
59        version.release_id()
60    );
61    Ok(())
62}