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;
8use strum::Display;
9
10/// Kind of a [`WorkbenchDefinition`].
11#[derive(Clone, Display, Debug, Copy, PartialEq)]
12pub enum WorkbenchKind {
13    /// 3D part
14    Part,
15    /// 2D sketch
16    Sketch,
17    /// Operation
18    Operation,
19}
20
21impl WorkbenchKind {
22    /// return kind name
23    pub fn as_str(&self) -> &'static str {
24        match self {
25            WorkbenchKind::Part => "part",
26            WorkbenchKind::Sketch => "sketch",
27            WorkbenchKind::Operation => "op",
28        }
29    }
30}
31
32/// Workbench definition, e.g `sketch`, `part` or `op`.
33#[derive(Clone)]
34pub struct WorkbenchDefinition {
35    /// Documentation.
36    pub doc: Option<DocBlock>,
37    /// Workbench attributes.
38    pub attribute_list: AttributeList,
39    /// Visibility from outside modules.
40    pub visibility: Visibility,
41    /// Workbench kind.
42    pub kind: Refer<WorkbenchKind>,
43    /// Workbench name.
44    pub id: Identifier,
45    /// Workbench's building plan.
46    pub plan: ParameterList,
47    /// Workbench body
48    pub body: Body,
49}
50
51impl<'a> Initialized<'a> for WorkbenchDefinition {
52    fn statements(&'a self) -> std::slice::Iter<'a, Statement> {
53        self.body.statements.iter()
54    }
55}
56
57impl SrcReferrer for WorkbenchDefinition {
58    fn src_ref(&self) -> SrcRef {
59        self.id.src_ref()
60    }
61}
62
63impl std::fmt::Display for WorkbenchDefinition {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        write!(
66            f,
67            "{visibility}{kind} {id}({plan}) {body}",
68            visibility = self.visibility,
69            kind = self.kind,
70            id = self.id,
71            plan = self.plan,
72            body = self.body
73        )
74    }
75}
76
77impl std::fmt::Debug for WorkbenchDefinition {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        write!(
80            f,
81            "{visibility}{kind} {id:?}({plan:?}) {body:?}",
82            visibility = self.visibility,
83            kind = self.kind,
84            id = self.id,
85            plan = self.plan,
86            body = self.body
87        )
88    }
89}
90
91impl TreeDisplay for WorkbenchDefinition {
92    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
93        writeln!(
94            f,
95            "{:depth$}{visibility}Workbench ({kind}) '{id}':",
96            "",
97            visibility = self.visibility,
98            kind = self.kind,
99            id = self.id
100        )?;
101        depth.indent();
102        if let Some(doc) = &self.doc {
103            doc.tree_print(f, depth)?;
104        }
105        self.plan.tree_print(f, depth)?;
106        self.body.tree_print(f, depth)
107    }
108}
109
110impl Doc for WorkbenchDefinition {
111    fn doc(&self) -> Option<DocBlock> {
112        self.doc.clone()
113    }
114}