summaryrefslogtreecommitdiff
path: root/src/login/snapshot.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-10-09 11:45:46 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-10-09 11:45:46 -0400
commitfecc78192ff1ad83c6a2f41e35a65ac189d25c6f (patch)
tree481d82e99cf8aad8fe256d8186ae72bcee23bf9f /src/login/snapshot.rs
parentdd62b823e01934a0f841256fdb17b551091896bf (diff)
parent2f0b77e8fd02a137047c8975a573626cd76310ff (diff)
Merge branch 'wip/event-vocabulary'
Diffstat (limited to 'src/login/snapshot.rs')
-rw-r--r--src/login/snapshot.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/login/snapshot.rs b/src/login/snapshot.rs
new file mode 100644
index 0000000..1379005
--- /dev/null
+++ b/src/login/snapshot.rs
@@ -0,0 +1,49 @@
+use super::{
+ event::{Created, Event},
+ Id,
+};
+
+// This also implements FromRequestParts (see `./extract.rs`). As a result, it
+// can be used as an extractor for endpoints that want to require login, 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 Login {
+ pub id: Id,
+ pub name: String,
+ // 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 Login {
+ // Two reasons for this allow:
+ //
+ // 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<Self>, event: Event) -> Option<Self> {
+ match (state, event) {
+ (None, Event::Created(event)) => Some(event.into()),
+ (state, event) => panic!("invalid message event {event:#?} for state {state:#?}"),
+ }
+ }
+}
+
+impl FromIterator<Event> for Option<Login> {
+ fn from_iter<I: IntoIterator<Item = Event>>(events: I) -> Self {
+ events.into_iter().fold(None, Login::apply)
+ }
+}
+
+impl From<&Created> for Login {
+ fn from(event: &Created) -> Self {
+ event.login.clone()
+ }
+}
+
+impl From<Created> for Login {
+ fn from(event: Created) -> Self {
+ event.login
+ }
+}