summaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2025-07-24 23:18:06 -0400
committerOwen Jacobson <owen@grimoire.ca>2025-07-24 23:53:37 -0400
commitcae0d11fa25160b38a411d4edd8a0f3b5b06df8c (patch)
treeec90a0b7fbb5ccc9fda1fb8f2a81672d2587f9fc /src/error.rs
parentaec3eaeebd37bce9ab4dad14e7e86ef0db8f0c2d (diff)
Define ID types as specializations, rather than newtypes.
This is based heavily on the work done for normalized strings, in `crate::normalize`. The key realization in that module is that the logic distinguishing one kind of thing (normalized strings in that case, IDs, in this case) can be packaged up as a type token, and that doing so may reduce the overall complexity. This implementation for ID also borrows heavily from the implementation for normalized strings. It's less flexible: an ID implemented this way can't expose _less_ of `crate::id::ID`'s interface, whereas newtype wrappers can, for example. However, our code doesn't use that flexiblity on purpose anywhere and we're relatively unlikely to change that. In return, the individual ID types require substantially less code - they do not, for example, need to re-implement `Display` for themselves. I very nearly made the trait `Prefix`: ```rust pub trait Prefix { const PREFIX: &str; } ``` however, I think having an effectively-constant method is less surprising overall.
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs25
1 files changed, 6 insertions, 19 deletions
diff --git a/src/error.rs b/src/error.rs
index 7483f00..3c46097 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -5,8 +5,6 @@ use axum::{
response::{IntoResponse, Response},
};
-use crate::id::Id as BaseId;
-
// I'm making an effort to avoid `anyhow` here, as that crate is _enormously_
// complex (though very usable). We don't need to be overly careful about
// allocations on errors in this app, so this is fine for most "general
@@ -45,25 +43,14 @@ impl IntoResponse for Internal {
}
}
-// Transient identifier for an InternalError. Prefixed with `E`.
-#[derive(Debug)]
-pub struct Id(BaseId);
-
-impl From<BaseId> for Id {
- fn from(id: BaseId) -> Self {
- Self(id)
- }
-}
+pub type Id = crate::id::Id<InternalError>;
-impl Id {
- pub fn generate() -> Self {
- BaseId::generate("E")
- }
-}
+#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
+pub struct InternalError;
-impl fmt::Display for Id {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- self.0.fmt(f)
+impl crate::id::Prefix for InternalError {
+ fn prefix(&self) -> &'static str {
+ "E"
}
}