From 2420f1e75d54a5f209b0267715f078a369d81eb1 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Sun, 23 Mar 2025 15:58:33 -0400 Subject: Rename the `login` module to `user`. --- src/user/snapshot.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/user/snapshot.rs (limited to 'src/user/snapshot.rs') diff --git a/src/user/snapshot.rs b/src/user/snapshot.rs new file mode 100644 index 0000000..d548e06 --- /dev/null +++ b/src/user/snapshot.rs @@ -0,0 +1,52 @@ +use super::{ + Id, + event::{Created, Event}, +}; +use crate::name::Name; + +// This also implements FromRequestParts (see `./extract.rs`). As a result, it +// can be used as an extractor for endpoints that want to require a user, or for +// endpoints that need to behave differently depending on whether the client is +// or is not logged in. +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)] +pub struct User { + pub id: Id, + pub name: Name, + // The omission of the hashed password is deliberate, to minimize the + // chance that it ends up tangled up in debug output or in some other chunk + // of logic elsewhere. +} + +impl User { + // Without this allow, clippy wants the `Option` return type to be `Self`. It's not a bad + // suggestion, but we need `Option` here, for two reasons: + // + // 1. This is used to collect streams using a fold, below, which requires a type + // consistent with the fold, and + // 2. It's also consistent with the other history state machine types. + #[allow(clippy::unnecessary_wraps)] + fn apply(state: Option, event: Event) -> Option { + match (state, event) { + (None, Event::Created(event)) => Some(event.into()), + (state, event) => panic!("invalid message event {event:#?} for state {state:#?}"), + } + } +} + +impl FromIterator for Option { + fn from_iter>(events: I) -> Self { + events.into_iter().fold(None, User::apply) + } +} + +impl From<&Created> for User { + fn from(event: &Created) -> Self { + event.user.clone() + } +} + +impl From for User { + fn from(event: Created) -> Self { + event.user + } +} -- cgit v1.2.3