use std::collections::HashMap;
use bon::Builder;
use indexmap::IndexSet;
use parse_display::{Display, FromStr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::format::View;
pub mod import {
use super::*;
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Builder)]
#[serde(rename = "GltfImportOptions")]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass,
pyo3::pyclass(name = "GltfImportOptions", from_py_object)
)]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub struct Options {}
#[cfg(feature = "python")]
#[pyo3_stub_gen::derive::gen_stub_pymethods]
#[pyo3::pymethods]
impl Options {
#[new]
pub fn new() -> Self {
Default::default()
}
}
}
pub mod export {
use super::*;
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, Builder)]
#[serde(default, rename = "GltfExportOptions")]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass,
pyo3::pyclass(name = "GltfExportOptions", from_py_object)
)]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub struct Options {
#[builder(default)]
pub storage: Storage,
#[builder(default)]
pub presentation: Presentation,
#[deprecated(note = "add `geometry_uuids` to `extras` instead")]
#[builder(default)]
pub include_uuids: bool,
#[builder(default)]
#[serde(default)]
pub extras: IndexSet<Extra>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "ts-rs", ts(optional))]
pub tessellation: Option<crate::format::tessellation::Options>,
#[builder(default)]
#[serde(skip_serializing_if = "HashMap::is_empty")]
pub views: HashMap<String, View>,
}
impl Options {
#[allow(deprecated)]
pub fn includes_geometry_uuids(&self) -> bool {
self.include_uuids || self.extras.contains(&Extra::GeometryUuids)
}
}
#[cfg(feature = "python")]
#[pyo3_stub_gen::derive::gen_stub_pymethods]
#[pyo3::pymethods]
impl Options {
#[new]
pub fn new() -> Self {
Default::default()
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
#[display(style = "snake_case")]
#[serde(rename = "GltfExportExtra", rename_all = "snake_case")]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass_enum,
pyo3::pyclass(name = "GltfExportExtra", from_py_object)
)]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub enum Extra {
Sketches,
Regions,
ImportedFiles,
Mbd,
GeometryUuids,
}
#[derive(
Default, Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr,
)]
#[display(style = "snake_case")]
#[serde(rename = "GltfStorage", rename_all = "snake_case")]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass_enum,
pyo3::pyclass(name = "GltfStorage", from_py_object)
)]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub enum Storage {
Binary,
Standard,
#[default]
Embedded,
}
#[derive(
Default, Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr,
)]
#[display(style = "snake_case")]
#[serde(rename = "GltfPresentation", rename_all = "snake_case")]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[cfg_attr(not(feature = "unstable_exhaustive"), non_exhaustive)]
pub enum Presentation {
Compact,
#[default]
Pretty,
}
}
#[cfg(test)]
mod tests {
use super::export::{Extra, Options, Presentation, Storage};
use crate::format::{tessellation::Mode, OutputFormat3d};
#[test]
fn tessellation_is_optional_and_defaults_to_accurate() {
let ordinary: Options = serde_json::from_str("{}").unwrap();
assert!(serde_json::to_value(ordinary).unwrap().get("tessellation").is_none());
for (settings, mode) in [
(serde_json::json!({}), Mode::Accurate),
(serde_json::json!({"mode": "accurate"}), Mode::Accurate),
(serde_json::json!({"mode": "visual"}), Mode::Visual),
] {
let options: Options = serde_json::from_value(serde_json::json!({"tessellation": settings})).unwrap();
assert_eq!(options.tessellation.unwrap().mode, mode);
assert_eq!(
serde_json::to_value(options).unwrap()["tessellation"]["mode"],
mode.to_string()
);
}
}
#[test]
fn additional_data_is_opt_in() {
let options: Options = serde_json::from_str(r#"{"storage":"binary","presentation":"compact"}"#).unwrap();
assert_eq!(
options,
Options::builder()
.storage(Storage::Binary)
.presentation(Presentation::Compact)
.build()
);
assert!(options.extras.is_empty());
assert!(!options.includes_geometry_uuids());
assert!(Options::default().extras.is_empty());
assert_eq!(Options::builder().build(), Options::default());
assert_eq!(serde_json::from_str::<Options>("{}").unwrap(), Options::default());
assert_eq!(serde_json::to_value(options).unwrap()["extras"], serde_json::json!([]));
}
#[test]
#[allow(deprecated)]
fn legacy_uuid_requests_round_trip_without_selecting_extras() {
for include_uuids in [false, true] {
let request = serde_json::json!({"type": "gltf", "include_uuids": include_uuids});
let format: OutputFormat3d = serde_json::from_value(request).unwrap();
let options = Options::builder().include_uuids(include_uuids).build();
assert_eq!(options.includes_geometry_uuids(), include_uuids);
assert_eq!(format, OutputFormat3d::Gltf(options));
let exported = serde_json::to_value(format).unwrap();
assert_eq!(exported["include_uuids"], include_uuids);
assert_eq!(exported["extras"], serde_json::json!([]));
}
}
#[test]
fn extras_options_round_trip() {
let selections = [
(Extra::Sketches, "sketches"),
(Extra::Regions, "regions"),
(Extra::ImportedFiles, "imported_files"),
(Extra::Mbd, "mbd"),
(Extra::GeometryUuids, "geometry_uuids"),
];
for mask in 0..(1 << selections.len()) {
let selected: Vec<_> = selections
.iter()
.enumerate()
.filter(|(index, _)| mask & (1 << index) != 0)
.map(|(_, selection)| *selection)
.collect();
let extras = selected.iter().map(|(extra, _)| *extra).collect();
let names: Vec<_> = selected.iter().map(|(_, name)| *name).collect();
let json = serde_json::json!({
"storage": "binary",
"presentation": "compact",
"include_uuids": false,
"extras": names
});
let options: Options = serde_json::from_value(json.clone()).unwrap();
assert_eq!(options.includes_geometry_uuids(), names.contains(&"geometry_uuids"));
assert_eq!(
options,
Options::builder()
.storage(Storage::Binary)
.presentation(Presentation::Compact)
.extras(extras)
.build()
);
assert_eq!(serde_json::to_value(options).unwrap(), json);
}
}
#[test]
#[allow(deprecated)]
fn extras_options_round_trip_in_output_format() {
let json = serde_json::json!({
"type": "gltf",
"storage": "binary",
"presentation": "compact",
"include_uuids": true,
"extras": ["sketches", "regions", "imported_files", "mbd"]
});
let format: OutputFormat3d = serde_json::from_value(json.clone()).unwrap();
assert_eq!(
format,
OutputFormat3d::Gltf(
Options::builder()
.storage(Storage::Binary)
.presentation(Presentation::Compact)
.include_uuids(true)
.extras([Extra::Sketches, Extra::Regions, Extra::ImportedFiles, Extra::Mbd].into())
.build()
)
);
assert_eq!(serde_json::to_value(format).unwrap(), json);
}
#[test]
fn extras_are_unique_and_order_independent() {
let options: Options =
serde_json::from_str(r#"{"extras":["regions","sketches","regions","sketches"]}"#).unwrap();
let reordered: Options = serde_json::from_str(r#"{"extras":["sketches","regions"]}"#).unwrap();
assert_eq!(options, reordered);
assert_eq!(options.extras.len(), 2);
assert_eq!(
serde_json::to_value(options).unwrap()["extras"],
serde_json::json!(["regions", "sketches"])
);
}
#[test]
fn omitted_extras_default_in_output_format() {
let format: OutputFormat3d = serde_json::from_str(r#"{"type":"gltf"}"#).unwrap();
assert_eq!(format, OutputFormat3d::Gltf(Options::default()));
assert_eq!(serde_json::to_value(format).unwrap()["extras"], serde_json::json!([]));
}
#[test]
fn invalid_extras_options_are_rejected() {
for extras in [
serde_json::json!(["unknown"]),
serde_json::json!(["uuids"]),
serde_json::json!(["MBD"]),
serde_json::json!("sketches"),
serde_json::json!(true),
serde_json::json!(null),
] {
let json = serde_json::json!({"extras": extras});
assert!(serde_json::from_value::<Options>(json).is_err());
}
}
}