1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! 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 {
let noreply = format!(
"@users.noreply.{}",
crate::host::current()
.name()
.split(':')
.next()
.unwrap_or_default()
);
let at = email.len().saturating_sub(noreply.len());
let login = (email.get(at..))
.filter(|domain| domain.eq_ignore_ascii_case(&noreply))
.and_then(|_| email.get(..at))
.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,
}
}
}