git_bug/entities/identity/snapshot/
mod.rs

1// git-bug-rs - A rust library for interfacing with git-bug repositories
2//
3// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
4// SPDX-License-Identifier: GPL-3.0-or-later
5//
6// This file is part of git-bug-rs/git-gub.
7//
8// You should have received a copy of the License along with this program.
9// If not, see <https://www.gnu.org/licenses/agpl.txt>.
10
11//! The [`Snapshot`] type specialized for [`Identities`][`Identity`].
12
13use std::fmt::Display;
14
15use url::Url;
16
17use super::Identity;
18use crate::replica::entity::snapshot::Snapshot;
19
20pub mod history_step;
21pub mod timeline;
22
23impl Snapshot<Identity> {
24    /// Returns the Identity's name at the time of this snapshot.
25    #[must_use]
26    // Only expects.
27    #[allow(clippy::missing_panics_doc)]
28    pub fn name(&self) -> &str {
29        &self
30            .timeline()
31            .name_history()
32            .last()
33            .expect("This is mandated by the crate op")
34            .name
35    }
36
37    /// Returns the Identity's email at the time of this snapshot.
38    #[must_use]
39    pub fn email(&self) -> Option<&str> {
40        self.timeline()
41            .email_history()
42            .last()
43            .map(|item| item.email.as_str())
44    }
45
46    /// Returns the Identity's login name at the time of this snapshot.
47    #[must_use]
48    pub fn login_name(&self) -> Option<&str> {
49        self.timeline()
50            .login_name_history()
51            .last()
52            .map(|item| item.login_name.as_str())
53    }
54
55    /// Returns the Identity's avatar url at the time of this snapshot.
56    #[must_use]
57    pub fn avatar_url(&self) -> Option<&Url> {
58        self.timeline()
59            .avatar_url_history()
60            .last()
61            .map(|item| &item.avatar_url)
62    }
63
64    /// Returns the Identity's meta data at the time of this snapshot.
65    ///
66    /// The iterator returns key value pairs.
67    #[must_use]
68    pub fn metadata(&self) -> Option<impl Iterator<Item = &(String, String)>> {
69        self.timeline()
70            .metadata_history()
71            .last()
72            .map(|item| item.metadata.iter())
73    }
74}
75
76impl Display for Snapshot<Identity> {
77    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78        match (self.email(), self.login_name()) {
79            (None, None) => f.write_str(self.name()),
80            (None, Some(login)) => write!(f, "{} ({login})", self.name()),
81            (Some(email), None) => write!(f, "{} <{email}>", self.name()),
82            (Some(email), Some(_login)) => write!(f, "{} <{email}>", self.name()),
83        }
84    }
85}