kcl-lib 0.2.144

KittyCAD Language implementation and tools
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Types for kcl project and modeling-app settings.

pub mod project;

use anyhow::Result;
use kittycad_modeling_cmds::units::UnitLength;
use parse_display::Display;
use parse_display::FromStr;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use validator::Validate;

const DEFAULT_PROJECT_NAME_TEMPLATE: &str = "untitled";

/// User specific settings for the app.
/// These live in `user.toml` in the app's configuration directory.
/// Updating the settings in the app will update this file automatically.
/// Do not edit this file manually, as it may be overwritten by the app.
/// Manual edits can cause corruption of the settings file.
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Validate)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
pub struct Configuration {
    /// The settings for the Design Studio.
    #[serde(default, skip_serializing_if = "is_default")]
    #[validate(nested)]
    pub settings: Settings,
}

impl Configuration {
    pub fn parse_and_validate(toml_str: &str) -> Result<Self> {
        let settings = toml::from_str::<Self>(toml_str)?;

        settings.validate()?;

        Ok(settings)
    }
}

/// High level settings.
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Validate)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
pub struct Settings {
    /// The settings for the Design Studio.
    #[serde(default, skip_serializing_if = "is_default")]
    #[validate(nested)]
    pub app: AppSettings,
    /// Settings that affect the behavior while modeling.
    #[serde(default, skip_serializing_if = "is_default")]
    #[validate(nested)]
    pub modeling: ModelingSettings,
    /// Settings that affect the behavior of the KCL text editor.
    #[serde(default, skip_serializing_if = "is_default")]
    #[validate(nested)]
    pub text_editor: TextEditorSettings,
    /// Settings that affect the behavior of project management.
    #[serde(default, skip_serializing_if = "is_default")]
    #[validate(nested)]
    pub project: ProjectSettings,
    /// Settings that affect the behavior of the command bar.
    #[serde(default, skip_serializing_if = "is_default")]
    #[validate(nested)]
    pub command_bar: CommandBarSettings,
}

/// Application wide settings.
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Validate)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
pub struct AppSettings {
    /// The settings for the appearance of the app.
    #[serde(default, skip_serializing_if = "is_default")]
    #[validate(nested)]
    pub appearance: AppearanceSettings,
    /// The onboarding status of the app.
    #[serde(default, skip_serializing_if = "is_default")]
    pub onboarding_status: OnboardingStatus,
    /// When the user is idle, teardown the stream after some time.
    #[serde(
        default,
        deserialize_with = "deserialize_stream_idle_mode",
        alias = "streamIdleMode",
        skip_serializing_if = "is_default"
    )]
    stream_idle_mode: Option<u32>,
    /// Allow orbiting in sketch mode.
    #[serde(default, skip_serializing_if = "is_default")]
    pub allow_orbit_in_sketch_mode: bool,
    /// Whether to show the debug panel, which lets you see various states
    /// of the app to aid in development.
    #[serde(default, skip_serializing_if = "is_default")]
    pub show_debug_panel: bool,
    /// Whether to enable Machine API discovery and printing controls on desktop.
    #[serde(default, skip_serializing_if = "is_default")]
    pub machine_api: bool,
}

/// Default to true.
fn make_it_so() -> bool {
    true
}

fn is_true(b: &bool) -> bool {
    *b
}

fn deserialize_stream_idle_mode<'de, D>(deserializer: D) -> Result<Option<u32>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum StreamIdleModeValue {
        Number(u32),
        String(String),
        Boolean(bool),
    }

    const DEFAULT_TIMEOUT: u32 = 1000 * 60 * 5;

    Ok(match StreamIdleModeValue::deserialize(deserializer) {
        Ok(StreamIdleModeValue::Number(value)) => Some(value),
        Ok(StreamIdleModeValue::String(value)) => Some(value.parse::<u32>().unwrap_or(DEFAULT_TIMEOUT)),
        // The old type of this value. I'm willing to say no one used it but
        // we can never guarantee it.
        Ok(StreamIdleModeValue::Boolean(true)) => Some(DEFAULT_TIMEOUT),
        Ok(StreamIdleModeValue::Boolean(false)) => None,
        _ => None,
    })
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq)]
#[ts(export)]
#[serde(untagged)]
pub enum FloatOrInt {
    String(String),
    Float(f64),
    Int(i64),
}

impl From<FloatOrInt> for f64 {
    fn from(float_or_int: FloatOrInt) -> Self {
        match float_or_int {
            FloatOrInt::String(s) => s.parse().unwrap(),
            FloatOrInt::Float(f) => f,
            FloatOrInt::Int(i) => i as f64,
        }
    }
}

/// The settings for the theme of the app.
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Validate)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
pub struct AppearanceSettings {
    /// The overall theme of the app.
    #[serde(default, skip_serializing_if = "is_default")]
    pub theme: AppTheme,
}

/// The overall appearance of the app.
#[derive(
    Debug, Default, Copy, Clone, Deserialize, Serialize, JsonSchema, Display, FromStr, ts_rs::TS, PartialEq, Eq,
)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum AppTheme {
    /// A light theme.
    Light,
    /// A dark theme.
    Dark,
    /// Use the system theme.
    /// This will use dark theme if the system theme is dark, and light theme if the system theme is light.
    #[default]
    System,
}

impl From<AppTheme> for kittycad::types::Color {
    fn from(theme: AppTheme) -> Self {
        match theme {
            AppTheme::Light => kittycad::types::Color {
                r: 249.0 / 255.0,
                g: 249.0 / 255.0,
                b: 249.0 / 255.0,
                a: 1.0,
            },
            AppTheme::Dark => kittycad::types::Color {
                r: 28.0 / 255.0,
                g: 28.0 / 255.0,
                b: 28.0 / 255.0,
                a: 1.0,
            },
            AppTheme::System => {
                // TODO: Check the system setting for the user.
                todo!()
            }
        }
    }
}

/// Settings that affect the behavior while modeling.
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Validate)]
#[serde(rename_all = "snake_case")]
#[ts(export)]
pub struct ModelingSettings {
    /// The default unit to use in modeling dimensions.
    #[serde(default = "default_length_unit_millimeters", skip_serializing_if = "is_default")]
    pub base_unit: UnitLength,
    /// The projection mode the camera should use while modeling.
    #[serde(default, skip_serializing_if = "is_default")]
    pub camera_projection: CameraProjectionType,
    /// The methodology the camera should use to orbit around the model.
    #[serde(default, skip_serializing_if = "is_default")]
    pub camera_orbit: CameraOrbitType,
    /// The controls for how to navigate the 3D view.
    #[serde(default, skip_serializing_if = "is_default")]
    pub mouse_controls: MouseControlType,
    /// Which type of orientation gizmo to use.
    #[serde(default, skip_serializing_if = "is_default")]
    pub gizmo_type: GizmoType,
    /// Toggle touch controls for 3D view navigation
    #[serde(default, skip_serializing_if = "is_default")]
    pub enable_touch_controls: DefaultTrue,
    /// Default to the experimental solver-based sketch mode for all new sketches.
    #[serde(default, skip_serializing_if = "is_default")]
    pub use_sketch_solve_mode: bool,
    /// Highlight edges of 3D objects?
    #[serde(default, skip_serializing_if = "is_default")]
    pub highlight_edges: DefaultTrue,
    /// Whether or not Screen Space Ambient Occlusion (SSAO) is enabled.
    #[serde(default, skip_serializing_if = "is_default")]
    pub enable_ssao: DefaultTrue,
    /// The default color to use for surface backfaces.
    #[serde(
        default = "default_backface_color",
        skip_serializing_if = "is_default_backface_color"
    )]
    pub backface_color: String,
    /// Whether or not to show a scale grid in the 3D modeling view
    #[serde(default, skip_serializing_if = "is_default")]
    pub show_scale_grid: bool,
    /// When enabled, the grid will use a fixed size based on your selected units rather than automatically scaling with zoom level.
    /// If true, the grid cells will be fixed-size, where the width is your default length unit.
    /// If false, the grid will get larger as you zoom out, and smaller as you zoom in.
    #[serde(default = "make_it_so", skip_serializing_if = "is_true")]
    pub fixed_size_grid: bool,
    /// When enabled, tools like line, rectangle, etc. will snap to the grid.
    #[serde(default, skip_serializing_if = "is_default")]
    pub snap_to_grid: bool,
    /// The space between major grid lines, specified in the current unit.
    #[serde(default, skip_serializing_if = "is_default")]
    pub major_grid_spacing: f64,
    /// The number of minor grid lines per major grid line.
    #[serde(default, skip_serializing_if = "is_default")]
    pub minor_grids_per_major: f64,
    /// The number of snaps between minor grid lines. 1 means snapping to each minor grid line.
    #[serde(default, skip_serializing_if = "is_default")]
    pub snaps_per_minor: f64,
}

fn default_length_unit_millimeters() -> UnitLength {
    UnitLength::Millimeters
}

// Also defined at src/lib/constants.ts#L333-L335
fn default_backface_color() -> String {
    "#00D5FF".to_string()
}

fn is_default_backface_color(color: &String) -> bool {
    *color == default_backface_color()
}

impl Default for ModelingSettings {
    fn default() -> Self {
        Self {
            base_unit: UnitLength::Millimeters,
            camera_projection: Default::default(),
            camera_orbit: Default::default(),
            mouse_controls: Default::default(),
            gizmo_type: Default::default(),
            enable_touch_controls: Default::default(),
            use_sketch_solve_mode: Default::default(),
            highlight_edges: Default::default(),
            enable_ssao: Default::default(),
            backface_color: default_backface_color(),
            show_scale_grid: Default::default(),
            fixed_size_grid: true,
            snap_to_grid: Default::default(),
            major_grid_spacing: Default::default(),
            minor_grids_per_major: Default::default(),
            snaps_per_minor: Default::default(),
        }
    }
}

#[derive(Debug, Copy, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Eq)]
#[ts(export)]
#[serde(transparent)]
pub struct DefaultTrue(pub bool);

impl Default for DefaultTrue {
    fn default() -> Self {
        Self(true)
    }
}

impl From<DefaultTrue> for bool {
    fn from(default_true: DefaultTrue) -> Self {
        default_true.0
    }
}

impl From<bool> for DefaultTrue {
    fn from(b: bool) -> Self {
        Self(b)
    }
}

/// The types of controls for how to navigate the 3D view.
#[derive(Debug, Default, Eq, PartialEq, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, Display, FromStr)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum MouseControlType {
    #[default]
    #[display("zoo")]
    #[serde(rename = "zoo")]
    Zoo,
    #[display("onshape")]
    #[serde(rename = "onshape")]
    OnShape,
    TrackpadFriendly,
    Solidworks,
    Nx,
    Creo,
    #[display("autocad")]
    #[serde(rename = "autocad")]
    AutoCad,
}

/// The types of camera projection for the 3D view.
#[derive(Debug, Default, Eq, PartialEq, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, Display, FromStr)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum CameraProjectionType {
    /// Perspective projection https://en.wikipedia.org/wiki/3D_projection#Perspective_projection
    Perspective,
    /// Orthographic projection https://en.wikipedia.org/wiki/3D_projection#Orthographic_projection
    #[default]
    Orthographic,
}

/// The types of camera orbit methods.
#[derive(Debug, Default, Eq, PartialEq, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, Display, FromStr)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum CameraOrbitType {
    /// Orbit using a spherical camera movement.
    #[default]
    #[display("spherical")]
    Spherical,
    /// Orbit using a trackball camera movement.
    #[display("trackball")]
    Trackball,
}

/// Which type of orientation gizmo to use.
#[derive(Debug, Default, Eq, PartialEq, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, Display, FromStr)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum GizmoType {
    /// 3D cube gizmo
    #[default]
    Cube,
    /// 3-axis gizmo
    Axis,
}

/// Settings that affect the behavior of the KCL text editor.
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Eq, Validate)]
#[serde(rename_all = "snake_case")]
#[ts(export)]
pub struct TextEditorSettings {
    /// Whether to wrap text in the editor or overflow with scroll.
    #[serde(default, skip_serializing_if = "is_default")]
    pub text_wrapping: DefaultTrue,
    /// Whether to make the cursor blink in the editor.
    #[serde(default, skip_serializing_if = "is_default")]
    pub blinking_cursor: DefaultTrue,
}

/// Same as TextEditorSettings but applies to a per-project basis.
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Eq, Validate)]
#[serde(rename_all = "snake_case")]
#[ts(export)]
pub struct ProjectTextEditorSettings {
    /// Whether to wrap text in the editor or overflow with scroll.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub text_wrapping: Option<bool>,
    /// Whether to make the cursor blink in the editor.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub blinking_cursor: Option<bool>,
}

/// Settings that affect the behavior of project management.
#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Eq, Validate)]
#[serde(rename_all = "snake_case")]
#[ts(export)]
pub struct ProjectSettings {
    /// The directory to save and load projects from.
    #[serde(default, skip_serializing_if = "is_default")]
    pub directory: std::path::PathBuf,
    /// The default project name to use when creating a new project.
    #[serde(default, skip_serializing_if = "is_default")]
    pub default_project_name: ProjectNameTemplate,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Eq)]
#[ts(export)]
#[serde(transparent)]
pub struct ProjectNameTemplate(pub String);

impl Default for ProjectNameTemplate {
    fn default() -> Self {
        Self(DEFAULT_PROJECT_NAME_TEMPLATE.to_string())
    }
}

impl From<ProjectNameTemplate> for String {
    fn from(project_name: ProjectNameTemplate) -> Self {
        project_name.0
    }
}

impl From<String> for ProjectNameTemplate {
    fn from(s: String) -> Self {
        Self(s)
    }
}

/// Settings that affect the behavior of the command bar.
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Eq, Validate)]
#[serde(rename_all = "snake_case")]
#[ts(export)]
pub struct CommandBarSettings {
    /// Whether to include settings in the command bar.
    #[serde(default, skip_serializing_if = "is_default")]
    pub include_settings: DefaultTrue,
}

/// Same as CommandBarSettings but applies to a per-project basis.
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, PartialEq, Eq, Validate)]
#[serde(rename_all = "snake_case")]
#[ts(export)]
pub struct ProjectCommandBarSettings {
    /// Whether to include settings in the command bar.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub include_settings: Option<bool>,
}

/// The types of onboarding status.
#[derive(Debug, Default, Eq, PartialEq, Clone, Deserialize, Serialize, JsonSchema, ts_rs::TS, Display, FromStr)]
#[ts(export)]
#[serde(rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum OnboardingStatus {
    /// The unset state.
    #[serde(rename = "")]
    #[display("")]
    Unset,
    /// The user has completed onboarding.
    Completed,
    /// The user has not completed onboarding.
    #[default]
    Incomplete,
    /// The user has dismissed onboarding.
    Dismissed,

    // Desktop Routes
    #[serde(rename = "/desktop")]
    #[display("/desktop")]
    DesktopWelcome,
    #[serde(rename = "/desktop/scene")]
    #[display("/desktop/scene")]
    DesktopScene,
    #[serde(rename = "/desktop/toolbar")]
    #[display("/desktop/toolbar")]
    DesktopToolbar,
    #[serde(rename = "/desktop/text-to-cad")]
    #[display("/desktop/text-to-cad")]
    DesktopTextToCadWelcome,
    #[serde(rename = "/desktop/text-to-cad-prompt")]
    #[display("/desktop/text-to-cad-prompt")]
    DesktopTextToCadPrompt,
    #[serde(rename = "/desktop/feature-tree-pane")]
    #[display("/desktop/feature-tree-pane")]
    DesktopFeatureTreePane,
    #[serde(rename = "/desktop/code-pane")]
    #[display("/desktop/code-pane")]
    DesktopCodePane,
    #[serde(rename = "/desktop/project-pane")]
    #[display("/desktop/project-pane")]
    DesktopProjectFilesPane,
    #[serde(rename = "/desktop/other-panes")]
    #[display("/desktop/other-panes")]
    DesktopOtherPanes,
    #[serde(rename = "/desktop/prompt-to-edit")]
    #[display("/desktop/prompt-to-edit")]
    DesktopPromptToEditWelcome,
    #[serde(rename = "/desktop/prompt-to-edit-prompt")]
    #[display("/desktop/prompt-to-edit-prompt")]
    DesktopPromptToEditPrompt,
    #[serde(rename = "/desktop/prompt-to-edit-result")]
    #[display("/desktop/prompt-to-edit-result")]
    DesktopPromptToEditResult,
    #[serde(rename = "/desktop/imports")]
    #[display("/desktop/imports")]
    DesktopImports,
    #[serde(rename = "/desktop/exports")]
    #[display("/desktop/exports")]
    DesktopExports,
    #[serde(rename = "/desktop/conclusion")]
    #[display("/desktop/conclusion")]
    DesktopConclusion,
}

fn is_default<T: Default + PartialEq>(t: &T) -> bool {
    t == &T::default()
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::AppSettings;
    use super::AppTheme;
    use super::AppearanceSettings;
    use super::CameraProjectionType;
    use super::CommandBarSettings;
    use super::Configuration;
    use super::ModelingSettings;
    use super::MouseControlType;
    use super::OnboardingStatus;
    use super::ProjectNameTemplate;
    use super::ProjectSettings;
    use super::Settings;
    use super::TextEditorSettings;
    use super::UnitLength;
    use super::default_backface_color;

    #[test]
    fn test_settings_empty_file_parses() {
        let empty_settings_file = r#""#;

        let parsed = toml::from_str::<Configuration>(empty_settings_file).unwrap();
        assert_eq!(parsed, Configuration::default());
        assert_eq!(parsed.settings.modeling.backface_color, default_backface_color());

        // Write the file back out.
        let serialized = toml::to_string(&parsed).unwrap();
        assert_eq!(serialized, r#""#);

        let parsed = Configuration::parse_and_validate(empty_settings_file).unwrap();
        assert_eq!(parsed, Configuration::default());
        assert_eq!(parsed.settings.modeling.backface_color, default_backface_color());
    }

    #[test]
    fn test_settings_parse_basic() {
        let settings_file = r#"[settings.app]
default_project_name = "untitled"
directory = ""
onboarding_status = "dismissed"

  [settings.app.appearance]
  theme = "dark"

[settings.modeling]
enable_ssao = false
base_unit = "in"
mouse_controls = "zoo"
camera_projection = "perspective"

[settings.project]
default_project_name = "untitled"
directory = ""

[settings.text_editor]
text_wrapping = true"#;

        let expected = Configuration {
            settings: Settings {
                app: AppSettings {
                    onboarding_status: OnboardingStatus::Dismissed,
                    appearance: AppearanceSettings { theme: AppTheme::Dark },
                    ..Default::default()
                },
                modeling: ModelingSettings {
                    enable_ssao: false.into(),
                    base_unit: UnitLength::Inches,
                    mouse_controls: MouseControlType::Zoo,
                    camera_projection: CameraProjectionType::Perspective,
                    fixed_size_grid: true,
                    ..Default::default()
                },
                project: ProjectSettings {
                    default_project_name: ProjectNameTemplate("untitled".to_string()),
                    directory: "".into(),
                },
                text_editor: TextEditorSettings {
                    text_wrapping: true.into(),
                    ..Default::default()
                },
                command_bar: CommandBarSettings {
                    include_settings: true.into(),
                },
            },
        };
        let parsed = toml::from_str::<Configuration>(settings_file).unwrap();
        assert_eq!(parsed, expected);

        // Write the file back out.
        let serialized = toml::to_string(&parsed).unwrap();
        assert_eq!(
            serialized,
            r#"[settings.app]
onboarding_status = "dismissed"

[settings.app.appearance]
theme = "dark"

[settings.modeling]
base_unit = "in"
camera_projection = "perspective"
enable_ssao = false
"#
        );

        let parsed = Configuration::parse_and_validate(settings_file).unwrap();
        assert_eq!(parsed, expected);
    }

    #[test]
    fn test_settings_backface_color_roundtrip() {
        let settings_file = r##"[settings.modeling]
backface_color = "#112233"
"##;

        let parsed = toml::from_str::<Configuration>(settings_file).unwrap();
        assert_eq!(parsed.settings.modeling.backface_color, "#112233");

        let serialized = toml::to_string(&parsed).unwrap();
        let reparsed = toml::from_str::<Configuration>(&serialized).unwrap();
        assert_eq!(reparsed, parsed);
        assert!(serialized.contains("backface_color = \"#112233\""));
    }
}