From 4c98cc736b7edf1f5d6c44a5b118dfd610e926f6 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 25 Nov 2024 22:42:13 +0100 Subject: [PATCH] Require `Operation` impls to implement `Display` --- .../2024-10-30/src/geometry/operation.rs | 4 ++- .../2024-10-30/src/geometry/ops_log.rs | 26 +++++++++++++++++++ .../2024-10-30/src/geometry/primitives.rs | 16 ++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/experiments/2024-10-30/src/geometry/operation.rs b/experiments/2024-10-30/src/geometry/operation.rs index add51048a..27796e2d0 100644 --- a/experiments/2024-10-30/src/geometry/operation.rs +++ b/experiments/2024-10-30/src/geometry/operation.rs @@ -1,6 +1,8 @@ +use std::fmt; + use super::{Triangle, Vertex}; -pub trait Operation { +pub trait Operation: fmt::Display { fn vertices(&self, vertices: &mut Vec); fn triangles(&self, triangles: &mut Vec); } diff --git a/experiments/2024-10-30/src/geometry/ops_log.rs b/experiments/2024-10-30/src/geometry/ops_log.rs index 20fca6808..27e98c39e 100644 --- a/experiments/2024-10-30/src/geometry/ops_log.rs +++ b/experiments/2024-10-30/src/geometry/ops_log.rs @@ -1,3 +1,5 @@ +use std::fmt; + use tuples::CombinRight; use super::{Operation, Triangle, Vertex}; @@ -49,6 +51,16 @@ impl OpsLog { } } +impl fmt::Display for OpsLog { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(op) = self.operations.last() { + op.fmt(f) + } else { + write!(f, "empty operations log") + } + } +} + impl Operation for OpsLog { fn vertices(&self, vertices: &mut Vec) { if let Some(op) = self.operations.last() { @@ -84,6 +96,12 @@ impl Operation for OperationInSequence { } } +impl fmt::Display for OperationInSequence { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.operation.fmt(f) + } +} + pub struct OperationResult<'r, T> { operations: &'r mut OpsLog, results: T, @@ -131,6 +149,7 @@ impl<'r, T> OperationResult<'r, T> { } pub struct ClonedOperation { + pub description: String, pub vertices: Vec, pub triangles: Vec, } @@ -144,12 +163,19 @@ impl ClonedOperation { op.triangles(&mut triangles); Self { + description: op.to_string(), vertices, triangles, } } } +impl fmt::Display for ClonedOperation { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.description) + } +} + impl Operation for ClonedOperation { fn vertices(&self, vertices: &mut Vec) { vertices.extend(&self.vertices); diff --git a/experiments/2024-10-30/src/geometry/primitives.rs b/experiments/2024-10-30/src/geometry/primitives.rs index 3f1590e43..71f004d5a 100644 --- a/experiments/2024-10-30/src/geometry/primitives.rs +++ b/experiments/2024-10-30/src/geometry/primitives.rs @@ -1,3 +1,5 @@ +use std::fmt; + use crate::math::Point; use super::Operation; @@ -18,6 +20,13 @@ where } } +impl fmt::Display for Vertex { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let [x, y, z] = self.point.coords.components.map(|s| s.value()); + write!(f, "vertex {x:.2}, {y:.2}, {z:.2}") + } +} + impl Operation for Vertex { fn vertices(&self, vertices: &mut Vec) { vertices.push(*self); @@ -42,6 +51,13 @@ where } } +impl fmt::Display for Triangle { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let [a, b, c] = self.vertices; + write!(f, "triangle {a} - {b} - {c}") + } +} + impl Operation for Triangle { fn vertices(&self, _: &mut Vec) {}