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)]
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            "{visibility}{kind} {id}({plan}) {body}",
84            visibility = self.visibility,
85            kind = self.kind,
86            id = self.id,
87            plan = self.plan,
88            body = self.body
89        )
90    }
91}
92
93impl std::fmt::Debug for WorkbenchDefinition {
94    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95        write!(
96            f,
97            "{visibility}{kind} {id:?}({plan:?}) {body:?}",
98            visibility = self.visibility,
99            kind = self.kind,
100            id = self.id,
101            plan = self.plan,
102            body = self.body
103        )
104    }
105}
106
107impl TreeDisplay for WorkbenchDefinition {
108    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
109        writeln!(
110            f,
111            "{:depth$}{visibility}Workbench ({kind}) '{id}':",
112            "",
113            visibility = self.visibility,
114            kind = self.kind,
115            id = self.id
116        )?;
117        depth.indent();
118        self.doc.tree_print(f, depth)?;
119        self.plan.tree_print(f, depth)?;
120        self.body.tree_print(f, depth)
121    }
122}