Skip to main content

microcad_lang/model/
output_type.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Model output type.
5
6use crate::lower::ir::WorkbenchKind;
7
8/// The output type of the [`crate::model::Model`].
9#[derive(Clone, Copy, Debug, Default, PartialEq)]
10pub enum OutputType {
11    /// The output type has not yet been determined.
12    #[default]
13    NotDetermined,
14    /// The [`crate::model::Model`] outputs a 2d geometry.
15    Geometry2D,
16    /// The [`crate::model::Model`] outputs a 3d geometry.
17    Geometry3D,
18    /// The [`crate::model::Model`] is invalid, you cannot mix 2d and 3d geometry.
19    InvalidMixed,
20}
21
22impl OutputType {
23    /// Merge this output type with another.
24    pub fn merge(&self, other: &Self) -> OutputType {
25        match (self, other) {
26            (OutputType::NotDetermined, output_type) => *output_type,
27            (OutputType::Geometry2D, OutputType::NotDetermined)
28            | (OutputType::Geometry2D, OutputType::Geometry2D)
29            | (OutputType::Geometry3D, OutputType::NotDetermined)
30            | (OutputType::Geometry3D, OutputType::Geometry3D) => *self,
31            (OutputType::Geometry2D, OutputType::Geometry3D)
32            | (OutputType::Geometry3D, OutputType::Geometry2D)
33            | (OutputType::Geometry2D, OutputType::InvalidMixed)
34            | (OutputType::Geometry3D, OutputType::InvalidMixed)
35            | (OutputType::InvalidMixed, _) => OutputType::InvalidMixed,
36        }
37    }
38}
39
40impl std::fmt::Display for OutputType {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(
43            f,
44            "{}",
45            match &self {
46                Self::NotDetermined => "Undetermined",
47                Self::Geometry2D => "2D",
48                Self::Geometry3D => "3D",
49                Self::InvalidMixed => "NO OUTPUT",
50            }
51        )
52    }
53}
54
55impl From<WorkbenchKind> for OutputType {
56    fn from(kind: WorkbenchKind) -> Self {
57        match kind {
58            WorkbenchKind::Sketch => Self::Geometry2D,
59            WorkbenchKind::Part => Self::Geometry3D,
60            WorkbenchKind::Operation => Self::NotDetermined,
61        }
62    }
63}