ghtui 0.2.5

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
//! Who wrote or committed a commit. A module of its own so that [`Login`]'s
//! field is private: a git name can't become one by accident, though a
//! hand-built [`GitActor`] or [`Login::unchecked`] can still make any.

use serde::{Deserialize, Serialize};

use crate::api::browse::GitActor;

/// Who wrote or committed a commit, as GitHub knows them. Kept externally
/// tagged in the cache: a login and a git name must not read back alike.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Person {
    /// The account GitHub linked to the commit's email: the only kind of
    /// person a profile link may be built from.
    User(Login),
    /// Only git's name for them, with no account behind it.
    Git(String),
    /// No author, or an empty name.
    Unknown,
}

impl Person {
    pub fn login(&self) -> Option<&str> {
        match self {
            Person::User(Login(login)) => Some(login),
            _ => None,
        }
    }

    /// Who git says wrote a commit (read from a clone, not GitHub): their
    /// account when the email is the no-reply address GitHub gives it
    /// (`123+login@users.noreply.github.com`), which GitHub links to that
    /// account too; else their name.
    pub fn from_git(name: &str, email: &str) -> Person {
        // Matched in place: this runs once per commit.
        const NOREPLY: &str = "users.noreply.";
        let host = crate::host::current().name();
        let host = host.split(':').next().unwrap_or_default();
        let login = (email.rsplit_once('@'))
            .filter(|(_, domain)| {
                let (noreply, rest) = domain.split_at_checked(NOREPLY.len()).unwrap_or_default();
                noreply.eq_ignore_ascii_case(NOREPLY) && rest.eq_ignore_ascii_case(host)
            })
            .map(|(user, _)| user.rsplit('+').next().unwrap_or(user))
            .filter(|login| {
                !login.is_empty() && login.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
            });
        match login {
            Some(login) => Person::User(Login(login.to_owned())),
            None if !name.trim().is_empty() => Person::Git(name.trim().to_owned()),
            None => Person::Unknown,
        }
    }

    /// The text to show. Not a login: never pass it to a URL builder.
    pub fn name(&self) -> &str {
        match self {
            Person::User(Login(name)) | Person::Git(name) => name,
            Person::Unknown => "unknown",
        }
    }
}

/// The login GitHub linked to a commit's email. Made only from a
/// [`GitActor`]'s `user`, by [`Login::unchecked`], or read back from the
/// cache (`Deserialize`, which only the store should use).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Login(String);

impl Login {
    /// A login that didn't come from GitHub: for fixtures and tests.
    pub fn unchecked(login: impl Into<String>) -> Login {
        Login(login.into())
    }
}

impl From<Option<GitActor>> for Person {
    fn from(actor: Option<GitActor>) -> Person {
        match actor {
            Some(GitActor { user: Some(u), .. }) => Person::User(Login(u.login)),
            Some(GitActor { name: Some(n), .. }) if !n.is_empty() => Person::Git(n),
            _ => Person::Unknown,
        }
    }
}