microcad_lang/syntax/
workbench.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Workbench definition syntax element
5
6use crate::{src_ref::*, syntax::*};
7use custom_debug::Debug;
8
9/// Kind of a [`WorkbenchDefinition`].
10#[derive(Clone, Debug, Copy, PartialEq)]
11pub enum WorkbenchKind {
12    /// 3D part
13    Part,
14    /// 2D sketch
15    Sketch,
16    /// Operation
17    Operation,
18}
19
20impl WorkbenchKind {
21    /// return kind name
22    pub fn as_str(&self) -> &'static str {
23        match self {
24            WorkbenchKind::Part => "part",
25            WorkbenchKind::Sketch => "sketch",
26            WorkbenchKind::Operation => "op",
27        }
28    }
29}
30
31impl std::fmt::Display for WorkbenchKind {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        write!(f, "{}", self.as_str())
34    }
35}
36
37/// Workbench definition, e.g `sketch`, `part` or `op`.
38#[derive(Clone, Debug)]
39pub struct WorkbenchDefinition {
40    /// Documentation.
41    pub doc: DocBlock,
42    /// Workbench attributes.
43    pub attribute_list: AttributeList,
44    /// Visibility from outside modules.
45    pub visibility: Visibility,
46    /// Workbench kind.
47    pub kind: Refer<WorkbenchKind>,
48    /// Workbench name.
49    pub id: Identifier,
50    /// Workbench's building plan.
51    pub plan: ParameterList,
52    /// Workbench body
53    pub body: Body,
54    /// Workbench code reference
55    pub src_ref: SrcRef,
56}
57
58impl WorkbenchDefinition {
59    /// Return the source code reference of the head of the definition.
60    ///
61    /// This excludes any attribute, visibility and body.
62    pub fn src_ref_head(&self) -> SrcRef {
63        SrcRef::merge(&self.kind, &self.plan)
64    }
65}
66
67impl<'a> Initialized<'a> for WorkbenchDefinition {
68    fn statements(&'a self) -> std::slice::Iter<'a, Statement> {
69        self.body.statements.iter()
70    }
71}
72
73impl SrcReferrer for WorkbenchDefinition {
74    fn src_ref(&self) -> SrcRef {
75        self.src_ref.clone()
76    }
77}
78
79impl std::fmt::Display for WorkbenchDefinition {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        write!(
82            f,
83            "{kind} {id}({plan}) {body}",
84            kind = self.kind,
85            id = self.id,
86            plan = self.plan,
87            body = self.body
88        )
89    }
90}
91
92impl TreeDisplay for WorkbenchDefinition {
93    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
94        writeln!(
95            f,
96            "{:depth$}Workbench ({kind}) '{id}':",
97            "",
98            kind = self.kind,
99            id = self.id
100        )?;
101        depth.indent();
102        self.doc.tree_print(f, depth)?;
103        self.plan.tree_print(f, depth)?;
104        self.body.tree_print(f, depth)
105    }
106}