Require Operation impls to implement Display

This commit is contained in:
Hanno Braun 2024-11-25 22:42:13 +01:00
parent 612109a895
commit 4c98cc736b
3 changed files with 45 additions and 1 deletions

View File

@ -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<Vertex>);
fn triangles(&self, triangles: &mut Vec<Triangle>);
}

View File

@ -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<Vertex>) {
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<Vertex>,
pub triangles: Vec<Triangle>,
}
@ -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<Vertex>) {
vertices.extend(&self.vertices);

View File

@ -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<Vertex>) {
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<Vertex>) {}