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>.

//! Implementation of the [`HistoryStep`] trait for use in
//! [`Identities`][`super::Identity`].

use url::Url;

use crate::replica::entity::{
    identity::IdentityStub, snapshot::timeline::history_step::HistoryStep, timestamp::TimeStamp,
};

/// The possible steps in the history of an [`Identity`][`super::Identity`].
#[derive(Debug, Clone)]
pub enum IdentityHistoryStep {
    /// A name change in the history.
    Name(NameHistoryStep),

    /// An email change in the history.
    Email(EmailHistoryStep),

    /// An login name change in the history.
    LoginName(LoginNameHistoryStep),

    /// An avatar URL change in the history.
    AvatarUrl(AvatarUrlHistoryStep),

    /// A metadata change in the history.
    Metadata(MetadataHistoryStep),
}

impl HistoryStep for IdentityHistoryStep {
    fn author(&self) -> IdentityStub {
        match self {
            IdentityHistoryStep::Name(a) => a.author,
            IdentityHistoryStep::Email(a) => a.author,
            IdentityHistoryStep::LoginName(a) => a.author,
            IdentityHistoryStep::AvatarUrl(a) => a.author,
            IdentityHistoryStep::Metadata(a) => a.author,
        }
    }

    fn at(&self) -> TimeStamp {
        match self {
            IdentityHistoryStep::Name(name_history_step) => name_history_step.at,
            IdentityHistoryStep::Email(email_history_step) => email_history_step.at,
            IdentityHistoryStep::LoginName(login_name_history_step) => login_name_history_step.at,
            IdentityHistoryStep::AvatarUrl(avatar_url_history_step) => avatar_url_history_step.at,
            IdentityHistoryStep::Metadata(metadata_history_step) => metadata_history_step.at,
        }
    }
}

/// One version of the [`Identity's`][`super::Identity`] name in the history.
#[derive(Debug, Clone)]
pub struct NameHistoryStep {
    /// The author of the change.
    pub author: IdentityStub,

    /// The new name.
    pub name: String,

    /// When this change happened.
    pub at: TimeStamp,
}

/// One version of the [`Identity's`][`super::Identity`] email in the history.
#[derive(Debug, Clone)]
pub struct EmailHistoryStep {
    /// The author of the change.
    pub author: IdentityStub,

    /// The new email.
    pub email: String,

    /// When this change happened.
    pub at: TimeStamp,
}

/// One version of the [`Identity's`][`super::Identity`] login name in the
/// history.
#[derive(Debug, Clone)]
pub struct LoginNameHistoryStep {
    /// The author of the change.
    pub author: IdentityStub,

    /// The new email.
    pub login_name: String,

    /// When this change happened.
    pub at: TimeStamp,
}

/// One version of the [`Identity's`][`super::Identity`] avatar URL in the
/// history.
#[derive(Debug, Clone)]
pub struct AvatarUrlHistoryStep {
    /// The author of the change.
    pub author: IdentityStub,

    /// The new URL.
    pub avatar_url: Url,

    /// When this change happened.
    pub at: TimeStamp,
}

/// One version of the [`Identity's`][`super::Identity`] metadata in the
/// history.
#[derive(Debug, Clone)]
pub struct MetadataHistoryStep {
    /// The author of the change.
    pub author: IdentityStub,

    /// The new metadata.
    pub metadata: Vec<(String, String)>,

    /// When this change happened.
    pub at: TimeStamp,
}