git-bug 0.2.4

A rust library for interfacing with git-bug repositories
Documentation
// git-bug-rs - A rust library for interfacing with git-bug repositories
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of git-bug-rs/git-gub.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/agpl.txt>.

//! The [`Snapshot`] type specialized for [`Identities`][`Identity`].

use std::fmt::Display;

use url::Url;

use super::Identity;
use crate::replica::entity::snapshot::Snapshot;

pub mod history_step;
pub mod timeline;

impl Snapshot<Identity> {
    /// Returns the Identity's name at the time of this snapshot.
    #[must_use]
    // Only expects.
    #[allow(clippy::missing_panics_doc)]
    pub fn name(&self) -> &str {
        &self
            .timeline()
            .name_history()
            .last()
            .expect("This is mandated by the crate op")
            .name
    }

    /// Returns the Identity's email at the time of this snapshot.
    #[must_use]
    pub fn email(&self) -> Option<&str> {
        self.timeline()
            .email_history()
            .last()
            .map(|item| item.email.as_str())
    }

    /// Returns the Identity's login name at the time of this snapshot.
    #[must_use]
    pub fn login_name(&self) -> Option<&str> {
        self.timeline()
            .login_name_history()
            .last()
            .map(|item| item.login_name.as_str())
    }

    /// Returns the Identity's avatar url at the time of this snapshot.
    #[must_use]
    pub fn avatar_url(&self) -> Option<&Url> {
        self.timeline()
            .avatar_url_history()
            .last()
            .map(|item| &item.avatar_url)
    }

    /// Returns the Identity's meta data at the time of this snapshot.
    ///
    /// The iterator returns key value pairs.
    #[must_use]
    pub fn metadata(&self) -> Option<impl Iterator<Item = &(String, String)>> {
        self.timeline()
            .metadata_history()
            .last()
            .map(|item| item.metadata.iter())
    }
}

impl Display for Snapshot<Identity> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match (self.email(), self.login_name()) {
            (None, None) => f.write_str(self.name()),
            (None, Some(login)) => write!(f, "{} ({login})", self.name()),
            (Some(email), None) => write!(f, "{} <{email}>", self.name()),
            (Some(email), Some(_login)) => write!(f, "{} <{email}>", self.name()),
        }
    }
}