diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-03-23 15:58:33 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-03-23 16:25:22 -0400 |
| commit | 2420f1e75d54a5f209b0267715f078a369d81eb1 (patch) | |
| tree | 20edd531a3f2f765a23fef8e7a508c91bc7dc294 /src/login/create.rs | |
| parent | 7e15690d54ff849596401b43d163df9353062850 (diff) | |
Rename the `login` module to `user`.
Diffstat (limited to 'src/login/create.rs')
| -rw-r--r-- | src/login/create.rs | 95 |
1 files changed, 0 insertions, 95 deletions
diff --git a/src/login/create.rs b/src/login/create.rs deleted file mode 100644 index c5cea08..0000000 --- a/src/login/create.rs +++ /dev/null @@ -1,95 +0,0 @@ -use sqlx::{sqlite::Sqlite, Transaction}; - -use super::{password::StoredHash, repo::Provider as _, validate, History, Password}; -use crate::{ - clock::DateTime, - event::{repo::Provider as _, Broadcaster, Event}, - name::Name, -}; - -#[must_use = "dropping a login creation attempt is likely a mistake"] -pub struct Create<'a> { - name: &'a Name, - password: &'a Password, - created_at: &'a DateTime, -} - -impl<'a> Create<'a> { - pub fn begin(name: &'a Name, password: &'a Password, created_at: &'a DateTime) -> Self { - Self { - name, - password, - created_at, - } - } - - pub fn validate(self) -> Result<Validated<'a>, Error> { - let Self { - name, - password, - created_at, - } = self; - - if !validate::name(name) { - return Err(Error::InvalidName(name.clone())); - } - - let password_hash = password.hash()?; - - Ok(Validated { - name, - password_hash, - created_at, - }) - } -} - -#[must_use = "dropping a login creation attempt is likely a mistake"] -pub struct Validated<'a> { - name: &'a Name, - password_hash: StoredHash, - created_at: &'a DateTime, -} - -impl Validated<'_> { - pub async fn store(self, tx: &mut Transaction<'_, Sqlite>) -> Result<Stored, sqlx::Error> { - let Self { - name, - password_hash, - created_at, - } = self; - - let created = tx.sequence().next(created_at).await?; - let login = tx.logins().create(name, &password_hash, &created).await?; - - Ok(Stored { login }) - } -} - -#[must_use = "dropping a login creation attempt is likely a mistake"] -pub struct Stored { - login: History, -} - -impl Stored { - #[must_use = "dropping a login creation attempt is likely a mistake"] - pub fn publish(self, events: &Broadcaster) -> History { - let Self { login } = self; - - events.broadcast(login.events().map(Event::from).collect::<Vec<_>>()); - - login - } - - pub fn login(&self) -> &History { - &self.login - } -} - -#[derive(Debug, thiserror::Error)] -pub enum Error { - #[error("invalid login name: {0}")] - InvalidName(Name), - #[error(transparent)] - PasswordHash(#[from] password_hash::Error), -} |
