From cae0d11fa25160b38a411d4edd8a0f3b5b06df8c Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Thu, 24 Jul 2025 23:18:06 -0400 Subject: 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. --- src/user/id.rs | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) (limited to 'src/user/id.rs') diff --git a/src/user/id.rs b/src/user/id.rs index bc14c1f..3ad8d16 100644 --- a/src/user/id.rs +++ b/src/user/id.rs @@ -1,25 +1,12 @@ -use crate::id::Id as BaseId; - // Stable identifier for a User. Prefixed with `U`. Users created before March, 2025 may have an `L` // prefix, instead. -#[derive(Clone, Debug, Eq, PartialEq, sqlx::Type, serde::Serialize)] -#[sqlx(transparent)] -pub struct Id(BaseId); - -impl From for Id { - fn from(id: BaseId) -> Self { - Self(id) - } -} +pub type Id = crate::id::Id; -impl Id { - pub fn generate() -> Self { - BaseId::generate("U") - } -} +#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub struct User; -impl std::fmt::Display for Id { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) +impl crate::id::Prefix for User { + fn prefix(&self) -> &'static str { + "U" } } -- cgit v1.2.3