Use arrays for multiple geometry (#5770)
* Parse [T] instead of T[] for array types Signed-off-by: Nick Cameron <nrc@ncameron.org> * homogenous arrays, type coercion, remove solid set and sketch set, etc Signed-off-by: Nick Cameron <nrc@ncameron.org> --------- Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
@ -23,8 +23,8 @@ type Point3D = kcmc::shared::Point3d<f64>;
|
||||
#[ts(export)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum Geometry {
|
||||
Sketch(Box<Sketch>),
|
||||
Solid(Box<Solid>),
|
||||
Sketch(Sketch),
|
||||
Solid(Solid),
|
||||
}
|
||||
|
||||
impl Geometry {
|
||||
@ -52,8 +52,8 @@ impl Geometry {
|
||||
#[serde(tag = "type")]
|
||||
#[allow(clippy::vec_box)]
|
||||
pub enum Geometries {
|
||||
Sketches(Vec<Box<Sketch>>),
|
||||
Solids(Vec<Box<Solid>>),
|
||||
Sketches(Vec<Sketch>),
|
||||
Solids(Vec<Solid>),
|
||||
}
|
||||
|
||||
impl From<Geometry> for Geometries {
|
||||
@ -65,150 +65,6 @@ impl From<Geometry> for Geometries {
|
||||
}
|
||||
}
|
||||
|
||||
/// A sketch or a group of sketches.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
#[allow(clippy::vec_box)]
|
||||
pub enum SketchSet {
|
||||
Sketch(Box<Sketch>),
|
||||
Sketches(Vec<Box<Sketch>>),
|
||||
}
|
||||
|
||||
impl SketchSet {
|
||||
pub fn meta(&self) -> Vec<Metadata> {
|
||||
match self {
|
||||
SketchSet::Sketch(sg) => sg.meta.clone(),
|
||||
SketchSet::Sketches(sg) => sg.iter().flat_map(|sg| sg.meta.clone()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SketchSet> for Vec<Sketch> {
|
||||
fn from(value: SketchSet) -> Self {
|
||||
match value {
|
||||
SketchSet::Sketch(sg) => vec![*sg],
|
||||
SketchSet::Sketches(sgs) => sgs.into_iter().map(|sg| *sg).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Sketch> for SketchSet {
|
||||
fn from(sg: Sketch) -> Self {
|
||||
SketchSet::Sketch(Box::new(sg))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<Sketch>> for SketchSet {
|
||||
fn from(sg: Box<Sketch>) -> Self {
|
||||
SketchSet::Sketch(sg)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Sketch>> for SketchSet {
|
||||
fn from(sg: Vec<Sketch>) -> Self {
|
||||
if sg.len() == 1 {
|
||||
SketchSet::Sketch(Box::new(sg[0].clone()))
|
||||
} else {
|
||||
SketchSet::Sketches(sg.into_iter().map(Box::new).collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Box<Sketch>>> for SketchSet {
|
||||
fn from(sg: Vec<Box<Sketch>>) -> Self {
|
||||
if sg.len() == 1 {
|
||||
SketchSet::Sketch(sg[0].clone())
|
||||
} else {
|
||||
SketchSet::Sketches(sg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SketchSet> for Vec<Box<Sketch>> {
|
||||
fn from(sg: SketchSet) -> Self {
|
||||
match sg {
|
||||
SketchSet::Sketch(sg) => vec![sg],
|
||||
SketchSet::Sketches(sgs) => sgs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Sketch> for Vec<Box<Sketch>> {
|
||||
fn from(sg: &Sketch) -> Self {
|
||||
vec![Box::new(sg.clone())]
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<Sketch>> for Vec<Box<Sketch>> {
|
||||
fn from(sg: Box<Sketch>) -> Self {
|
||||
vec![sg]
|
||||
}
|
||||
}
|
||||
|
||||
/// A solid or a group of solids.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
#[allow(clippy::vec_box)]
|
||||
pub enum SolidSet {
|
||||
Solid(Box<Solid>),
|
||||
Solids(Vec<Box<Solid>>),
|
||||
}
|
||||
|
||||
impl From<Solid> for SolidSet {
|
||||
fn from(eg: Solid) -> Self {
|
||||
SolidSet::Solid(Box::new(eg))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<Solid>> for SolidSet {
|
||||
fn from(eg: Box<Solid>) -> Self {
|
||||
SolidSet::Solid(eg)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Solid>> for SolidSet {
|
||||
fn from(eg: Vec<Solid>) -> Self {
|
||||
if eg.len() == 1 {
|
||||
SolidSet::Solid(Box::new(eg[0].clone()))
|
||||
} else {
|
||||
SolidSet::Solids(eg.into_iter().map(Box::new).collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Box<Solid>>> for SolidSet {
|
||||
fn from(eg: Vec<Box<Solid>>) -> Self {
|
||||
if eg.len() == 1 {
|
||||
SolidSet::Solid(eg[0].clone())
|
||||
} else {
|
||||
SolidSet::Solids(eg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SolidSet> for Vec<Box<Solid>> {
|
||||
fn from(eg: SolidSet) -> Self {
|
||||
match eg {
|
||||
SolidSet::Solid(eg) => vec![eg],
|
||||
SolidSet::Solids(egs) => egs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Solid> for Vec<Box<Solid>> {
|
||||
fn from(eg: &Solid) -> Self {
|
||||
vec![Box::new(eg.clone())]
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<Solid>> for Vec<Box<Solid>> {
|
||||
fn from(eg: Box<Solid>) -> Self {
|
||||
vec![eg]
|
||||
}
|
||||
}
|
||||
|
||||
/// Data for an imported geometry.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
@ -228,17 +84,29 @@ pub struct ImportedGeometry {
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
#[allow(clippy::vec_box)]
|
||||
pub enum SolidOrImportedGeometry {
|
||||
Solid(Box<Solid>),
|
||||
ImportedGeometry(Box<ImportedGeometry>),
|
||||
SolidSet(Vec<Box<Solid>>),
|
||||
SolidSet(Vec<Solid>),
|
||||
}
|
||||
|
||||
impl From<SolidOrImportedGeometry> for crate::execution::KclValue {
|
||||
fn from(value: SolidOrImportedGeometry) -> Self {
|
||||
match value {
|
||||
SolidOrImportedGeometry::Solid(s) => crate::execution::KclValue::Solid { value: s },
|
||||
SolidOrImportedGeometry::ImportedGeometry(s) => crate::execution::KclValue::ImportedGeometry(*s),
|
||||
SolidOrImportedGeometry::SolidSet(s) => crate::execution::KclValue::Solids { value: s },
|
||||
SolidOrImportedGeometry::SolidSet(mut s) => {
|
||||
if s.len() == 1 {
|
||||
crate::execution::KclValue::Solid {
|
||||
value: Box::new(s.pop().unwrap()),
|
||||
}
|
||||
} else {
|
||||
crate::execution::KclValue::HomArray {
|
||||
value: s
|
||||
.into_iter()
|
||||
.map(|s| crate::execution::KclValue::Solid { value: Box::new(s) })
|
||||
.collect(),
|
||||
ty: crate::execution::PrimitiveType::Solid,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -246,7 +114,6 @@ impl From<SolidOrImportedGeometry> for crate::execution::KclValue {
|
||||
impl SolidOrImportedGeometry {
|
||||
pub(crate) fn ids(&self) -> Vec<uuid::Uuid> {
|
||||
match self {
|
||||
SolidOrImportedGeometry::Solid(s) => vec![s.id],
|
||||
SolidOrImportedGeometry::ImportedGeometry(s) => vec![s.id],
|
||||
SolidOrImportedGeometry::SolidSet(s) => s.iter().map(|s| s.id).collect(),
|
||||
}
|
||||
|
Reference in New Issue
Block a user