SlHelpers
Loading...
Searching...
No Matches
Person.h
1// SPDX-License-Identifier: GPL-2.0-only
2
3#pragma once
4
5#include <optional>
6#include <string>
7
8namespace SlKernCVS {
9
15class Role {
16private:
17 static constexpr const std::string_view roleNames[] = {
18 "Author",
19 "Signed-off-by",
20 "Co-developed-by",
21 "Suggested-by",
22 "Reviewed-by",
23 "Acked-by",
24 "Tested-by",
25 "Reported-by",
26 "Maintainer",
27 "Upstream"
28 };
29public:
36 enum RoleType {
37 Author,
38 SignedOffBy, FirstRole = SignedOffBy,
39 CoDevelopedBy,
40 SuggestedBy,
41 ReviewedBy,
42 AckedBy, LastRole = AckedBy,
43 TestedBy,
44 ReportedBy,
45 Maintainer,
46 Upstream,
47 Last
48 };
49 static_assert(Last == std::size(roleNames));
50
51 Role() = delete;
53 Role(size_t index) : m_role(static_cast<RoleType>(index)) {}
55 Role(RoleType role) : m_role(role) {}
56
58 RoleType role() const { return m_role; }
59
61 constexpr auto index() const { return static_cast<std::size_t>(m_role); }
63 constexpr const auto &toString() const { return roleNames[index()]; }
64private:
65 RoleType m_role;
66};
67
71class Person {
72public:
73 Person() = delete;
74
82 Person(Role r, std::string name, std::string email, unsigned count = 0) :
83 m_role(std::move(r)), m_name(std::move(name)), m_email(std::move(email)),
84 m_count(count) {}
85
87 const Role &role() const { return m_role; }
89 const std::string &name() const { return m_name; }
91 const std::string &email() const { return m_email; }
93 std::string userName() const { return m_email.substr(0, m_email.find("@")); }
94
99 auto count() const { return m_count; }
100
106 std::string pretty(bool includeName = true) const {
107 if (includeName && !name().empty())
108 return name() + " <" + email() + ">";
109 return email();
110 }
111
120 template <typename T>
121 std::string pretty(const T &translate, bool includeName = true) const {
122 if (includeName && !name().empty())
123 return name() + " <" + translate(email()) + ">";
124 return translate(email());
125 }
126
131 void setEmail(const std::string &email) { m_email = email; }
132
143 static std::optional<Person> parsePerson(std::string_view src, Role role);
144
150 static std::optional<Person> parse(std::string_view src)
151 {
152 for (std::size_t i = Role::FirstRole; i <= Role::LastRole; ++i) {
153 Role r(i);
154 if (src.starts_with(r.toString()))
155 if (auto p = parsePerson(src, std::move(r)))
156 return p;
157 }
158 return std::nullopt;
159 }
160private:
161 Role m_role;
162 std::string m_name;
163 std::string m_email;
164 unsigned m_count;
165
166};
167
168} // namespace
const std::string & email() const
Get e-mail of this Person.
Definition Person.h:91
const std::string & name() const
Get name of this Person.
Definition Person.h:89
static std::optional< Person > parse(std::string_view src)
Try to parse a line.
Definition Person.h:150
Person(Role r, std::string name, std::string email, unsigned count=0)
Create a Person with Role r, name, email and count of changes.
Definition Person.h:82
static std::optional< Person > parsePerson(std::string_view src, Role role)
Parse src into a Person.
const Role & role() const
Get Role of this Person.
Definition Person.h:87
auto count() const
Get count of changes historically done by this Person.
Definition Person.h:99
std::string userName() const
Get user name (from e-mail).
Definition Person.h:93
void setEmail(const std::string &email)
Re-set e-mail to email.
Definition Person.h:131
std::string pretty(const T &translate, bool includeName=true) const
Pretty format as "username <e-mail>".
Definition Person.h:121
std::string pretty(bool includeName=true) const
Pretty format as "username <e-mail>".
Definition Person.h:106
Role of a Person.
Definition Person.h:15
Role(RoleType role)
Construct new Role with role set.
Definition Person.h:55
RoleType role() const
Get the actual RoleType.
Definition Person.h:58
Role(size_t index)
Construct new Role with role set by the index.
Definition Person.h:53
constexpr const auto & toString() const
Convert Role to string.
Definition Person.h:63
constexpr auto index() const
Convert Role to number/index.
Definition Person.h:61
RoleType
Actual roles.
Definition Person.h:36