scadman 0.2.1

Code generator for OpenSCAD.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Internal module.
//! Macros in this module are not intended for public use,
//! despite being public.

use std::fmt::{Display, Formatter};

use crate::{
    common::{ScadObjectImpl, INDENT},
    scad_display::ScadDisplay,
};

/// Indent a string
///
/// # Arguments
///
/// + `s` - The string to be indented
/// + `indent` - The number of spaces to indent each line
///
/// # Returns
///
/// A new string with each line indented by the specified number of spaces
pub fn indent_str(s: &str, indent: usize) -> String {
    let mut lines: Vec<_> = s
        .lines()
        .map(|line| format!("{}{}", " ".repeat(indent), line))
        .collect();
    if s.ends_with('\n') {
        lines.push(String::new());
    }
    lines.join("\n")
}

/// Common code for primitive representation
///
/// # Arguments
///
/// + `body` - The SCAD sentence to be represented as a primitive
///
/// # Returns
///
/// A string representation of the primitive SCAD object, ending with a semicolon and newline
pub fn primitive_repr<T: ScadDisplay>(body: &T) -> String {
    format!("{};\n", body.repr_scad())
}

/// Represent a modifier with its child object in SCAD
///
/// - If the child's representation starts with '{{', the modifier is placed directly before the block
/// - Otherwise, the child is indented and placed on a new line after the modifier
///
/// # Arguments
///
/// * `body` - The modifier to be applied
/// * `child` - The child object to which the modifier is applied
///
/// # Returns
///
/// A string representation of the modifier and its child
pub fn modifier_repr<T: ScadDisplay>(body: &T, child: &ScadObjectImpl) -> String {
    let body_repr = body.repr_scad();
    let child_repr = child.to_code();
    if child_repr.chars().next().unwrap_or_default() == '{' {
        format!("{body_repr} {child_repr}")
    } else {
        let indented_child = indent_str(&child_repr, INDENT);
        format!("{body_repr}\n{indented_child}")
    }
}

/// Represent a block of SCAD objects
///
/// # Arguments
///
/// * `objects` - A slice of SCAD objects to be included in the block
///
/// # Returns
///
/// A string representation of the block, with objects indented and enclosed in curly braces
pub fn block_repr(objects: &[ScadObjectImpl]) -> String {
    let children_repr = objects
        .iter()
        .map(ScadObjectImpl::to_code)
        .collect::<String>();
    let indented_children = indent_str(&children_repr, INDENT);
    format!("{{\n{indented_children}}}\n")
}

/// Single option with a SCAD object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScadOption {
    /// Single value, no key
    Value(String),
    /// Key-value pair
    KeyValue((String, String)),
}

impl Display for ScadOption {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Value(v) => Display::fmt(&v, f),
            Self::KeyValue((k, v)) => {
                Display::fmt(&k, f)?;
                write!(f, " = ")?;
                Display::fmt(&v, f)
            }
        }
    }
}

impl ScadOption {
    /// Create a new option with a key-value pair.
    /// This function create an unnamed option if the `name` is empty string.
    ///
    /// # Arguments
    ///
    /// + `name` - name of the option, empty string for unnamed option
    /// + `value` - value of the option
    ///
    /// # Returns
    ///
    /// New [`ScadOption`]
    ///
    /// # Examples
    ///
    /// ```text
    /// use scadman::internal::ScadOption;
    /// assert_eq!(ScadOption::from_key_value("key", false),
    ///            ScadOption::KeyValue(("key".to_string(), "false".to_string())));
    /// assert_eq!(ScadOption::from_key_value("", true), ScadOption::Value("true".to_string()));
    /// ```
    pub fn from_key_value<T: ScadDisplay>(name: &str, value: T) -> Self {
        if name.is_empty() {
            Self::Value(value.repr_scad())
        } else {
            Self::KeyValue((name.to_string(), value.repr_scad()))
        }
    }

    /// Create a new option with a key-value pair.
    /// This function returns `None` if the `value` is `None`.
    /// This function create an unnamed option if the `name` is empty string.
    ///
    /// # Argument
    ///
    /// + `name` - name of the option, empty string for unnamed option
    /// + `value` - value of the option, `None` to fail.
    ///
    /// # Returns
    ///
    /// + [`ScadOption`] - if the `value` is [`Some<T>`]
    /// + [`None`] - if the `value` is [`None`]
    ///
    /// # Examples
    ///
    /// ```text
    /// use scadman::internal::ScadOption;
    /// assert_eq!(ScadOption::from_key_value_option("key", Some(false)),
    ///            Some(ScadOption::KeyValue(("key".to_string(), "false".to_string()))));
    /// assert_eq!(ScadOption::from_key_value_option::<bool>("key", None), None);
    /// ```
    pub fn from_key_value_option<T: ScadDisplay>(name: &str, value: Option<T>) -> Option<Self> {
        Some(Self::from_key_value(name, value?))
    }
}

/// Create a [`Vec<ScadOption>`] from key-value pairs with arbitrarily
/// [`impl ScadDisplay`] value.
///
/// This macro accepts the common calling forms used across the codebase:
/// - `__generate_scad_options!( (name, value) );`
/// - `__generate_scad_options!( (req1, v1); (req2, v2); );`
/// - `__generate_scad_options!( (req...), ; (opt1, v1); (opt2, v2); );`
/// - It also tolerates `;;` (empty optional list) and trailing semicolons.
///
/// To avoid parsing ambiguity between overlapping matcher arms, the macro
/// matches the key/name positions as `expr` and provides only a single
/// semicolon-separated pattern. This form matches callers such as:
///   ("size", self.size) ; ("center", self.center);
#[doc(hidden)]
#[macro_export]
macro_rules! __generate_scad_options {
    // Semicolon-separated form only: required ; optional
    // Use `expr` for key positions to allow literals, method calls and
    // identifiers uniformly. Optional pairs are placed into an explicit
    // `opt: ( ... )` group to avoid parsing ambiguity between the
    // required and optional sequences.
    ( $( ($name:expr_2021, $value:expr_2021) );* $(;)? $( opt: ( $( ($oname:expr_2021, $ovalue:expr_2021) );* $(;)? ) )? ) => {
        {
            #[allow(unused_mut)]
            let mut opts: Vec<$crate::internal::ScadOption> = vec![
                $(
                    $crate::internal::ScadOption::from_key_value(&$name, $value),
                )*
            ];
            $(
                $(
                    if let Some(opt) = $crate::internal::ScadOption::from_key_value_option(&$oname, $ovalue) {
                        opts.push(opt);
                    }
                )*
            )?
            opts
        }
    };

    // Fallback: no options
    () => {
        {
            Vec::<$crate::internal::ScadOption>::new()
        }
    };
}

/// Generate a SCAD code for a SCAD object with [`ScadOption`]s.
///
/// # Arguments
///
/// + `name` - name of the SCAD object
/// + `opts` - [`ScadOption`]s of the SCAD object
///
/// # Returns
///
/// SCAD code of the SCAD object
///
/// # Examples
///
/// ```text
/// use scadman::{scad::Unit, internal::ScadOption};
/// let opts = vec![
///    ScadOption::from_key_value("size", 1 as Unit),
///    ScadOption::from_key_value("center", true),
/// ];
/// assert_eq!(generate_body("square", opts), "square(size = 1, center = true)");
/// ```
pub fn generate_sentence_repr(name: &str, opts: Vec<ScadOption>) -> String {
    let reprs = opts.iter().map(ToString::to_string).collect::<Vec<_>>();
    format!("{}({})", name, reprs.join(", "))
}

/// Macro to implement chaining `apply()` for modifiers.
#[doc(hidden)]
#[macro_export]
macro_rules! __impl_modifier_chaining {
    ($mod_ty:ident) => {
        impl $mod_ty {
            /// Apply this modifier and return itself for chaining.
            pub const fn apply(self) -> Self {
                self
            }
        }
    };
}

/// Macro to implement panicking `to_code` and `From` for modifiers.
/// This ensures that attempting to treat a modifier as a complete `ScadObject`
/// without a child will cause a runtime panic, as expected by some tests.
#[doc(hidden)]
#[macro_export]
macro_rules! __impl_panicking_modifier_methods {
    ($mod_ty:ident, $dim_marker:ty) => {
        impl $mod_ty {
            /// Generate SCAD code from this SCAD object.
            ///
            /// # Exceptions
            ///
            /// Panics because the object is modifier without child objects.
            pub fn to_code(&self) -> String {
                panic!(
                    "A modifier cannot be converted to SCAD code directly without a child object. Use .apply_to() or similar methods."
                );
            }
        }

        impl From<$mod_ty> for $crate::common::ScadObjectGeneric<$dim_marker> {
            fn from(_: $mod_ty) -> Self {
                panic!(
                    "A modifier cannot be converted to SCAD code directly without a child object. Use .apply_to() or similar methods."
                );
            }
        }
    };
}

/// TODO: doc
#[doc(hidden)]
#[macro_export]
macro_rules! __impl_primitive_to_code {
    ($prim_ty:ident, $dim_marker:ty) => {
        impl $prim_ty {
            /// Generate SCAD code from this SCAD object.
            ///
            /// # Returns
            ///
            /// [`String`] represents code for OpenSCAD.
            pub fn to_code(&self) -> String {
                let obj: ScadObjectGeneric<$dim_marker> = self.clone().into();
                obj.to_code()
            }
        }
    };
}

/// Macro to implement `apply_to` for modifiers.
///
/// Parameters:
/// - $method_name: name of the method (e.g., `apply_to`)
/// - $mod_ty: modifier sentence type (e.g., `Translate3D`)
/// - $body_ty: concrete ScadModifierBody type path (e.g., `crate::scad_3d::ScadModifierBody3D`)
/// - $typed_output_obj: typed wrapper return type (e.g., `crate::common::ScadObject3D`)
/// - $mod_impl_ty: concrete ScadModifier type path (e.g., `crate::scad_3d::ScadModifier3D`)
/// - $obj_enum_ty: concrete ScadObject enum type (e.g., `crate::scad_3d::ScadObject3D`)
/// - $impl_variant: ScadObjectImpl variant constructor path
///   (e.g., `crate::common::ScadObjectImpl::Object3D` or `::Object2D`)
/// - $output_marker: dimension marker type for the output (e.g., `crate::common::D3` / `D2`)
/// - $child_marker: dimension marker type for the child (e.g., `crate::common::D3` / `D2`)
///
/// The generated method accepts `child: impl Into<ScadObjectGeneric<$child_marker>>`
/// so callers can pass either the typed wrapper, builder results that implement
/// `Scad`, or the legacy untyped runtime `ScadObject`.
#[doc(hidden)]
#[macro_export]
macro_rules! __impl_apply_to_modifier {
    (
        $method_name:ident,
        $mod_ty:ident,
        $body_ty:path,
        $typed_output_obj:path,
        $mod_impl_ty:path,
        $obj_enum_ty:path,
        $impl_variant:path,
        $output_marker:path,
        $child_marker:path
    ) => {
        impl $mod_ty {
            /// apply this modifier to a child object
            ///
            /// # Arguments
            ///
            /// - `child` - the child object to apply the modifier to
            ///
            /// # Returns
            ///
            /// A new object with the modifier applied.
            pub fn $method_name<T: Into<$crate::common::ScadObjectGeneric<$child_marker>>>(
                self,
                child: T,
            ) -> $typed_output_obj {
                use std::rc::Rc;

                // Convert the input into the appropriate typed wrapper. This may perform a runtime
                // check and panic if dimensions mismatch (preserving existing behavior).
                let untyped_child_generic: $crate::common::ScadObjectGeneric<$child_marker> =
                    child.into();

                // Extract the inner runtime implementation Rc to pass to the modifier.
                let child_impl_rc: Rc<$crate::common::ScadObjectImpl> =
                    Rc::clone(&untyped_child_generic.inner);

                // Assert correct dimension at runtime as before.
                assert!(
                    child_impl_rc.get_type() == <$child_marker>::MARKER,
                    "Modifier requires: {:?}, but received: {:?}",
                    <$child_marker>::MARKER,
                    child_impl_rc.get_type()
                );

                // Convert this sentence into the modifier body expected by the target scad module.
                let body: $body_ty = self.into();

                // Try to construct the modifier; keep the existing panic message on failure.
                let m = <$mod_impl_ty>::try_new(body.clone(), child_impl_rc).unwrap_or_else(|| {
                    panic!(
                        "Modifier {:?} requires: {:?}",
                        body,
                        body.get_children_type()
                    )
                });

                // Wrap into the concrete object enum and then into the runtime implementation variant.
                let o = <$obj_enum_ty>::Modifier(m);
                let rc_impl = Rc::new($impl_variant(Rc::new(o)));

                // Convert rc_impl into the typed wrapper using from_impl and the provided marker.
                $crate::common::ScadObjectGeneric::<$output_marker>::from_impl(rc_impl)
            }
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __impl_builder_primitive {
    ( $type:ident, $dim_marker:ty ) => {
        paste::paste! {
            impl $crate::common::ScadBuildable for $type {
                type Target = $crate::common::ScadObjectGeneric<$dim_marker>;
                type Builder = [<$type Builder>];
            }

            impl $crate::common::ScadBuilder for [<$type Builder>] {
                type Sentence = $type;
                type Error = [<$type BuilderError>];
                fn build_scad(&self) -> Result<Self::Sentence, Self::Error> {
                    Self::build(&self)
                }
            }
        }
    };
}

/// implement [`ScadSentnece`] and [`ScadBuilder`] for certain type
#[doc(hidden)]
#[macro_export]
macro_rules! __impl_builder_modifier {
    ( $type:ident ) => {
        paste::paste! {
            impl $crate::common::ScadBuildable for $type {
                type Target = $type;
                type Builder = [<$type Builder>];
            }

            impl $crate::common::ScadBuilder for [<$type Builder>] {
                type Sentence = $type;
                type Error = [<$type BuilderError>];
                fn build_scad(&self) -> Result<Self::Sentence, Self::Error> {
                    Self::build(&self)
                }
            }
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! __impl_from_scad_for_collection_with_try_new {
    (
        $dim_marker:ty,         // e.g., D2
        $scad_block_ty:ty,      // e.g., ScadBlock2D
        $scad_object_enum_ty:ty,// e.g., ScadObject2D
        $scad_object_impl_variant:path, // e.g., ScadObjectImpl::Object2D
        $expect_msg:literal     // e.g., "Internal error: ..."
    ) => {
        impl<T> From<&[T]> for $crate::common::ScadObjectGeneric<$dim_marker>
        where
            T: Into<$crate::common::ScadObjectGeneric<$dim_marker>> + Clone,
        {
            fn from(item: &[T]) -> Self {
                let objects_rc_impl: Vec<std::rc::Rc<$crate::common::ScadObjectImpl>> =
                    item.iter().map(|item| item.clone().into().inner).collect();
                let objects_impl: Vec<$crate::common::ScadObjectImpl> = objects_rc_impl
                    .into_iter()
                    .map(|rc| Rc::unwrap_or_clone(rc))
                    .collect();

                let block = <$scad_block_ty>::try_new(&objects_impl).expect($expect_msg);

                let o = <$scad_object_enum_ty>::Block(block);
                let rc_impl = std::rc::Rc::new($scad_object_impl_variant(std::rc::Rc::new(o)));
                $crate::common::ScadObjectGeneric::from_impl(rc_impl)
            }
        }

        impl<T, const N: usize> From<[T; N]> for $crate::common::ScadObjectGeneric<$dim_marker>
        where
            T: Into<$crate::common::ScadObjectGeneric<$dim_marker>> + Clone,
        {
            fn from(item: [T; N]) -> Self {
                item[..].into()
            }
        }

        impl<T> From<Vec<T>> for $crate::common::ScadObjectGeneric<$dim_marker>
        where
            T: Into<$crate::common::ScadObjectGeneric<$dim_marker>> + Clone,
        {
            fn from(item: Vec<T>) -> Self {
                item.as_slice().into()
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use std::rc::Rc;

    use crate::common::Unit;

    use super::*;

    #[test]
    fn test_indent_str() {
        assert_eq!(indent_str("hello\nworld!\n", 3), "   hello\n   world!\n");
        assert_eq!(
            indent_str("   hello\n  world!", 2),
            "     hello\n    world!"
        );
        assert_eq!(indent_str("\n\n\n", 1), " \n \n \n");
    }

    struct ScadDisplayMock(String);
    impl ScadDisplay for ScadDisplayMock {
        fn repr_scad(&self) -> String {
            self.0.clone()
        }
    }

    #[test]
    fn test_primitive_repr() {
        assert_eq!(
            primitive_repr(&ScadDisplayMock("prim()".to_string())),
            "prim();\n"
        );
    }

    #[derive(Clone)]
    struct ScadObjectMock(String);
    impl ScadObjectMock {
        fn inner(self) -> ScadObjectImpl {
            struct Inner(String);
            impl crate::common::ScadObjectReprMixed for Inner {
                fn to_code(&self) -> String {
                    self.0.clone()
                }
                fn as_any(&self) -> &dyn std::any::Any {
                    &self.0
                }
            }
            ScadObjectImpl::ObjectMixed(Rc::new(Inner(self.0)))
        }
    }

    #[test]
    fn test_modifier_repr() {
        assert_eq!(
            modifier_repr(
                &ScadDisplayMock("mod()".to_string()),
                &ScadObjectMock("prim();\n".to_string()).inner()
            ),
            "mod()\n  prim();\n"
        );
        assert_eq!(
            modifier_repr(
                &ScadDisplayMock("mod()".to_string()),
                &ScadObjectMock("mod2()\n  prim();\n".to_string()).inner()
            ),
            "mod()\n  mod2()\n    prim();\n"
        );
        assert_eq!(
            modifier_repr(
                &ScadDisplayMock("mod()".to_string()),
                &ScadObjectMock("{\n  prim1();\n  prim2();\n}\n".to_string()).inner()
            ),
            "mod() {\n  prim1();\n  prim2();\n}\n"
        );
        assert_eq!(
            modifier_repr(
                &ScadDisplayMock("mod()".to_string()),
                &ScadObjectMock("/* comment */\n{\n  prim1();\n  prim2();\n}\n".to_string())
                    .inner()
            ),
            "mod()\n  /* comment */\n  {\n    prim1();\n    prim2();\n  }\n"
        );
        assert_eq!(
            modifier_repr(
                &ScadDisplayMock("mod1()".to_string()),
                &ScadObjectMock(
                    "{\n  /* comment */\n  mod2(){\n    prim2();\n  }\n}\n".to_string()
                )
                .inner()
            ),
            "mod1() {\n  /* comment */\n  mod2(){\n    prim2();\n  }\n}\n"
        );
    }

    // TODO: test block_repr
    #[test]
    fn test_block_repr() {
        assert_eq!(
            block_repr(&[
                ScadObjectMock("prim1();\n".to_string()).inner(),
                ScadObjectMock("mod()\n  prim2();\n".to_string()).inner()
            ]),
            "{\n  prim1();\n  mod()\n    prim2();\n}\n"
        );
    }

    #[test]
    fn test_scad_option() {
        assert_eq!(
            ScadOption::from_key_value("key", false),
            ScadOption::KeyValue(("key".to_string(), "false".to_string()))
        );
        assert_eq!(
            ScadOption::from_key_value("", true),
            ScadOption::Value("true".to_string())
        );

        assert_eq!(
            ScadOption::from_key_value_option("key", Some(false)),
            Some(ScadOption::KeyValue((
                "key".to_string(),
                "false".to_string()
            )))
        );
        assert_eq!(ScadOption::from_key_value_option::<bool>("key", None), None);
    }

    #[test]
    fn test_generate_body() {
        let opts = vec![
            ScadOption::from_key_value("size", Unit::from(1)),
            ScadOption::from_key_value("center", true),
        ];
        assert_eq!(
            generate_sentence_repr("square", opts),
            "square(size = 1, center = true)"
        );
    }
}