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/token/id.rs | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) (limited to 'src/token') diff --git a/src/token/id.rs b/src/token/id.rs index 9ef063c..e978d59 100644 --- a/src/token/id.rs +++ b/src/token/id.rs @@ -1,27 +1,10 @@ -use std::fmt; +pub type Id = crate::id::Id; -use crate::id::Id as BaseId; +#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub struct Token; -// Stable identifier for a token. Prefixed with `T`. -#[derive(Clone, Debug, Eq, Hash, PartialEq, sqlx::Type, serde::Deserialize, serde::Serialize)] -#[sqlx(transparent)] -#[serde(transparent)] -pub struct Id(BaseId); - -impl From for Id { - fn from(id: BaseId) -> Self { - Self(id) - } -} - -impl Id { - pub fn generate() -> Self { - BaseId::generate("T") - } -} - -impl fmt::Display for Id { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(f) +impl crate::id::Prefix for Token { + fn prefix(&self) -> &'static str { + "T" } } -- cgit v1.2.3