diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-07-24 23:18:06 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-07-24 23:53:37 -0400 |
| commit | cae0d11fa25160b38a411d4edd8a0f3b5b06df8c (patch) | |
| tree | ec90a0b7fbb5ccc9fda1fb8f2a81672d2587f9fc /src/invite/id.rs | |
| parent | aec3eaeebd37bce9ab4dad14e7e86ef0db8f0c2d (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/invite/id.rs')
| -rw-r--r-- | src/invite/id.rs | 26 |
1 files changed, 6 insertions, 20 deletions
diff --git a/src/invite/id.rs b/src/invite/id.rs index bd53f1f..c37f3ac 100644 --- a/src/invite/id.rs +++ b/src/invite/id.rs @@ -1,24 +1,10 @@ -use crate::id::Id as BaseId; +pub type Id = crate::id::Id<Invitation>; -// Stable identifier for an invite Prefixed with `I`. -#[derive(Clone, Debug, Eq, PartialEq, sqlx::Type, serde::Deserialize, serde::Serialize)] -#[sqlx(transparent)] -pub struct Id(BaseId); +#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub struct Invitation; -impl From<BaseId> for Id { - fn from(id: BaseId) -> Self { - Self(id) - } -} - -impl Id { - pub fn generate() -> Self { - BaseId::generate("I") - } -} - -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 Invitation { + fn prefix(&self) -> &'static str { + "I" } } |
