microcad-lang 0.5.0

µcad language
Documentation
// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Work piece element

use microcad_lang_base::{Hashed, Identifier};

use crate::{model::*, value::*};

/// A workpiece is an element produced by a workbench.
#[derive(Debug, Clone)]
pub struct Workpiece {
    /// Workpiece kind: `op`, `sketch`, `part`.
    pub kind: WorkbenchKind,
    /// Workpiece properties.
    pub properties: Properties,
    /// Creator symbol.
    pub creator: Hashed<Creator>,
}

impl std::fmt::Display for Workpiece {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Workpiece({}) {}", self.kind, *self.creator)
    }
}

impl ComputedHash for Workpiece {
    fn computed_hash(&self) -> HashId {
        self.creator.computed_hash()
    }
}

impl PropertiesAccess for Workpiece {
    fn get_property(&self, id: &Identifier) -> Option<&Value> {
        self.properties.get(id)
    }

    fn set_property(&mut self, id: Identifier, value: Value) -> Option<Value> {
        self.properties.insert(id, value)
    }
    fn get_properties(&self) -> Option<&Properties> {
        Some(&self.properties)
    }

    fn add_properties(&mut self, props: Properties) {
        self.properties
            .extend(props.iter().map(|(id, prop)| (id.clone(), prop.clone())));
    }
}