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, 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, Login::apply) } } impl From<&Created> for Login { fn from(event: &Created) -> Self { event.login.clone() } } impl From for Login { fn from(event: Created) -> Self { event.login } }