Lines
13.04 %
Functions
8.33 %
Branches
100 %
// This file is part of hnefatafl-copenhagen.
//
// hnefatafl-copenhagen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// hnefatafl-copenhagen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2025 David Campbell <david@hnefatafl.org>
use std::{
collections::{HashMap, HashSet},
fmt,
};
use crate::{email::Email, glicko::Rating};
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use crate::Id;
impl Accounts {
/// # Errors
///
/// If serialization fails.
pub fn display_admin(&self) -> anyhow::Result<String> {
let string = ron::ser::to_string(&self)?;
Ok(string)
}
impl fmt::Display for Accounts {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut accounts = Vec::new();
for (name, account) in &self.0 {
accounts.push(format!("{name} {account}"));
accounts.sort_unstable();
let accounts = accounts.join(" ");
write!(f, "{accounts}")
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct Account {
#[serde(default)]
pub email: Option<Email>,
/// A unix timestamp in seconds when the email was sent.
pub email_sent: i64,
pub password: String,
/// If logged in, holds the index into clients.
pub logged_in: Option<usize>,
pub draws: u64,
pub wins: u64,
pub losses: u64,
pub rating: Rating,
pub send_emails: bool,
#[serde(skip)]
pub pending_games: HashSet<Id>,
pub creation_date: DateTimeUtc,
pub last_logged_in: DateTimeUtc,
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct DateTimeUtc(pub Timestamp);
impl Default for DateTimeUtc {
fn default() -> Self {
Self(Timestamp::now())
impl fmt::Display for Account {
if self.logged_in.is_some() {
write!(
f,
"{} {} {} {} logged_in",
self.wins, self.losses, self.draws, self.rating
)
} else {
"{} {} {} {} logged_out",
pub struct Accounts(pub HashMap<String, Account>);