1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum StoreError {
9 InvalidVertex(u32),
11 InvalidTriangle(u32),
13 VertexInUse { vertex: u32, corners: u32 },
15 LengthMismatch { expected: usize, got: usize },
17 InvalidArgument(&'static str),
19 UnknownChannel(String),
21 ChannelExists(String),
23 NotSourceChannel(String),
25 NotDerivedChannel(String),
27 PatchMismatch,
29 StalePatch,
31 TransactionPoisoned,
33 OutOfMemory,
35}
36
37impl fmt::Display for StoreError {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 match self {
40 Self::InvalidVertex(v) => write!(f, "vertex {v} does not exist"),
41 Self::InvalidTriangle(t) => write!(f, "triangle {t} does not exist"),
42 Self::VertexInUse { vertex, corners } => {
43 write!(
44 f,
45 "vertex {vertex} is still used by {corners} triangle corners"
46 )
47 }
48 Self::LengthMismatch { expected, got } => {
49 write!(f, "expected length {expected}, got {got}")
50 }
51 Self::InvalidArgument(msg) => write!(f, "invalid argument: {msg}"),
52 Self::UnknownChannel(name) => write!(f, "channel \"{name}\" does not exist"),
53 Self::ChannelExists(name) => write!(f, "channel \"{name}\" already exists"),
54 Self::NotSourceChannel(name) => write!(f, "channel \"{name}\" is not a source channel"),
55 Self::NotDerivedChannel(name) => {
56 write!(f, "channel \"{name}\" is not a derived channel")
57 }
58 Self::PatchMismatch => write!(f, "patch does not match this mesh's current revision"),
59 Self::StalePatch => write!(f, "patch was created before the mesh was compacted"),
60 Self::TransactionPoisoned => {
61 write!(
62 f,
63 "transaction was rolled back because an earlier operation failed"
64 )
65 }
66 Self::OutOfMemory => write!(f, "out of memory"),
67 }
68 }
69}
70
71impl std::error::Error for StoreError {}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn messages_name_the_problem() {
79 assert_eq!(
80 StoreError::InvalidVertex(7).to_string(),
81 "vertex 7 does not exist"
82 );
83 assert_eq!(
84 StoreError::VertexInUse {
85 vertex: 3,
86 corners: 2
87 }
88 .to_string(),
89 "vertex 3 is still used by 2 triangle corners"
90 );
91 assert_eq!(
92 StoreError::UnknownChannel("mask".into()).to_string(),
93 "channel \"mask\" does not exist"
94 );
95 }
96}